AuthenticationEntryPoint used to handle spring security exceptions.
If you added oauth2 to your pom, you can use OAuth2AuthenticationEntryPoint for your entry point, the entry point for spring handling security exceptions, if you use the base for your authentication, you can configure it like this
http .httpBasic().authenticationEntryPoint(getCustomerEntryPoint()); @Bean protected AuthenticationEntryPoint getCustomerEntryPoint() { return new OAuth2AuthenticationEntryPoint(); }
as you can see exception handling in OAuth2AuthenticationEntryPoint
// Try to extract a SpringSecurityException from the stacktrace Throwable[] causeChain = throwableAnalyzer.determineCauseChain(e); Exception ase = (OAuth2Exception) throwableAnalyzer.getFirstThrowableOfType( OAuth2Exception.class, causeChain); if (ase != null) { return handleOAuth2Exception((OAuth2Exception) ase); } ase = (AuthenticationException) throwableAnalyzer.getFirstThrowableOfType(AuthenticationException.class, causeChain); if (ase != null) { return handleOAuth2Exception(new UnauthorizedException(e.getMessage(), e)); } ase = (AccessDeniedException) throwableAnalyzer .getFirstThrowableOfType(AccessDeniedException.class, causeChain); if (ase instanceof AccessDeniedException) { return handleOAuth2Exception(new ForbiddenException(ase.getMessage(), ase)); } ase = (HttpRequestMethodNotSupportedException) throwableAnalyzer .getFirstThrowableOfType(HttpRequestMethodNotSupportedException.class, causeChain); if (ase instanceof HttpRequestMethodNotSupportedException) { return handleOAuth2Exception(new MethodNotAllowed(ase.getMessage(), ase)); } return handleOAuth2Exception(new ServerErrorException(e.getMessage(), e));
it will try to get an OAuth2Exception , AuthenticationException , AccessDeniedException in order, I think you can use this.
And you can also set up an ExceptionTranslationFilter , this filter is in the spring security chain, and after that it will catch and handle an exception like AuthenticationException , AccessDeniedException , and this is the main entry point and will be useful for most authentication methods like FormLogin .
http .exceptionHandling().authenticationEntryPoint(getCustomerEntryPoint());
If you are not using spring oauth2, now you understand AuthenticationEntryPoint , so you can also implement your own AuthenticationEntryPoint and configure your response to the exception, just override the commence method, for example
BasicAuthenticationEntryPoint : it will return the header "WWW-Authenticate" LoginUrlAuthenticationEntryPoint : it will be redirected to the destination URL if authentication