Spring Security OAuth2: how to add multiple Security Filter Chain of type ResourceServerConfigurer?
I set up a Spring Boot multi modules (5 modules) app with Spring Security OAuth2. Everything works well but as the application is growing I want to separate the security part in each module. The main module enables everything:
@SpringBootApplication
@EnableResourceServer
@EnableAuthorizationServer
@EnableWebSecurity(debug = true)
public class Application {
...
}
Now in each module I defined a bean of type ResourceServerConfigurer
@Configuration
@Order(2)
public class Module1SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure( HttpSecurity http ) throws Exception {
http.sessionManagement().sessionCreationPolicy( STATELESS );
http.antMatcher( "/module1/**")
.authorizeRequests()
.antMatchers( "/module1/resource").authenticated()
.antMatchers( "/module1/test" ).authenticated()
.anyRequest().access( "#oauth2.hasScope('webclient')" );
}
}
Same thing with module2:
@Configuration
@Order(1)
public class Module2SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure( HttpSecurity http ) throws Exception {
http.sessionManagement().sessionCreationPolicy( STATELESS );
http.antMatcher( "/module2/**")
.authorizeRequests()
.antMatchers( "/module2/resource").authenticated()
.antMatchers( "/module2/test" ).authenticated()
.anyRequest().access( "#oauth2.hasScope('webclient')" );
}
}
And so on...
The problem is that only one FilterChain is registered, the one whith @Order(2)
. I took a look at the doc of ResourceServerConfigurer
and it states this:
... if more than one configures the same preoperty, then the last one wins. The configurers are sorted by Order before being applied
How can I proceed to bypass this limitation?
Thanks a lot.
EDIT
Doing this (extending WebSecurityConfigurerAdapter
instead of ResourceServerConfigurerAdapter
):
@Configuration
@Order(1)
public class Module2SecurityFilterChain extends WebSecurityConfigurerAdapter {...}
seems to register the filter chain but there is another problem, when I authenticate a user (getting token on /oauth/token
) I can't acces a resource protected by this chain, I got a 403 Forbidden
. How does this black box work?
spring-boot spring-security spring-security-oauth2
add a comment |
I set up a Spring Boot multi modules (5 modules) app with Spring Security OAuth2. Everything works well but as the application is growing I want to separate the security part in each module. The main module enables everything:
@SpringBootApplication
@EnableResourceServer
@EnableAuthorizationServer
@EnableWebSecurity(debug = true)
public class Application {
...
}
Now in each module I defined a bean of type ResourceServerConfigurer
@Configuration
@Order(2)
public class Module1SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure( HttpSecurity http ) throws Exception {
http.sessionManagement().sessionCreationPolicy( STATELESS );
http.antMatcher( "/module1/**")
.authorizeRequests()
.antMatchers( "/module1/resource").authenticated()
.antMatchers( "/module1/test" ).authenticated()
.anyRequest().access( "#oauth2.hasScope('webclient')" );
}
}
Same thing with module2:
@Configuration
@Order(1)
public class Module2SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure( HttpSecurity http ) throws Exception {
http.sessionManagement().sessionCreationPolicy( STATELESS );
http.antMatcher( "/module2/**")
.authorizeRequests()
.antMatchers( "/module2/resource").authenticated()
.antMatchers( "/module2/test" ).authenticated()
.anyRequest().access( "#oauth2.hasScope('webclient')" );
}
}
And so on...
The problem is that only one FilterChain is registered, the one whith @Order(2)
. I took a look at the doc of ResourceServerConfigurer
and it states this:
... if more than one configures the same preoperty, then the last one wins. The configurers are sorted by Order before being applied
How can I proceed to bypass this limitation?
Thanks a lot.
EDIT
Doing this (extending WebSecurityConfigurerAdapter
instead of ResourceServerConfigurerAdapter
):
@Configuration
@Order(1)
public class Module2SecurityFilterChain extends WebSecurityConfigurerAdapter {...}
seems to register the filter chain but there is another problem, when I authenticate a user (getting token on /oauth/token
) I can't acces a resource protected by this chain, I got a 403 Forbidden
. How does this black box work?
spring-boot spring-security spring-security-oauth2
add a comment |
I set up a Spring Boot multi modules (5 modules) app with Spring Security OAuth2. Everything works well but as the application is growing I want to separate the security part in each module. The main module enables everything:
@SpringBootApplication
@EnableResourceServer
@EnableAuthorizationServer
@EnableWebSecurity(debug = true)
public class Application {
...
}
Now in each module I defined a bean of type ResourceServerConfigurer
@Configuration
@Order(2)
public class Module1SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure( HttpSecurity http ) throws Exception {
http.sessionManagement().sessionCreationPolicy( STATELESS );
http.antMatcher( "/module1/**")
.authorizeRequests()
.antMatchers( "/module1/resource").authenticated()
.antMatchers( "/module1/test" ).authenticated()
.anyRequest().access( "#oauth2.hasScope('webclient')" );
}
}
Same thing with module2:
@Configuration
@Order(1)
public class Module2SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure( HttpSecurity http ) throws Exception {
http.sessionManagement().sessionCreationPolicy( STATELESS );
http.antMatcher( "/module2/**")
.authorizeRequests()
.antMatchers( "/module2/resource").authenticated()
.antMatchers( "/module2/test" ).authenticated()
.anyRequest().access( "#oauth2.hasScope('webclient')" );
}
}
And so on...
The problem is that only one FilterChain is registered, the one whith @Order(2)
. I took a look at the doc of ResourceServerConfigurer
and it states this:
... if more than one configures the same preoperty, then the last one wins. The configurers are sorted by Order before being applied
How can I proceed to bypass this limitation?
Thanks a lot.
EDIT
Doing this (extending WebSecurityConfigurerAdapter
instead of ResourceServerConfigurerAdapter
):
@Configuration
@Order(1)
public class Module2SecurityFilterChain extends WebSecurityConfigurerAdapter {...}
seems to register the filter chain but there is another problem, when I authenticate a user (getting token on /oauth/token
) I can't acces a resource protected by this chain, I got a 403 Forbidden
. How does this black box work?
spring-boot spring-security spring-security-oauth2
I set up a Spring Boot multi modules (5 modules) app with Spring Security OAuth2. Everything works well but as the application is growing I want to separate the security part in each module. The main module enables everything:
@SpringBootApplication
@EnableResourceServer
@EnableAuthorizationServer
@EnableWebSecurity(debug = true)
public class Application {
...
}
Now in each module I defined a bean of type ResourceServerConfigurer
@Configuration
@Order(2)
public class Module1SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure( HttpSecurity http ) throws Exception {
http.sessionManagement().sessionCreationPolicy( STATELESS );
http.antMatcher( "/module1/**")
.authorizeRequests()
.antMatchers( "/module1/resource").authenticated()
.antMatchers( "/module1/test" ).authenticated()
.anyRequest().access( "#oauth2.hasScope('webclient')" );
}
}
Same thing with module2:
@Configuration
@Order(1)
public class Module2SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure( HttpSecurity http ) throws Exception {
http.sessionManagement().sessionCreationPolicy( STATELESS );
http.antMatcher( "/module2/**")
.authorizeRequests()
.antMatchers( "/module2/resource").authenticated()
.antMatchers( "/module2/test" ).authenticated()
.anyRequest().access( "#oauth2.hasScope('webclient')" );
}
}
And so on...
The problem is that only one FilterChain is registered, the one whith @Order(2)
. I took a look at the doc of ResourceServerConfigurer
and it states this:
... if more than one configures the same preoperty, then the last one wins. The configurers are sorted by Order before being applied
How can I proceed to bypass this limitation?
Thanks a lot.
EDIT
Doing this (extending WebSecurityConfigurerAdapter
instead of ResourceServerConfigurerAdapter
):
@Configuration
@Order(1)
public class Module2SecurityFilterChain extends WebSecurityConfigurerAdapter {...}
seems to register the filter chain but there is another problem, when I authenticate a user (getting token on /oauth/token
) I can't acces a resource protected by this chain, I got a 403 Forbidden
. How does this black box work?
spring-boot spring-security spring-security-oauth2
spring-boot spring-security spring-security-oauth2
edited Jan 2 at 12:58
akuma8
asked Jan 1 at 22:58
akuma8akuma8
902823
902823
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can configure multiple matchers using across multiple beans by using requestMatchers().antMatchers(String...)
like so:
@Configuration
public class Module2SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/module2/**")
.authorizeRequests()
.antMatchers("/module2/resource").authenticated()
.antMatchers("/module2/test").authenticated()
.anyRequest().access("#oauth2.hasScope('webclient')");
}
}
It's a little confusing, but when you call http.antMatcher(String)
, this is stating that you want to match only against that one endpoint. So, calling it twice (once in Module1SecurityFilterChain
and then again in Module2SecurityFilterChain
), the second call overrides the first.
However, using http.requestMatchers().antMatchers(String)
indicates that the given String
should be added to the existing list of endpoints being already matched. You can think of antMatcher
as a bit like "setMatcher
" and antMatchers
like "appendMatcher
".
Thanks for replying, I want to try your suggestion but there isn't the methodauthorizeRequests()
afterhttp.requestMatchers().antMatchers(String)
– akuma8
Jan 2 at 21:08
I accept the response (after editing it to add theand()
method ^^) even if it's not exactly what I wanted but the goal seems to be the same. In fact I wanted to define multiple FilterChain but with you answer, there is only one mapped to all module's request. Like each module's security configuration is separated it's also a good option. Spring Security isn't easy to undestand at all, a real black box. Thanks again for your answer
– akuma8
Jan 2 at 21:41
Another question, why withWebSecurityConfigurerAdapter
we do not have this behavior? I mean, why itshttp.antMatcher(String)
creates another FilterChain and doesn't override the others?
– akuma8
Jan 2 at 21:48
I have another issue related to this solution, please see here: stackoverflow.com/q/54030887/6643803
– akuma8
Jan 3 at 22:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999591%2fspring-security-oauth2-how-to-add-multiple-security-filter-chain-of-type-resour%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can configure multiple matchers using across multiple beans by using requestMatchers().antMatchers(String...)
like so:
@Configuration
public class Module2SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/module2/**")
.authorizeRequests()
.antMatchers("/module2/resource").authenticated()
.antMatchers("/module2/test").authenticated()
.anyRequest().access("#oauth2.hasScope('webclient')");
}
}
It's a little confusing, but when you call http.antMatcher(String)
, this is stating that you want to match only against that one endpoint. So, calling it twice (once in Module1SecurityFilterChain
and then again in Module2SecurityFilterChain
), the second call overrides the first.
However, using http.requestMatchers().antMatchers(String)
indicates that the given String
should be added to the existing list of endpoints being already matched. You can think of antMatcher
as a bit like "setMatcher
" and antMatchers
like "appendMatcher
".
Thanks for replying, I want to try your suggestion but there isn't the methodauthorizeRequests()
afterhttp.requestMatchers().antMatchers(String)
– akuma8
Jan 2 at 21:08
I accept the response (after editing it to add theand()
method ^^) even if it's not exactly what I wanted but the goal seems to be the same. In fact I wanted to define multiple FilterChain but with you answer, there is only one mapped to all module's request. Like each module's security configuration is separated it's also a good option. Spring Security isn't easy to undestand at all, a real black box. Thanks again for your answer
– akuma8
Jan 2 at 21:41
Another question, why withWebSecurityConfigurerAdapter
we do not have this behavior? I mean, why itshttp.antMatcher(String)
creates another FilterChain and doesn't override the others?
– akuma8
Jan 2 at 21:48
I have another issue related to this solution, please see here: stackoverflow.com/q/54030887/6643803
– akuma8
Jan 3 at 22:52
add a comment |
You can configure multiple matchers using across multiple beans by using requestMatchers().antMatchers(String...)
like so:
@Configuration
public class Module2SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/module2/**")
.authorizeRequests()
.antMatchers("/module2/resource").authenticated()
.antMatchers("/module2/test").authenticated()
.anyRequest().access("#oauth2.hasScope('webclient')");
}
}
It's a little confusing, but when you call http.antMatcher(String)
, this is stating that you want to match only against that one endpoint. So, calling it twice (once in Module1SecurityFilterChain
and then again in Module2SecurityFilterChain
), the second call overrides the first.
However, using http.requestMatchers().antMatchers(String)
indicates that the given String
should be added to the existing list of endpoints being already matched. You can think of antMatcher
as a bit like "setMatcher
" and antMatchers
like "appendMatcher
".
Thanks for replying, I want to try your suggestion but there isn't the methodauthorizeRequests()
afterhttp.requestMatchers().antMatchers(String)
– akuma8
Jan 2 at 21:08
I accept the response (after editing it to add theand()
method ^^) even if it's not exactly what I wanted but the goal seems to be the same. In fact I wanted to define multiple FilterChain but with you answer, there is only one mapped to all module's request. Like each module's security configuration is separated it's also a good option. Spring Security isn't easy to undestand at all, a real black box. Thanks again for your answer
– akuma8
Jan 2 at 21:41
Another question, why withWebSecurityConfigurerAdapter
we do not have this behavior? I mean, why itshttp.antMatcher(String)
creates another FilterChain and doesn't override the others?
– akuma8
Jan 2 at 21:48
I have another issue related to this solution, please see here: stackoverflow.com/q/54030887/6643803
– akuma8
Jan 3 at 22:52
add a comment |
You can configure multiple matchers using across multiple beans by using requestMatchers().antMatchers(String...)
like so:
@Configuration
public class Module2SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/module2/**")
.authorizeRequests()
.antMatchers("/module2/resource").authenticated()
.antMatchers("/module2/test").authenticated()
.anyRequest().access("#oauth2.hasScope('webclient')");
}
}
It's a little confusing, but when you call http.antMatcher(String)
, this is stating that you want to match only against that one endpoint. So, calling it twice (once in Module1SecurityFilterChain
and then again in Module2SecurityFilterChain
), the second call overrides the first.
However, using http.requestMatchers().antMatchers(String)
indicates that the given String
should be added to the existing list of endpoints being already matched. You can think of antMatcher
as a bit like "setMatcher
" and antMatchers
like "appendMatcher
".
You can configure multiple matchers using across multiple beans by using requestMatchers().antMatchers(String...)
like so:
@Configuration
public class Module2SecurityFilterChain extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/module2/**")
.authorizeRequests()
.antMatchers("/module2/resource").authenticated()
.antMatchers("/module2/test").authenticated()
.anyRequest().access("#oauth2.hasScope('webclient')");
}
}
It's a little confusing, but when you call http.antMatcher(String)
, this is stating that you want to match only against that one endpoint. So, calling it twice (once in Module1SecurityFilterChain
and then again in Module2SecurityFilterChain
), the second call overrides the first.
However, using http.requestMatchers().antMatchers(String)
indicates that the given String
should be added to the existing list of endpoints being already matched. You can think of antMatcher
as a bit like "setMatcher
" and antMatchers
like "appendMatcher
".
answered Jan 2 at 20:29
jzheauxjzheaux
2,6192921
2,6192921
Thanks for replying, I want to try your suggestion but there isn't the methodauthorizeRequests()
afterhttp.requestMatchers().antMatchers(String)
– akuma8
Jan 2 at 21:08
I accept the response (after editing it to add theand()
method ^^) even if it's not exactly what I wanted but the goal seems to be the same. In fact I wanted to define multiple FilterChain but with you answer, there is only one mapped to all module's request. Like each module's security configuration is separated it's also a good option. Spring Security isn't easy to undestand at all, a real black box. Thanks again for your answer
– akuma8
Jan 2 at 21:41
Another question, why withWebSecurityConfigurerAdapter
we do not have this behavior? I mean, why itshttp.antMatcher(String)
creates another FilterChain and doesn't override the others?
– akuma8
Jan 2 at 21:48
I have another issue related to this solution, please see here: stackoverflow.com/q/54030887/6643803
– akuma8
Jan 3 at 22:52
add a comment |
Thanks for replying, I want to try your suggestion but there isn't the methodauthorizeRequests()
afterhttp.requestMatchers().antMatchers(String)
– akuma8
Jan 2 at 21:08
I accept the response (after editing it to add theand()
method ^^) even if it's not exactly what I wanted but the goal seems to be the same. In fact I wanted to define multiple FilterChain but with you answer, there is only one mapped to all module's request. Like each module's security configuration is separated it's also a good option. Spring Security isn't easy to undestand at all, a real black box. Thanks again for your answer
– akuma8
Jan 2 at 21:41
Another question, why withWebSecurityConfigurerAdapter
we do not have this behavior? I mean, why itshttp.antMatcher(String)
creates another FilterChain and doesn't override the others?
– akuma8
Jan 2 at 21:48
I have another issue related to this solution, please see here: stackoverflow.com/q/54030887/6643803
– akuma8
Jan 3 at 22:52
Thanks for replying, I want to try your suggestion but there isn't the method
authorizeRequests()
after http.requestMatchers().antMatchers(String)
– akuma8
Jan 2 at 21:08
Thanks for replying, I want to try your suggestion but there isn't the method
authorizeRequests()
after http.requestMatchers().antMatchers(String)
– akuma8
Jan 2 at 21:08
I accept the response (after editing it to add the
and()
method ^^) even if it's not exactly what I wanted but the goal seems to be the same. In fact I wanted to define multiple FilterChain but with you answer, there is only one mapped to all module's request. Like each module's security configuration is separated it's also a good option. Spring Security isn't easy to undestand at all, a real black box. Thanks again for your answer– akuma8
Jan 2 at 21:41
I accept the response (after editing it to add the
and()
method ^^) even if it's not exactly what I wanted but the goal seems to be the same. In fact I wanted to define multiple FilterChain but with you answer, there is only one mapped to all module's request. Like each module's security configuration is separated it's also a good option. Spring Security isn't easy to undestand at all, a real black box. Thanks again for your answer– akuma8
Jan 2 at 21:41
Another question, why with
WebSecurityConfigurerAdapter
we do not have this behavior? I mean, why its http.antMatcher(String)
creates another FilterChain and doesn't override the others?– akuma8
Jan 2 at 21:48
Another question, why with
WebSecurityConfigurerAdapter
we do not have this behavior? I mean, why its http.antMatcher(String)
creates another FilterChain and doesn't override the others?– akuma8
Jan 2 at 21:48
I have another issue related to this solution, please see here: stackoverflow.com/q/54030887/6643803
– akuma8
Jan 3 at 22:52
I have another issue related to this solution, please see here: stackoverflow.com/q/54030887/6643803
– akuma8
Jan 3 at 22:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999591%2fspring-security-oauth2-how-to-add-multiple-security-filter-chain-of-type-resour%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown