I'm trying to add in spring-security-oauth to an existing app with spring-security. I'm using Java config.
I have an existing amended filter chain in place (with some custom filters added in) but requests to '/oauth/token' aren't using it, but are using the 'default' filter chain. How can i get access to the filter chain that's securing the oauth endpoints so that i can use the custom filters there also or can I wire in the OAuth endpoint(s) into the existing setup?
Spring Security maintains a filter chain internally where each of the filters has a particular responsibility and filters are added or removed from the configuration depending on which services are required. The ordering of the filters is important as there are dependencies between them.
The type WebSecurityConfigurerAdapter is deprecatedWell, it's because the developers of Spring framework encourage users to move towards a component-based security configuration.
Filters can modify inbound and outbound requests and responses including modification of headers, entity and other request/response parameters. Interceptors are used primarily for modification of entity input and output streams. You can use interceptors for example to zip and unzip output and input entity streams.
there is indeed a slightly smoother way using the interface AuthorizationServerConfigurer
.
You can stick to the annotation @EnableAuthorizationServer
and implement above interface in your configuration file. This will enable you to alter the oauth2-filter-chain by doing something like this:
@Configuration
@EnableWebSecurity
@EnableAuthorizationServer
public class SecurityConfig extends WebSecurityConfigurerAdapter
implements AuthorizationServerConfigurer
// some configuration ...
public void configure(AuthorizationServerSecurityConfigurer oauthSecurity) throws Exception {
oauthSecurity.addTokenEndpointAuthenticationFilter(new YourFilter());
}
// more configuration ...
}
In contrast to the addFilterXYX
-methods of HttpSecurity
you have no fine-grained influence here where the filter will be positioned in the filter chain. Any filter added by addTokenEndpointAuthenticationFilter
will be inserted before the BasicAuthenticationFilter
.
If you need to control the position of you filter in a more detailed way you could create a bean extending AuthorizationServerConfigurerAdapter
instead of using the annotation @EnableAuthorizationServer
. I did not try that but I guess you could then extend AuthorizationServerSecurityConfiguration
like systemfreund suggested without having to specify @Order(-1)
because only your custom configuration gets imported. Probably you would also have to @Import
AuthorizationServerEndpointsConfiguration
like it is done in the convenience annotation @EnableAuthorizationServer
.
It's probably not the best way to do it, but I did not manage to find a better approach. The idea is to provide a custom AuthorizationServerSecurityConfiguration
instance and override the default instance which is @Import
ed via @EnableAuthorizationServer
. We just need to make sure to add an @Order
annotation with higher precendence than the default configuration:
@EnableAuthorizationServer
@Import(CustomSecurityConfig.class)
public class Application {
}
@Configuration
@Order(-1)
public class CustomSecurityConfig extends AuthorizationServerSecurityConfiguration {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http); // do the default configuration first
http
.addFilterBefore(new MyFilter(), ...);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With