Java Code Examples for org.springframework.security.core.AuthenticationException#getCause()

The following examples show how to use org.springframework.security.core.AuthenticationException#getCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JwtAuthenticationEntryPoint.java    From spring-oauth2-jwt-jdbc with MIT License 6 votes vote down vote up
@Override
public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e)
        throws IOException, ServletException {
    httpServletResponse.setStatus(SC_FORBIDDEN);
    httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);

    String message;
    if(e.getCause() != null) {
        message = e.getCause().getMessage();
    } else {
        message = e.getMessage();
    }
    byte[] body = new ObjectMapper()
            .writeValueAsBytes(Collections.singletonMap("error", message));
    httpServletResponse.getOutputStream().write(body);
}
 
Example 2
Source File: RESTRequestParameterProcessingFilter.java    From airsonic-advanced with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {
    ErrorCode errorCode = null;
    if (exception.getCause() instanceof APIException) {
        errorCode = ((APIException) exception.getCause()).getError();
    } else {
        errorCode = ErrorCode.NOT_AUTHENTICATED;
    }

    sendErrorXml(request, response, errorCode);
}
 
Example 3
Source File: CustomAuthenticationEntryPoint.java    From lion with Apache License 2.0 5 votes vote down vote up
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

    log.error(authException.getMessage());

    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/json;charset=UTF-8");

    Throwable cause = authException.getCause();
    if (cause instanceof InvalidTokenException) {
        response.getWriter().print(JsonUtil.jsonObj2Str(Result.failure(ResponseCode.UNAUTHORIZED, "无效的 Access Token")));
    } else if (cause instanceof InvalidGrantException) {
        response.getWriter().print(JsonUtil.jsonObj2Str(Result.failure(ResponseCode.UNAUTHORIZED, "无效的 Refresh Token")));
    } else if (cause instanceof AccessDeniedException) {
        response.getWriter().print(JsonUtil.jsonObj2Str(Result.failure(ResponseCode.FORBIDDEN, "权限不足无法访问")));
    } else {
        response.getWriter().print(JsonUtil.jsonObj2Str(Result.failure(ResponseCode.UNAUTHORIZED, "尚未认证无法访问")));
    }

    /*
    if (isAjaxRequest(request)) {
        response.sendError(HttpStatus.UNAUTHORIZED.value(), authException.getMessage());
    } else {
        response.sendRedirect("/login");
    }
    */

}
 
Example 4
Source File: InsightsAuthenticationFilter.java    From Insights with Apache License 2.0 5 votes vote down vote up
/**
 * used when authentication provider throws exception
 *
 */
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
		AuthenticationException authException) throws IOException, ServletException {
	Log.error("unsuccessfulAuthentication ==== {}  ", authException);
	Throwable exceptionClass = authException.getCause();
	if (exceptionClass != null && exceptionClass.getClass().getName().contains("AccountExpiredException")) {
		AuthenticationUtils.setResponseMessage(response, AuthenticationUtils.TOKEN_EXPIRE_CODE, "Token Expire ");
	} else {
		AuthenticationUtils.setResponseMessage(response, AuthenticationUtils.UNAUTHORISE,
				"Authentication not successful, Please relogin ");
	}
}
 
Example 5
Source File: VerboseBasicAuthenticationEntryPoint.java    From hesperides with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
					 AuthenticationException authException) throws IOException {
	response.addHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"" + getRealmName() + "\"");
	response.setStatus(HttpStatus.UNAUTHORIZED.value());
	String errorMessage = authException.getMessage();
	if (authException.getCause() != null) {
		// LDAP error messages have been seen to contain \u0000 characters. We remove them:
		errorMessage += " : " + authException.getCause().getMessage().replace("\u0000", "");
	}
	response.getOutputStream().println(errorMessage);
}