Support @PreAuthorize
spring authorization server don't support spring security method security annotation,like @PreAuthorize
@EightMonth It's not clear to me how you intend on using @PreAuthorize in Spring Authorization Server.
Please provide more details so I can understand how you intend on using it.
Everyone knows that SAS (Spring Authorization Server) is based on spring security and is an implementation of spring security in the Oauth2 protocol. However, SAS supports global authentication, but does not support spring security method authentication. Annotations such as @ PreAuthorize @ PostAuthorize still apply to Spring security. However, due to the lack of corresponding Authority in the context, it is verified as insufficient permissions. And what caused this problem was that in the construction method of OAuth2AuthorizationGrantAuthenticationToken, an empty Au was given by default。
I try to @PreAuthorize("hasScope('admin')"),but has a tip is "Failed to evaluate expression 'hasScope('admin')'" from PreAuthorizeAuthorizationManager
@EightMonth Based on the screenshot provided, OAuth2AccessTokenAuthenticationToken is not meant to be used for Authorization. The getPrincipal() is the authenticated client (OAuth2ClientAuthenticationToken) NOT the authenticated user (resource owner), since this is a client flow to the token endpoint. The authority list is not needed here, hence the empty List.
This still does not explain where you would use @PreAuthorize("hasScope('admin')") since all the Protocol Endpoints are Filter implementations and not Controller's. Which method exactly are you placing @PreAuthorize? Please provide sample code.
I have already dealt with it myself, thanks
I think spring authorization server or resources server can support @PreAuthorize write SpEL like @PreAuthorize(“hasScope('read_message')”),but i just write it like @PreAuthorize(“hasAuthority('SCOPE_read_message')”) now, it's so ugly,DO YOU?
@EightMonth As mentioned, the Protocol Endpoints are implemented using Filter's NOT Controller's, therefore @PreAuthorize cannot be used. Furthermore, I do not see any use cases for @PreAuthorize in Spring Authorization Server.
However, you can use @PreAuthorize in Resource Server. If you have questions on how to use it in Resource Server, please ask on Stack Overflow. We prefer to use GitHub issues only for bugs and enhancements.
I'm closing this issue as @PreAuthorize capability is not needed in Spring Authorization Server.