LoginController.java 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.jeff.tianti.controller;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.apache.shiro.SecurityUtils;
  6. import org.apache.shiro.authc.AuthenticationException;
  7. import org.apache.shiro.authc.LockedAccountException;
  8. import org.apache.shiro.authc.UsernamePasswordToken;
  9. import org.apache.shiro.subject.Subject;
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.ui.Model;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import com.jeff.tianti.common.util.Md5Util;
  14. import com.jeff.tianti.org.entity.Resource;
  15. import com.jeff.tianti.util.WebHelper;
  16. @Controller
  17. public class LoginController {
  18. @RequestMapping("/login")
  19. public String login(){
  20. return "login";
  21. }
  22. @RequestMapping("/do_login")
  23. public String doLogin(HttpServletRequest request, Model model){
  24. String username = request.getParameter("username");
  25. String pwd = request.getParameter("pwd");
  26. boolean rememberMe = false;
  27. String md5Pwd = Md5Util.generatePassword(pwd);
  28. try {
  29. UsernamePasswordToken token = new UsernamePasswordToken(username, md5Pwd, rememberMe);
  30. Subject subject = SecurityUtils.getSubject();
  31. subject.login(token);
  32. //跳转第一个菜单
  33. List<Resource> hasResource = (List<Resource>) request.getSession().getAttribute(WebHelper.SESSION_MENU_RESOURCE);
  34. if(hasResource != null && !hasResource.isEmpty()){
  35. for(Resource resource : hasResource){
  36. List<Resource> chResources = resource.getChildren();
  37. if(StringUtils.isNotBlank(resource.getUrl()) && (chResources == null || chResources.isEmpty())){
  38. return "redirect:" + resource.getUrl();
  39. }
  40. if(chResources != null && !chResources.isEmpty()){
  41. for(Resource chRes : chResources){
  42. if(StringUtils.isNotBlank(chRes.getUrl())){
  43. return "redirect:" + chRes.getUrl();
  44. }
  45. }
  46. }
  47. }
  48. }
  49. return "redirect:/user/list";
  50. } catch (LockedAccountException lae) {
  51. // lae.printStackTrace();
  52. model.addAttribute("msg", "账号已被禁用");
  53. } catch (AuthenticationException ae) {
  54. // ae.printStackTrace();
  55. model.addAttribute("msg", "账号或密码错误");
  56. } catch (Exception e) {
  57. // e.printStackTrace();
  58. model.addAttribute("msg", "登录异常");
  59. }
  60. return "login";
  61. }
  62. @RequestMapping("/login_out")
  63. public String loginOut(HttpServletRequest request){
  64. Subject subject = SecurityUtils.getSubject();
  65. subject.logout();
  66. return "redirect:/login";
  67. }
  68. }