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

使用Cognoto IAM角色的endpoint上的Spring Security?

洪英豪
2023-03-14

我试图根据在OAuth2凭证中设置的角色来限制Spring boot服务上的特定endpoint。

这是endpoint

@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @GetMapping(produces = "application/json")
    public TestResponse get() {
        return new TestResponse("Admin API Response");
    }
}

然后使用SecurityConfiguration bean对其进行保护

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf()
            .and()
            .authorizeRequests()
            .antMatchers("/login", "/", "/home", "/logout", "/ping").permitAll()
            .antMatchers("/api/admin").hasRole("arn:aws:iam::xxxxxx:role/spring-sso-test-ADMIN")
            .antMatchers("/api/user").hasRole("arn:aws:iam::xxxxxx:role/spring-sso-test-USER")
            .and()
            .oauth2Login()
            .and()
            .logout()
            .logoutSuccessUrl("/logout");
    }
}

调试了委托人,可以在属性列表中看到正确的 IAM 角色 cognito:roles 列表

然而,当我点击endpoint时,我得到一个HTTP 403未授权。这意味着用户已经成功地通过了身份验证,但是Spring不能识别或理解属性或者如何映射它们?

我尝试使用@Secure注释,但这并没有改变任何事情。

@Secured("arn:aws:iam::xxxxxx:role/spring-sso-test-ADMIN")
@GetMapping(produces = "application/json")
public TestResponse get() {
        return new TestResponse("Admin API Response");
}

如何允许它使用 AWS Cognito 中定义的 IAM 角色工作?

共有1个答案

贺华容
2023-03-14

当您使用< code>hasRole DSL方法时,Spring Security会将< code>ROLE_前缀添加到您的权限中。因此,权限< code > arn:AWS:iam::XXXXXX:ROLE/spring-SSO-test-ADMIN 将变成< code > ROLE _ arn:AWS:iam::XXXXXX:ROLE/spring-SSO-test-ADMIN 。

您应该改用<code>hasAuthority<code>方法。

此外,您应该从<code>属性,并添加<code>权限,因为这是Spring Security将查询以获取权限的属性。

要映射权限,可以使用OAuth2UserService


@Bean
SecurityFilterChain app(HttpSecurity http) throws Exception {
    http
            .oauth2Login(oauth2 -> oauth2
                .userInfoEndpoint(userInfo -> userInfo
                    .oidcUserService(this.oidcUserService())
                    ...
                )
            );
    return http.build();
}

private OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() {
    // your custom implementation
}

更多细节请留档。

 类似资料:
  • 我创建了这个简单的控制器: 如您所见,它受的保护。 根据日志: 如您所见,用户已通过身份验证,并具有角色查看器。然而,由于“访问被拒绝”,它无法到达endpoint。 上一个日志片段: 主体:用户名;凭据:[受保护];身份验证:真;详细信息:空;授予权限:查看者 我的JWTFilter看起来像: 有什么想法吗?

  • 要检查多个角色是否具有方法级访问权限 我已使用@PreAuthorize annotation检查角色 @preAuthorize("hasRole(\""AuthoritiesContain. USER "\",)" ) 如何使用@PreAuthorize注释检查多个角色?

  • 我是阿帕奇骆驼队的新手。我试图将一个交换从java方法发送到一个路由,但它给了我“由:org.apache.camel.component.direct.DirectConsumerNotAvailableException:endpoint上没有可用的消费者”错误。我想知道这个错误到底是什么,我们什么时候得到这个?

  • 本文向大家介绍ansible 使用角色,包括了ansible 使用角色的使用技巧和注意事项,需要的朋友参考一下 示例 Ansible使用角色的概念来更好地允许模块化代码并避免重复自己。 角色只是Ansible知道从何处加载vars文件,任务和处理程序的文件夹结构。一个例子可能看起来像这样: 然后,您可以将该角色用于基本的剧本,如下所示: 针对此剧本运行Ansible时,它将针对组中的所有主机并针对

  • 我有一个自定义需求,我想根据特定角色决定是否可以访问API。我使用的是Spring框架。我想支持这样的观点: 其中表示一个角色<代码>|和

  • 我目前试图设置一个管理员角色,以便访问一个简单的管理页面,使用以下留档通过提供:连接角色 我已经有一段时间不停地用我的头来撞击它,但我仍然不知道如何设置角色,例如,现在我正在从数据库中提取一个管理值,并暂时将其存储在一个全局变量中,但我不知道如何将其用于连接角色,比如只允许特定用户访问我的管理页面。 如果我的文档没有帮助我确保仅当用户是管理员时才能访问网页,那么有人可以澄清或展示如何执行此操作的示