spring-authorization-server icon indicating copy to clipboard operation
spring-authorization-server copied to clipboard

Support @PreAuthorize

Open coldingcode opened this issue 2 years ago • 7 comments

spring authorization server don't support spring security method security annotation,like @PreAuthorize

coldingcode avatar Jan 09 '24 09:01 coldingcode

@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.

jgrandja avatar Jan 09 '24 21:01 jgrandja

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。 image

coldingcode avatar Jan 10 '24 01:01 coldingcode

I try to @PreAuthorize("hasScope('admin')"),but has a tip is "Failed to evaluate expression 'hasScope('admin')'" from PreAuthorizeAuthorizationManager

coldingcode avatar Jan 10 '24 07:01 coldingcode

@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.

jgrandja avatar Jan 10 '24 11:01 jgrandja

I have already dealt with it myself, thanks

coldingcode avatar Jan 11 '24 01:01 coldingcode

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?

coldingcode avatar Jan 12 '24 01:01 coldingcode

@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.

jgrandja avatar Jan 12 '24 11:01 jgrandja