| | |
| | | package com.lf.server.controller.sys; |
| | | |
| | | import com.lf.server.controller.BaseController; |
| | | import com.lf.server.entity.all.ResponseMsg; |
| | | import com.lf.server.entity.data.LoginEntity; |
| | | import com.lf.server.entity.data.TokenEntity; |
| | | import com.lf.server.entity.data.UsersEntity; |
| | | import com.lf.server.entity.sys.LoginInfo; |
| | | import com.lf.server.entity.sys.Result; |
| | | import com.lf.server.entity.sys.User; |
| | | import com.lf.server.service.sys.UserService; |
| | | import com.lf.server.helper.StringHelper; |
| | | import com.lf.server.service.data.LoginService; |
| | | import com.lf.server.service.data.TokenService; |
| | | import com.lf.server.service.data.UsersService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.servlet.ModelAndView; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | /** |
| | | * 签名控制器 |
| | |
| | | */ |
| | | @RestController |
| | | @RequestMapping("/sign") |
| | | public class SignController { |
| | | public class SignController extends BaseController { |
| | | @Autowired |
| | | UserService userService; |
| | | UsersService userService; |
| | | |
| | | @GetMapping("/getName") |
| | | public User getName(String loginName) { |
| | | return userService.queryUserByName(loginName); |
| | | } |
| | | @Autowired |
| | | LoginService loginService; |
| | | |
| | | @Autowired |
| | | TokenService tokenService; |
| | | |
| | | /** |
| | | * 跳到首页 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 跳到未授权页面 |
| | | * return "redirect:/toLogin" |
| | | * |
| | | * @return String |
| | | */ |
| | | @GetMapping("/noauth") |
| | | public ModelAndView toNoAuth(ModelAndView mv) { |
| | | mv.setViewName("noauth"); |
| | | |
| | | return mv; |
| | | } |
| | | |
| | | /** |
| | | * 登录认证 |
| | | * |
| | | * @return String |
| | | */ |
| | | @GetMapping("/login") |
| | | public ModelAndView login(String username, String password, String service, Integer rememberMe) { |
| | | ModelAndView mv = new ModelAndView(); |
| | | @PostMapping(value = "/login", produces = "application/json; charset=UTF-8") |
| | | public ResponseMsg<TokenEntity> login(@RequestBody UsersEntity user, HttpServletRequest req, HttpServletResponse res) { |
| | | try { |
| | | mv.setViewName("index"); |
| | | if (service != null && service.length() > 0) { |
| | | mv.addObject("url", service); |
| | | } else { |
| | | mv.addObject("msg", username); |
| | | if (user == null) { |
| | | return fail("请输入用户名和密码!", null); |
| | | } |
| | | } catch (Exception e) { |
| | | } |
| | | if (StringHelper.isEmpty(user.getUid())) { |
| | | return fail("用户名不能为空!", null); |
| | | } |
| | | if (StringHelper.isEmpty(user.getPwd())) { |
| | | return fail("密码不能为空!", null); |
| | | } |
| | | |
| | | return mv; |
| | | UsersEntity ue = userService.selectByUid(user.getUid()); |
| | | if (ue == null) { |
| | | return fail("用户名不存在!", null); |
| | | } |
| | | |
| | | LoginEntity le = loginService.getNewLogin(user.getId(), req); |
| | | if (!user.getPwd().equals(ue.getPwd())) { |
| | | le.setStatus(0); |
| | | le.setDescr("密码错误"); |
| | | loginService.insertLogin(le); |
| | | return fail("密码不正确!", null); |
| | | } |
| | | le.setStatus(1); |
| | | loginService.insertLogin(le); |
| | | |
| | | TokenEntity te = tokenService.getNewToken(ue.getId(), req); |
| | | tokenService.insertToken(te); |
| | | |
| | | return success(te); |
| | | } catch (Exception ex) { |
| | | return fail(ex.getMessage(), null); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | mv.setViewName("login"); |
| | | |
| | | return mv; |
| | | } |
| | | |
| | | /** |
| | | * Ajax登录 |
| | | * |
| | | * @param userInfo |
| | | * @return |
| | | */ |
| | | @PostMapping(value = "/ajaxLogin", produces = "application/json; charset=UTF-8") |
| | | public Result ajaxLogin(@RequestBody User userInfo) { |
| | | try { |
| | | LoginInfo loginInfo = userService.getLoginInfo(userInfo.getLoginName()); |
| | | |
| | | return new Result(200, loginInfo != null ? "登录成功" : "登录失败", loginInfo); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return new Result(500, e.getMessage()); |
| | | } |
| | | } |
| | | } |