当前位置: 首页 > 知识库问答 >
问题:

向来自外部源的KeyClope身份验证添加其他角色

王泓
2023-03-14

我想通过KeyClope对用户进行身份验证,但我需要向Spring Security使用的身份验证对象添加额外的角色。添加的角色保存在Postgres数据库中。

>

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    ApplicationAuthenticationProvider provider = new ApplicationAuthenticationProvider();
    provider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
    auth.authenticationProvider(provider);
}

@组件公共类Application AuthenticationProvider扩展KeycloakAuthenticationProvider{

@Autowired
private UserService userService;

private GrantedAuthoritiesMapper grantedAuthoritiesMapper;

public void setGrantedAuthoritiesMapper(GrantedAuthoritiesMapper grantedAuthoritiesMapper) {
    this.grantedAuthoritiesMapper = grantedAuthoritiesMapper;
}

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    KeycloakAuthenticationToken token = (KeycloakAuthenticationToken) authentication;
    List<GrantedAuthority> grantedAuthorities = new ArrayList<>();

    String username = ((KeycloakAuthenticationToken) authentication)
            .getAccount().getKeycloakSecurityContext().getToken().getPreferredUsername();
    List<Role> roles = userService.findRoles(username);

    for (Role role : roles) {
        grantedAuthorities.add(new KeycloakRole(role.toString()));
    }
    return new KeycloakAuthenticationToken(token.getAccount(), token.isInteractive(), mapAuthorities(grantedAuthorities));
}

@Override
public boolean supports(Class<?> authentication) {
    return authentication.equals(UsernamePasswordAuthenticationToken.class);
}

private Collection<? extends GrantedAuthority> mapAuthorities(
        Collection<? extends GrantedAuthority> authorities) {
    return grantedAuthoritiesMapper != null
            ? grantedAuthoritiesMapper.mapAuthorities(authorities)
            : authorities;
}

}

尝试添加额外的过滤器,但我不确定正确的配置。

@豆子

@Override
protected KeycloakAuthenticationProcessingFilter keycloakAuthenticationProcessingFilter() throws Exception {
    RequestMatcher requestMatcher =
            new OrRequestMatcher(
                    new AntPathRequestMatcher("/api/login"),
                    new QueryParamPresenceRequestMatcher(OAuth2Constants.ACCESS_TOKEN),
                    // We're providing our own authorization header matcher
                    new IgnoreKeycloakProcessingFilterRequestMatcher()
            );
    return new KeycloakAuthenticationProcessingFilter(authenticationManagerBean(), requestMatcher);
}

// Matches request with Authorization header which value doesn't start with "Basic " prefix
private class IgnoreKeycloakProcessingFilterRequestMatcher implements RequestMatcher {
    IgnoreKeycloakProcessingFilterRequestMatcher() {
    }

public boolean matches(HttpServletRequest request) {
    String authorizationHeaderValue = request.getHeader("Authorization");
    return authorizationHeaderValue != null && !authorizationHeaderValue.startsWith("Basic ");
}

}

共有1个答案

翟聪
2023-03-14

现在,我只在登录/密码时使用KeyClope。角色和权限现在保存在本地数据库中。

 类似资料:
  • 我想添加只允许经过身份验证的用户在Jitsi会议中创建会议的功能。我看到了外部API的jwt参数、config.tokenAuthUrl和lib-jitsi-met令牌文档,但我对如何将它们放在一起感到非常困惑。 现在,我的工作流程如下: 用户通过自定义应用程序登录谷歌。 用户被重定向到一个新的Jitsi会议,使用从Google登录信息派生的jwt参数。 我遇到的问题是验证这个令牌,以及如何设置它

  • 我有一个REST服务,它依赖于外部系统来验证令牌,但需要自己进行授权(使用like@Secured进行API级访问)。 要求: UI使用外部系统生成令牌 一种可能的解决方案是使用过滤器: > UI使用外部系统生成令牌 UI使用令牌对我的服务进行REST调用 我的服务有一个过滤器,它使用令牌调用外部系统 有效令牌的外部系统发回用户详细信息 我对成功呼叫集的服务与SecurityContextHold

  • 授权服务器可以支持任何与其安全要求匹配的合适的HTTP身份验证方案。当使用其他身份验证方法时,授权服务器必须定义客户端标识(注册记录)和认证方案之间的映射。

  • 我正在学习使用ADFS进行基于SAML的声明验证。与其将用户带到IDP的登录页面,然后请求用户输入她的凭据,不如在SP中获取用户的凭据并将其传递给IDP以在那里进行验证,然后使用SAML令牌返回SP。 这纯粹是实验性的,我想尝试一下,以避免用户在IdP登录页面中输入凭据,而不是在SP中获取她的凭据从IdP验证。 谢谢

  • 我想将所有的用户信息存储到firebase中,并将电子邮件和密码传递给firebase auth,然后将图像插入到firebase,但在TaskSnapshot.getDownloadURL().ToString())中出现错误;我也不确定这是不是将用户电子邮件和密码传递给firebase auth的正确方法 public void AddUser(final String UserEmail,f

  • 我正在尝试在 Swift 中创建一个 iOS 应用程序,该应用程序使用 AWS Lambda 使用以下身份验证服务 - https://github.com/danilop/LambdAuth 它使用适用于 iOS 的 AWS 移动开发工具包与迪纳摩数据库和 Lambda 进行通信 - http://docs.aws.amazon.com/mobile/sdkforios/developergui