Spring OAuth comes with OAuth2MethodSecurityExpressionHandler , a class that adds the ability to perform such checks using @PreAuthorize expressions. All you have to do is register this class, for example. for example if you use Javaconfig:
@Configuration @EnableGlobalMethodSecurity(prePostEnabled = true) public static class MethodSecurityConfig extends GlobalMethodSecurityConfiguration { @Override protected MethodSecurityExpressionHandler createExpressionHandler() { return new OAuth2MethodSecurityExpressionHandler(); } }
Now you can simply use:
@PreAuthorize("#oauth2.hasScope('requiredScope')")
to protect your request methods. To find out what additional methods are available for hasScope , check the OAuth2SecurityExpressionMethods class.
The downside is that OAuth2MethodSecurityExpressionHandler extends the OAuth2MethodSecurityExpressionHandler , and therefore you cannot combine it with other classes that also extend this class.
Alternatively, you can also map OAuth scopes to classic user roles .
yankee
source share