-
-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for override AuthenticationEntryPoint #152
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to use @ConditionalOnMissingBean
for the default values and inject a complete Customizer<ExceptionHandlingConfigurer<HttpSecurity>>
in configureResourceServer
method.
@@ -40,6 +41,7 @@ public static HttpSecurity configureResourceServer( | |||
HttpSecurity http, | |||
ServerProperties serverProperties, | |||
SpringAddonsOidcResourceServerProperties addonsResourceServerProperties, | |||
AuthenticationEntryPoint exceptionHandlerAuthenticationEntryPoint, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe should we inject Customizer<ExceptionHandlingConfigurer<HttpSecurity>>
instead of just the AuthenticationEntryPoint
(see below for default beans). This would give more flexibility to framework users.
|
||
if (serverProperties.getSsl() != null && serverProperties.getSsl().isEnabled()) { | ||
http.requiresChannel(channel -> channel.anyRequest().requiresSecure()); | ||
} | ||
|
||
return httpPostProcessor.process(http); | ||
} | ||
|
||
|
||
public static HttpSecurity configureResourceServer( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this new method, in SpringAddonsOidcResourceServerBeans
, expose beans as follow:
@ConditionalOnMissingBean
@Bean
AuthenticationEntryPoint authenticationEntryPoint() {
return (request, response, authException) -> {
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, "Bearer realm=\"Restricted Content\"");
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
};
}
@ConditionalOnMissingBean
@Bean
Customizer<ExceptionHandlingConfigurer<HttpSecurity>> exceptionHandlingCustomizer(AuthenticationEntryPoint authenticationEntryPoint) {
return exceptionHandling -> exceptionHandling.authenticationEntryPoint(authenticationEntryPoint);
}
If an AuthenticationEntryPoint
or Customizer<ExceptionHandlingConfigurer<HttpSecurity>>
are provided in an application configuration, this beans would back off (because of @ConditionalOnMissingBean
) and the custom beans will be picked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be careful that in this way we also overwrite with the same implementation the authenticationEntryPoint bean property of the BearerTokenAuthenticationFilter, for this reason I had declared 2 of them so as not to silently change BearerTokenAuthenticationFilter, can it go if we use BearerTokenAuthenticationFilter as default for the two cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spent a all day in transports with my 10 months old daughter and had no time to give feedback yesterday (thanks to a tempest in France :/). sorry about that.
I hadn't noticed that we can configure authenticationEntryPoint
and accessDeniedHandler
in both the oauth2ResourceServer
and the exceptionHandling
. My bad.
Hi, I think so: we have 3 cases: AccessDeniedHandler -> responds to the FORBIDDEN 403 case Customizer<ExceptionHandlingConfigurer> -> intercepts the missing token authenticationEntryPoint at the BearerTokenAuthenticationFilter level intercepts an invalid token I would let you customize the configuration for all three. With the default set to what it uses from the factory |
As a side note, Actually, response.setStatusCode(principal instanceof AnonymousAuthenticationToken ? HttpStatus.UNAUTHORIZED : HttpStatus.FORBIDDEN); This means that you can process missing tokens with both
I'm not sure I suggest that we choose between one of the two:
@ConditionalOnMissingBean
@Bean
AuthenticationEntryPoint authenticationEntryPoint() {
return (request, response, authException) -> {
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, "Bearer realm=\"Restricted Content\"");
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
};
}
@ConditionalOnMissingBean
@Bean
Customizer<ExceptionHandlingConfigurer<HttpSecurity>> exceptionHandlingCustomizer(
AuthenticationEntryPoint authenticationEntryPoint,
Optional<AccessDeniedHandler> accessDeniedHandler) {
return exceptionHandling -> {
exceptionHandling.authenticationEntryPoint(authenticationEntryPoint);
accessDeniedHandler.ifPresent(exceptionHandling::accessDeniedHandler);
};
}
@Conditional(IsJwtDecoderResourceServerCondition.class)
@Order(Ordered.LOWEST_PRECEDENCE)
@Bean
SecurityFilterChain springAddonsJwtResourceServerSecurityFilterChain(
HttpSecurity http,
ServerProperties serverProperties,
SpringAddonsOidcProperties addonsProperties,
ResourceServerExpressionInterceptUrlRegistryPostProcessor authorizePostProcessor,
ResourceServerHttpSecurityPostProcessor httpPostProcessor,
AuthenticationManagerResolver<HttpServletRequest> authenticationManagerResolver,
Customizer<ExceptionHandlingConfigurer<HttpSecurity>> exceptionHandlingCustomizer)
throws Exception {
http.oauth2ResourceServer(server -> {
server.authenticationManagerResolver(authenticationManagerResolver);
});
http.exceptionHandling(exceptionHandlingCustomizer);
ServletConfigurationSupport
.configureResourceServer(http, serverProperties, addonsProperties.getResourceserver(), authorizePostProcessor, httpPostProcessor);
return http.build();
}
@ConditionalOnMissingBean
@Bean
AuthenticationEntryPoint authenticationEntryPoint() {
return (request, response, authException) -> {
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, "Bearer realm=\"Restricted Content\"");
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
};
}
@Conditional(IsJwtDecoderResourceServerCondition.class)
@Order(Ordered.LOWEST_PRECEDENCE)
@Bean
SecurityFilterChain springAddonsJwtResourceServerSecurityFilterChain(
HttpSecurity http,
ServerProperties serverProperties,
SpringAddonsOidcProperties addonsProperties,
ResourceServerExpressionInterceptUrlRegistryPostProcessor authorizePostProcessor,
ResourceServerHttpSecurityPostProcessor httpPostProcessor,
AuthenticationManagerResolver<HttpServletRequest> authenticationManagerResolver,
AuthenticationEntryPoint authenticationEntryPoint,
Optional<AccessDeniedHandler> accessDeniedHandler)
throws Exception {
http.oauth2ResourceServer(server -> {
server.authenticationManagerResolver(authenticationManagerResolver);
server.authenticationEntryPoint(authenticationEntryPoint);
accessDeniedHandler.ifPresent(server::accessDeniedHandler);
});
ServletConfigurationSupport
.configureResourceServer(http, serverProperties, addonsProperties.getResourceserver(), authorizePostProcessor, httpPostProcessor);
return http.build();
} |
I think the second one is better, I implement it and push it |
@ch4mpy push done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the many quick updates
I have already pushed the same feature for reactive applications. Will release with a bump to Spring Boot 3.1.5 as transient dependency. |
I hope to be able to help again in the future! |
Don not hesitate to open more tickets or PRs. I don't know why the If it isn't there tomorrow morning, I'll publish a new version with the same content. |
I have added possibility to ovveride AuthenticationEntryPoint for excpetion handing and and oauth2ResourceServer