0

我正在尝试从 spring 安全应用程序中的 amazon cognito 获取我的自定义属性,而我正在为此苦苦挣扎

该应用程序已经通过 Jwt 进行身份验证,但我没有设法从登录用户那里获得更多信息......

有一些已知的方法可以从我的 JWT 或类似的东西中获取我的 cognito 自定义用户属性?

我的配置类看起来像这样:

@Override
public void configure(HttpSecurity http) throws Exception{
    http
            .authorizeRequests(expressionInterceptUrlRegistry -> expressionInterceptUrlRegistry.anyRequest().authenticated())
            .oauth2ResourceServer().jwt();
}

我试图在我的 OncePerRequest 过滤器中获取一些用户信息,这里:

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
    var principal = (JwtAuthenticationToken)SecurityContextHolder.getContext().getAuthentication();
    String clientId = null;
    String userId = null;
    if (principal != null) {
        [ADD SOME CODE TO GET COGNITO CUSTOM ATTRIBUTE]
        clientId = ((Jwt) principal.getPrincipal()).getClaim("client_id").toString();
        userId = principal.getName();
    }

谢谢

4

1 回答 1

0

自定义属性带有“custom:”前缀。所以你可以使用上面相同的代码,

clientId = ((Jwt) principal.getPrincipal()).getClaim("custom:xxx").toString();
于 2021-01-13T04:15:44.397 回答