首先我们来看下我们整个流程图
我们看到他在最后一位从这个拦截器的下面代码:
// Attempt authorization
try {
this.accessDecisionManager .decide(authenticated , object , attributes );
}
catch (AccessDeniedException accessDeniedException ) {
publishEvent( new AuthorizationFailureEvent(object , attributes , authenticated, accessDeniedException ));
throw accessDeniedException;
}
private void handleSpringSecurityException(HttpServletRequest request, HttpServletResponse response , FilterChain chain,
RuntimeException exception) throws IOException, ServletException {
if (exception instanceof AuthenticationException) {
logger.debug( "Authentication exception occurred; redirecting to authentication entry point", exception);
sendStartAuthentication( request, response, chain, (AuthenticationException) exception );
}
else if (exception instanceof AccessDeniedException ) {
if (authenticationTrustResolver .isAnonymous(SecurityContextHolder.getContext().getAuthentication())) {
logger.debug( "Access is denied (user is anonymous); redirecting to authentication entry point",
exception);
sendStartAuthentication( request, response, chain, new InsufficientAuthenticationException(
"Full authentication is required to access this resource"));
}
else {
logger.debug( "Access is denied (user is not anonymous); delegating to AccessDeniedHandler", exception);
accessDeniedHandler.handle(request , response , (AccessDeniedException) exception);
}
}
}
protected void sendStartAuthentication(HttpServletRequest request, HttpServletResponse response , FilterChain chain,
AuthenticationException reason) throws ServletException, IOException {
// SEC-112: Clear the SecurityContextHolder's Authentication, as the
// existing Authentication is no longer considered valid
SecurityContextHolder. getContext().setAuthentication(null);
requestCache.saveRequest(request , response );
logger.debug( "Calling Authentication entry point." );
authenticationEntryPoint.commence(request , response , reason );
}
public void doFilter(ServletRequest request , ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest wrappedSavedRequest =
requestCache.getMatchingRequest((HttpServletRequest) request, (HttpServletResponse)response );
chain.doFilter( wrappedSavedRequest == null ? request : wrappedSavedRequest, response );
}