@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { UsernamePasswordToken upToken = (UsernamePasswordToken) token; SimpleAccount account = getAccountFromUsername(upToken.getUsername()); if (account != null) { if (account.isLocked()) { throw new LockedAccountException("Account [" + account + "] is locked."); } if (account.isCredentialsExpired()) { String msg = "The credentials for account [" + account + "] are expired"; throw new ExpiredCredentialsException(msg); } } return account; }
@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authToken) throws AuthenticationException { OAuth2Token token = (OAuth2Token) authToken; SimpleAccount account = authorizationServer.getAccountFromAccessToken(token.getToken()); if (account != null) { if (account.isLocked()) { throw new LockedAccountException("Account [" + account + "] is locked."); } if (account.isCredentialsExpired()) { String msg = "The credentials for account [" + account + "] are expired"; throw new ExpiredCredentialsException(msg); } } return account; }
@Override protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) { if (WebHelper.isAjax((HttpServletRequest) request)) { Result result = Result.failure(); if (e instanceof IncorrectCredentialsException) { result.message("密码错误"); } else if (e instanceof ExpiredCredentialsException) { result.message("密码已过期"); } else if (e instanceof UnknownAccountException) { result.message("该账号不存在"); } else if (e instanceof DisabledAccountException) { result.message("该账号已禁用"); } else if (e instanceof LockedAccountException) { result.message("该账号已锁定"); } else if (e instanceof AccountException) { result.message("账号错误"); } else if (e instanceof CredentialsException) { result.message("密码错误"); } try { writeObject(request, response, result); } catch (IOException ex) { throw new RuntimeException(ex); } return false; } return super.onLoginFailure(token, e, request, response); }
@Override protected AuthenticationInfo doGetAuthenticationInfo( final AuthenticationToken token) throws AuthenticationException { if (isClosed()) { throw new ForwardedRuntimeException(AuthManagementException.class, 1003); } final UsernamePasswordToken upToken = (UsernamePasswordToken) token; final SimpleAccount account = this.users.get(upToken.getUsername()); if (account != null) { if (account.isLocked()) { throw new LockedAccountException("Account [" + account + "] is locked."); } if (account.isCredentialsExpired()) { throw new ExpiredCredentialsException( "The credentials for account [" + account + "] are expired"); } } return account; }
/** * Asserts that the persisted account is not expired, and if not, throws an ExpiredCredentialsException. * * @param account the persisted {@link AccountEntity} * @throws ExpiredCredentialsException it the account is expired */ private void assertCredentialsNotExpired(AccountEntity account) throws ExpiredCredentialsException { if (account.getIsCredentialsExpired()) { // the account is expired. Throw ExpiredCredentialsException StringBuilder sb = new StringBuilder(); sb.append("Credentials is expired for user "); sb.append(account.getUsername()); throw new ExpiredCredentialsException(sb.toString()); } }
/** * Simulates the unsuccessful authentication process through {@link PersistenceRealm}, * because the account has been expired. * <p> * {@link org.apache.shiro.authc.ExpiredCredentialsException} must be thrown during the process. */ @Test public void expiredCredentialsTest() { // create mocks AccountEntity accountMock = createMock(AccountEntity.class); UsernamePasswordToken tokenMock = createMock(UsernamePasswordToken.class); // expectations expect(tokenMock.getUsername()).andReturn(USERNAME); expect(accountRepositoryMock.getAccountByUsername(entityManagerMock, USERNAME)).andReturn(accountMock); expect(accountMock.getUsername()).andReturn(USERNAME); expect(accountMock.getPassword()).andReturn(PASSWORD_HASH); expect(accountMock.getPasswordSalt()).andReturn(PASSWORD_SALT); expect(accountMock.getIsCredentialsExpired()).andReturn(true); replayAll(); try { // authentificate token realm.getAuthenticationInfo(tokenMock); } catch (ExpiredCredentialsException e) { // expected verifyAll(); return; } fail("ExpiredCredentialException have must be thrown"); }