Mapping user roles to oauth2 scopes/authorities

后端 未结 2 1546
刺人心
刺人心 2020-12-10 02:58

We have a entitlements database which has application id, roles and users mapped to roles per application. Following the advice on thread how do I map user roles to oauth2

2条回答
  •  青春惊慌失措
    2020-12-10 03:17

    I was struggling with same issue, since by default the securityContext has client details I extended the DefaultOauth2RequestFactory and have set the User authentication manually in SecurityContext

        public TokenRequest createTokenRequest(Map requestParameters, ClientDetails authenticatedClient) {
     SecurityContextHolder.getContext()
                 .setAuthentication(new UsernamePasswordAuthenticationToken(requestParameters.get("username"), null,
                         userDetailsService.loadUserByUsername(requestParameters.get("username")).getAuthorities()));
      return super.createTokenRequest(requestParameters, authenticatedClient);
        }
    

    With this code in place the SecurityContext will always be populated by User authentication rather than Client authentication you can do this for specific grant type aswell

    if (requestParameters.get("grant_type").equals("password")) { //same code as above }
    

提交回复
热议问题