Using scopes as roles in Spring Security OAuth2 (provider)

后端 未结 2 800
深忆病人
深忆病人 2020-12-25 13:03

Let\'s consider a fairly simple hypothetical application where users can read or write posts.

Some users can read and write articles while some others can only read

相关标签:
2条回答
  • 2020-12-25 13:26

    You can configure your own AccessTokenConverter (mainly for JWT) and extract the claims you want from the JWT access token and generate an Authority object. Just define a Bean factory that return an AccessTokenConverter

    0 讨论(0)
  • 2020-12-25 13:41

    Scopes (and roles) are arbitrary strings, so there is no problem if you want to make then the same. To make the access rule declarations identical you could write an ExpressionHandler that tested authorities or scopes with the same values depending on the type of Authentication it found.

    A different approach suggests itself after you read the comments: add a custom TokenStore or ResourceServerTokenServices. These are easily accessible extension points and would permit modifying the OAuth2Authentication so that its granted authorities were the same as the scopes.

    My preference, however, is to control the allowed scopes using a OAuth2RequestFactory, limiting them at the point of the token grant to values that are consistent with the user's authorities.

    0 讨论(0)
提交回复
热议问题