#oauth2 security expressions on method level
A simpler solution would be to let Spring Boot autoconfigure. Adding the following dependency solved this for me:
compile('org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.0.4.RELEASE')
This is an old question, things have changed. With Spring Security 5 one should use:
.hasAuthority("SCOPE_scopename")
Spring adds authorities to the principal based on the scopes it received from the provider, prefixed with “SCOPE_“.
More info: https://www.baeldung.com/spring-security-openid-connect
I think you also need to add: @EnableGlobalMethodSecurity(prePostEnabled = true) in order to get it to work.
Answered on deferent page
To enable #oAuth2 security expressions it is only needed to set default expression handler as OAuth2MethodSecurityExpressionHandler instead of DefaultMethodSecurityExpressionHandler. Because OAuth2MethodSecurityExpressionHandler extends it anyway then the whole previous functionality remains the same. I my configuration I use both GlobalMethodSecurityConfiguration and WebSecurityConfigurerAdapter.
@Configuration
@EnableGlobalMethodSecurity
public class MethodSecurityConfiguration extends GlobalMethodSecurityConfiguration {
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
return new OAuth2MethodSecurityExpressionHandler();
}
}
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
...
}
@Configuration
@Import({ SecurityConfiguration.class, MethodSecurityConfiguration.class })
public class AppConfiguration {
...
}