/**
  * 实际的登录代码 如果登录成功,跳转至首页;登录失败,则将失败信息反馈对用户
  *
  * @param request
  * @param model
  * @return
  */
 @RequestMapping(value = "/dologin.do")
 public String doLogin(HttpServletRequest request, Model model) {
   String msg = "";
   String userName = request.getParameter("userName");
   String password = request.getParameter("password");
   System.out.println(userName);
   System.out.println(password);
   UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
   token.setRememberMe(true);
   Subject subject = SecurityUtils.getSubject();
   try {
     subject.login(token);
     if (subject.isAuthenticated()) {
       return "index";
     } else {
       return "login";
     }
   } catch (IncorrectCredentialsException e) {
     msg = "登录密码错误. Password for account " + token.getPrincipal() + " was incorrect.";
     model.addAttribute("message", msg);
     System.out.println(msg);
   } catch (ExcessiveAttemptsException e) {
     msg = "登录失败次数过多";
     model.addAttribute("message", msg);
     System.out.println(msg);
   } catch (LockedAccountException e) {
     msg = "帐号已被锁定. The account for username " + token.getPrincipal() + " was locked.";
     model.addAttribute("message", msg);
     System.out.println(msg);
   } catch (DisabledAccountException e) {
     msg = "帐号已被禁用. The account for username " + token.getPrincipal() + " was disabled.";
     model.addAttribute("message", msg);
     System.out.println(msg);
   } catch (ExpiredCredentialsException e) {
     msg = "帐号已过期. the account for username " + token.getPrincipal() + "  was expired.";
     model.addAttribute("message", msg);
     System.out.println(msg);
   } catch (UnknownAccountException e) {
     msg = "帐号不存在. There is no user with username of " + token.getPrincipal();
     model.addAttribute("message", msg);
     System.out.println(msg);
   } catch (UnauthorizedException e) {
     msg = "您没有得到相应的授权!" + e.getMessage();
     model.addAttribute("message", msg);
     System.out.println(msg);
   }
   return "login";
 }
  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
      throws AuthenticationException {
    if (!(authenticationToken instanceof UsernamePasswordToken)) {
      throw new UnsupportedTokenException(
          "Token of type "
              + authenticationToken.getClass().getName()
              + " is not supported.  A "
              + UsernamePasswordToken.class.getName()
              + " is required.");
    }
    UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;

    String password = new String(token.getPassword());

    try {
      crowdClientHolder.getAuthenticationManager().authenticate(token.getUsername(), password);
      return new SimpleAuthenticationInfo(token.getPrincipal(), token.getCredentials(), getName());
    } catch (RemoteException e) {
      throw new AuthenticationException(DEFAULT_MESSAGE, e);
    } catch (com.atlassian.crowd.exception.InactiveAccountException e) {
      throw new AuthenticationException(DEFAULT_MESSAGE, e);
    } catch (com.atlassian.crowd.exception.ExpiredCredentialException e) {
      throw new AuthenticationException(DEFAULT_MESSAGE, e);
    } catch (com.atlassian.crowd.exception.InvalidAuthenticationException e) {
      throw new AuthenticationException(DEFAULT_MESSAGE, e);
    } catch (com.atlassian.crowd.exception.InvalidAuthorizationTokenException e) {
      throw new AuthenticationException(DEFAULT_MESSAGE, e);
    } catch (com.atlassian.crowd.exception.ApplicationAccessDeniedException e) {
      throw new AuthenticationException(DEFAULT_MESSAGE, e);
    }
  }
Example #3
0
 private AuthenticationInfo buildAuthenticationInfo(final UsernamePasswordToken token) {
   return new SimpleAuthenticationInfo(token.getPrincipal(), token.getCredentials(), getName());
 }