Spring OAuth2-使用附加信息授权URL
Spring security允许我们使用Spring OAuth2-使用附加信息授权URL,spring,spring-boot,spring-security,spring-security-oauth2,spring-oauth2,Spring,Spring Boot,Spring Security,Spring Security Oauth2,Spring Oauth2,Spring security允许我们使用hasAnyAuthority(),hasAnyRole(),hasRole()授权URL,如果我们设置了授权权限。若我创建了一个自定义令牌增强器,在其中我可以在令牌中添加附加信息,那个么有并没有办法使用附加信息进行授权 CustomTokenEnhancer: public final class CustomTokenEnhancer implements TokenEnhancer { @Override public OAuth2
hasAnyAuthority()
,hasAnyRole()
,hasRole()
授权URL,如果我们设置了授权权限。若我创建了一个自定义令牌增强器,在其中我可以在令牌中添加附加信息,那个么有并没有办法使用附加信息进行授权
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;
}
}
我认为你可以做你想做的事,而不需要使用额外的信息。OAuth协议不需要其他信息。它只是用于存储描述性信息。您应该能够通过客户机的作用域、权限和授权类型以及用户的权限(角色)实现所需。您可以查看Oauth2规范()以了解更多信息 您还可以使用不同的安全策略,如MethodSecurityConfig:
@EnableGlobalMethodSecurity(prePostEnabled = true)
public static class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
return new OAuth2MethodSecurityExpressionHandler();
}
}
@PreAuthorize("hasRole('ADMIN') OR #oauth2.clientHasRole('AUTHORIZED-MERCHANT')")
@GetMapping(value = "authorized-url")
public ResponseEntity<List<Order>> getOrders() {
return ResponseEntity.ok(orderService.getOrders());
}
@EnableGlobalMethodSecurity(preprestenabled=true)
公共静态类方法SecurityConfig扩展了GlobalMethodSecurityConfiguration{
@凌驾
受保护的MethodSecurityExpressionHandler createExpressionHandler(){
返回新的OAuth2MethodSecurityExpressionHandler();
}
}
@预授权(“hasRole('ADMIN')或#oauth2.clientHasRole('AUTHORIZED-MERCHANT'))
@GetMapping(value=“授权url”)
公共响应性命令(){
返回ResponseEntity.ok(orderService.getOrders());
}
@EnableGlobalMethodSecurity(prePostEnabled = true)
public static class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
return new OAuth2MethodSecurityExpressionHandler();
}
}
@PreAuthorize("hasRole('ADMIN') OR #oauth2.clientHasRole('AUTHORIZED-MERCHANT')")
@GetMapping(value = "authorized-url")
public ResponseEntity<List<Order>> getOrders() {
return ResponseEntity.ok(orderService.getOrders());
}