身份验证成功后,我可以更改角色吗?

问题描述 投票:-1回答:2

我成功通过了Spring Security OAuth2的Github身份验证。但是,通过Github获取的用户角色是USER_ROLE。

所以我想知道是否可以通过判断授权成功后获得的Github用户信息来修改相应的角色,以便控制用户的权限。

例如,getPrincipal()获取唯一的“名称”。然后通过“名称”(例如“ADMIN”)修改角色。最后,使用@PreAuthorize(“hasRole('ROLE_ADMIN')”)来控制权限。

或者还有其他更好的解决方案吗?我想在这个应用程序中集成G​​ithub的OAuth2授权和基于角色的权限管理。

java spring spring-boot spring-security
2个回答
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
投票

我最终通过重建OAuth2ClientAuthenticationProcessingFilter并覆盖successAuthentication方法来解决。

关键点在于,第一步是在身份验证成功后确定它是否是来自Principal的我的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();
            }
        }));
    }
}
}
© www.soinside.com 2019 - 2024. All rights reserved.