@Override protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) { HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.setContentType("application/json;charset=utf-8"); try { //处理登录失败的异常 Throwable throwable = e.getCause() == null ? e : e.getCause(); R r = R.error(HttpStatus.SC_UNAUTHORIZED, throwable.getMessage()); String json = JSON.toJSONString(r); httpResponse.getWriter().print(json); } catch (IOException e1) { e1.printStackTrace(); } return false; }
@Override protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) { HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.setContentType("application/json;charset=utf-8"); try { //处理登录失败的异常 Throwable throwable = e.getCause() == null ? e : e.getCause(); R r = R.error(HttpStatus.SC_UNAUTHORIZED, throwable.getMessage()); String json = new Gson().toJson(r); httpResponse.getWriter().print(json); } catch (IOException e1) { } return false; }
@Override protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) { HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.setContentType("application/json;charset=utf-8"); try { //处理登录失败的异常 Throwable throwable = e.getCause() == null ? e : e.getCause(); R r = R.error(HttpStatus.SC_UNAUTHORIZED, throwable.getMessage()); String json = new Gson().toJson(r); httpResponse.getWriter().print(json); } catch (IOException e1) { } return false; }
@ExceptionHandler(AuthenticationException.class) public RestMessage<String> handleAuthenticationException(AuthenticationException ex, Locale locale) { String errorMessage = messageSource.getMessage(AUTHENTICATION_ERROR, null, locale); logger.error(errorMessage, ex.getCause()); return RestMessage.buildFail(HttpStatus.UNAUTHORIZED.value(), errorMessage); }
@Override protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) { HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.setContentType("application/json;charset=utf-8"); try { //处理登录失败的异常 Throwable throwable = e.getCause() == null ? e : e.getCause(); Result r = Result.error(HttpStatus.SC_UNAUTHORIZED, throwable.getMessage()); String json = new Gson().toJson(r); httpResponse.getWriter().print(json); } catch (IOException e1) { } return false; }
@Override protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) { HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.setContentType("application/json;charset=utf-8"); httpResponse.setHeader("Access-Control-Allow-Credentials", "true"); httpResponse.setHeader("Access-Control-Allow-Origin", HttpContextUtils.getOrigin()); try { //处理登录失败的异常 Throwable throwable = e.getCause() == null ? e : e.getCause(); R r = R.error(HttpStatus.SC_UNAUTHORIZED, throwable.getMessage()); String json = new Gson().toJson(r); httpResponse.getWriter().print(json); } catch (IOException e1) { } return false; }
@Override protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) { HttpServletResponse httpResponse = (HttpServletResponse) response; HttpServletRequest httpRequest = (HttpServletRequest) request; httpResponse.setContentType("application/json;charset=utf-8"); httpResponse.setHeader("Access-Control-Allow-Credentials", "true"); httpResponse.setHeader("Access-Control-Allow-Origin", httpRequest.getHeader("Origin")); try { //处理登录失败的异常 Throwable throwable = e.getCause() == null ? e : e.getCause(); String json =JacksonUtils.toJson(ResultMap.error(HttpStatus.SC_UNAUTHORIZED, throwable.getMessage())); httpResponse.getWriter().print(json); } catch (IOException e1) { } return false; }
out.println(e.getCause().getMessage()); if (debug) { e.printStackTrace(out);
out.println(e.getCause().getMessage()); if (debug) { e.printStackTrace(out);
@Override //KNOX-534 overriding this method to be able to audit authentication exceptions protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws org.apache.shiro.authc.AuthenticationException { try { return super.doGetAuthenticationInfo(token); } catch ( org.apache.shiro.authc.AuthenticationException e ) { auditor.audit( Action.AUTHENTICATION , token.getPrincipal().toString(), ResourceType.PRINCIPAL, ActionOutcome.FAILURE, e.getMessage() ); ShiroLog.failedLoginInfo(token); ShiroLog.failedLoginStackTrace(e); ShiroLog.failedLoginAttempt(e.getCause()); throw e; } }
if ( e.getCause() != null && e.getCause() instanceof AuthProviderTimeoutException ) Throwable cause = e.getCause().getCause(); securityLog.error( "[%s]: failed to log in: auth server timeout%s", escape( token.getPrincipal().toString() ), cause != null && cause.getMessage() != null ? " (" + cause.getMessage() + ")" : "" ); throw new AuthProviderTimeoutException( e.getCause().getMessage(), e.getCause() ); else if ( e.getCause() != null && e.getCause() instanceof AuthProviderFailedException ) Throwable cause = e.getCause().getCause(); securityLog.error( "[%s]: failed to log in: auth server connection refused%s", escape( token.getPrincipal().toString() ), cause != null && cause.getMessage() != null ? " (" + cause.getMessage() + ")" : "" ); throw new AuthProviderFailedException( e.getCause().getMessage(), e.getCause() ); Throwable cause = e.getCause(); Throwable causeCause = e.getCause() != null ? e.getCause().getCause() : null; String errorMessage = String.format( "invalid principal or credentials%s%s", cause != null && cause.getMessage() != null ? " (" + cause.getMessage() + ")" : "",
@Override //KNOX-534 overriding this method to be able to audit authentication exceptions protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws org.apache.shiro.authc.AuthenticationException { try { return super.doGetAuthenticationInfo(token); } catch ( org.apache.shiro.authc.AuthenticationException e ) { auditor.audit( Action.AUTHENTICATION , token.getPrincipal().toString(), ResourceType.PRINCIPAL, ActionOutcome.FAILURE, e.getMessage() ); ShiroLog.failedLoginInfo(token); ShiroLog.failedLoginStackTrace(e); ShiroLog.failedLoginAttempt(e.getCause()); throw e; } }
private void authenticateUser(Subject currentUser, ShiroSecurityToken securityToken) { boolean authenticated = currentUser.isAuthenticated(); boolean sameUser = securityToken.getUsername().equals(currentUser.getPrincipal()); LOG.trace("Authenticated: {}, same Username: {}", authenticated, sameUser); if (!authenticated || !sameUser) { UsernamePasswordToken token = new UsernamePasswordToken(securityToken.getUsername(), securityToken.getPassword()); if (policy.isAlwaysReauthenticate()) { token.setRememberMe(false); } else { token.setRememberMe(true); } try { currentUser.login(token); LOG.debug("Current user {} successfully authenticated", currentUser.getPrincipal()); } catch (UnknownAccountException uae) { throw new UnknownAccountException("Authentication Failed. There is no user with username of " + token.getPrincipal(), uae.getCause()); } catch (IncorrectCredentialsException ice) { throw new IncorrectCredentialsException("Authentication Failed. Password for account " + token.getPrincipal() + " was incorrect!", ice.getCause()); } catch (LockedAccountException lae) { throw new LockedAccountException("Authentication Failed. The account for username " + token.getPrincipal() + " is locked." + "Please contact your administrator to unlock it.", lae.getCause()); } catch (AuthenticationException ae) { throw new AuthenticationException("Authentication Failed.", ae.getCause()); } } }