Spring security allows us to authorize URLs with hasAnyAuthority()
, hasAnyRole()
, hasRole()
if we set granted authorities. If I create a custom token enhancer where I can add additional information in my token, is there a way to make authorization with the additional information?
CustomTokenEnhancer:
public final class CustomTokenEnhancer implements TokenEnhancer {
@Override
public OAuth2AccessToken enhance(
OAuth2AccessToken accessToken,
OAuth2Authentication authentication) {
Map<String, Object> additionalInfo = new HashMap<>();
additionalInfo.put("company", "authorizeAPIsWithCompany");
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
return accessToken;
}
}
is it possible authorize APIs based on above additional info key, value? If not, how should I approach this idea?
e.g.:
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/authorizedURL").hasCompany("authorizeAPIsWithCompany")
.....
}