| | |
| | | import com.lf.server.entity.sys.Result; |
| | | import com.lf.server.entity.sys.User; |
| | | import com.lf.server.service.sys.UserService; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.apache.shiro.authc.*; |
| | | import org.apache.shiro.subject.Subject; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.servlet.ModelAndView; |
| | |
| | | public ModelAndView toIndex(ModelAndView mv) { |
| | | mv.setViewName("index"); |
| | | |
| | | User user = (User) SecurityUtils.getSubject().getPrincipal(); |
| | | User user = null; |
| | | if (user != null) { |
| | | mv.addObject("msg", "Hello " + user.getLoginName() + " !"); |
| | | } |
| | |
| | | |
| | | /** |
| | | * 跳到未授权页面 |
| | | * return "redirect:/toLogin" |
| | | * |
| | | * @return String |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 用于测试记住我和认证的区别 |
| | | * |
| | | * @return String |
| | | */ |
| | | @GetMapping("/buy") |
| | | public String buy() { |
| | | Subject subject = SecurityUtils.getSubject(); |
| | | |
| | | // 只有认证后才能访问,如果只是记住我则需要先登录 |
| | | if (!subject.isAuthenticated()) { |
| | | return "redirect:/toLogin"; |
| | | } |
| | | |
| | | return "add"; |
| | | } |
| | | |
| | | /** |
| | | * 登录认证 |
| | | * |
| | | * @return String |
| | |
| | | public ModelAndView login(String username, String password, String service, Integer rememberMe) { |
| | | ModelAndView mv = new ModelAndView(); |
| | | try { |
| | | Subject subject = SecurityUtils.getSubject(); |
| | | |
| | | // 获取令牌 |
| | | UsernamePasswordToken token = new UsernamePasswordToken(username, password); |
| | | if (rememberMe != null && rememberMe == 1) { |
| | | token.setRememberMe(true); |
| | | } |
| | | |
| | | // 登录认证 |
| | | subject.login(token); |
| | | |
| | | mv.setViewName("index"); |
| | | if (service != null && service.length() > 0) { |
| | | mv.addObject("url", service); |
| | | } else { |
| | | mv.addObject("msg", username); |
| | | } |
| | | } catch (UnknownAccountException e) { // 返回null就会进入这里 |
| | | mv.setViewName("login"); |
| | | mv.addObject("msg", "用户名不存在!"); |
| | | } catch (IncorrectCredentialsException e) { // 密码错误就会进入这里 |
| | | mv.setViewName("login"); |
| | | mv.addObject("msg", "密码错误!"); |
| | | } catch (Exception e) { |
| | | } |
| | | |
| | | return mv; |
| | |
| | | */ |
| | | @GetMapping("/logout") |
| | | public ModelAndView logout() { |
| | | Subject subject = SecurityUtils.getSubject(); |
| | | subject.logout(); |
| | | |
| | | ModelAndView mv = new ModelAndView(); |
| | | mv.setViewName("login"); |
| | | |
| | |
| | | @PostMapping(value = "/ajaxLogin", produces = "application/json; charset=UTF-8") |
| | | public Result ajaxLogin(@RequestBody User userInfo) { |
| | | try { |
| | | Subject subject = SecurityUtils.getSubject(); |
| | | |
| | | UsernamePasswordToken token = new UsernamePasswordToken(userInfo.getLoginName(), userInfo.getPassword()); |
| | | subject.login(token); |
| | | |
| | | LoginInfo loginInfo = userService.getLoginInfo(userInfo.getLoginName()); |
| | | |
| | | return new Result(200, loginInfo != null ? "登录成功" : "登录失败", loginInfo); |
| | | } catch (IncorrectCredentialsException e) { |
| | | return new Result(500, "密码错误"); |
| | | } catch (LockedAccountException e) { |
| | | return new Result(500, "登录失败,该用户已被冻结"); |
| | | } catch (AuthenticationException e) { |
| | | return new Result(500, "该用户不存在"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return new Result(500, e.getMessage()); |