2017-12-31 227 views
-1

私はSpring Security OAuth2でGithub認証に成功しました。ただし、Githubを介して取得されるユーザー役割はUSER_ROLEです。認証が成功した後にロールを変更することはできますか?

私は、ユーザーのアクセス許可を制御するために、認証が成功した後に取得されたGithubユーザー情報を判断することによって、対応する役割を変更できるかどうかを知りました。

たとえば、getPrincipal()は一意の「名前」を取得します。次に、「名前」(「ADMIN」など)で役割を変更します。最後に@PreAuthorize( "hasRole( 'ROLE_ADMIN')")を使用して権限を制御します。

他にも優れたソリューションがありますか?私はこのアプリケーションでGithubのOAuth2認証と役割ベースの権利管理を統合したいと考えています。

答えて

0

最も簡単なオプションは、現在の認証を取得し、その新しいインスタンスを作成し、SecurityContextHolderの内容を直接設定するで説明したようにSecurityContextHolderで設定することです。複数のスレッドで認証に作用することの意味を理解しておいてください(reference to understandを読んでください)。現在の認証にGrantedAuthorityを追加する例を以下に示す:

// update the current Authentication 
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(auth.getAuthorities()); 
authorities.add(new GrantedAuthorityImpl('ROLE_NEWROLE')); 
Authentication newAuth = new UsernamePasswordToken(auth.getPrincipal(),auth.getCredentials(),authorities) 
SecurityContextHolder.getContext().setAuthentication(newAuth); 
+0

UsernamePasswordTokenが間違っています。 OAuthなどの認証方法は異なります。最終的にOAuth2ClientAuthenticationProcessingFilterを再構築し、successfulAuthenticationメソッドをオーバーライドして解決しました。重要な点は、認証が成功した後に、プリンシパルから自分のGithubアカウントであるかどうかを判断することです。次に、SecurityContextHolder.getContext()によるOAuth2Authenticationの現在の正常な認証。 GetAuthentication()get。その後、新しい認証を作成し、SecurityContextHolderに新しいOAuth2Authenticationを作成します。 – Cciradih

0

やっとOAuth2ClientAuthenticationProcessingFilterを再構築し、successfulAuthenticationメソッドをオーバーライドすることにより解決しました。

重要なポイントは、認証が成功した後、プリンシパルから自分のGithubアカウントであるかどうかを判断することです。

次に、SecurityContextHolder.getContext()。getAuthentication()によって現在成功している認証OAuth2Authenticationが取得されます。その後、新しい認証を作成し、SecurityContextHolderに新しいOAuth2Authenticationを作成します。

public class CustomOAuth2ClientAuthenticationProcessingFilter extends OAuth2ClientAuthenticationProcessingFilter { 
public CustomOAuth2ClientAuthenticationProcessingFilter(String defaultFilterProcessesUrl) { 
    super(defaultFilterProcessesUrl); 
} 

@Override 
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException { 
    super.successfulAuthentication(request, response, chain, authResult); 
    if (authResult.getPrincipal().equals("cciradih")) { 
     OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication(); 
     SecurityContextHolder.getContext().setAuthentication(new OAuth2Authentication(oAuth2Authentication.getOAuth2Request(), new Authentication() { 
      @Override 
      public Collection<? extends GrantedAuthority> getAuthorities() { 
       return AuthorityUtils.createAuthorityList("ROLE_ADMIN", "ROLE_USER"); 
      } 

      @Override 
      public Object getCredentials() { 
       return oAuth2Authentication.getCredentials(); 
      } 

      @Override 
      public Object getDetails() { 
       return oAuth2Authentication.getUserAuthentication().getDetails(); 
      } 

      @Override 
      public Object getPrincipal() { 
       return oAuth2Authentication.getPrincipal(); 
      } 

      @Override 
      public boolean isAuthenticated() { 
       return oAuth2Authentication.getUserAuthentication().isAuthenticated(); 
      } 

      @Override 
      public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException { 

      } 

      @Override 
      public String getName() { 
       return oAuth2Authentication.getName(); 
      } 
     })); 
    } 
} 
} 
関連する問題