Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webflux Spring中OAuth2的身份验证_Spring_Spring Boot_Oauth_Spring Webflux_Okta - Fatal编程技术网

Webflux Spring中OAuth2的身份验证

Webflux Spring中OAuth2的身份验证,spring,spring-boot,oauth,spring-webflux,okta,Spring,Spring Boot,Oauth,Spring Webflux,Okta,我正在开发一个应用程序,其中我希望有基于角色的访问控制,不幸的是,我没有找到任何使用SpringWebFlux的好例子。 我的oauth2.client.provider是Okta 这是我的SecurityWebFilterChain: @Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { return http

我正在开发一个应用程序,其中我希望有基于角色的访问控制,不幸的是,我没有找到任何使用SpringWebFlux的好例子。 我的oauth2.client.provider是Okta

这是我的SecurityWebFilterChain:

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        return http
                .authorizeExchange()
                .pathMatchers("/*").permitAll()
                .pathMatchers("/admin").hasRole("admins");
}

在中,我发现我应该配置资源服务器。请给我一个如何做的提示。

您需要使用Spring Boot 2.1的里程碑版本才能工作。M3或更高应能达到此目的。为Spring Security 5.1 OIDC支持添加必要的依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-oauth2-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-oauth2-jose</artifactId>
</dependency>
重新启动你的应用程序,转到,你应该被重定向到Okta登录。输入有效凭据,您将在成功登录后重定向回您的应用程序

要限制基于角色的访问,您需要为用户创建组

创建角色管理员和角色用户组(用户添加组)并向其中添加用户。您可以使用您注册的帐户,或创建新用户(用户添加人员)。导航到API授权服务器,单击授权服务器选项卡并编辑默认服务器。单击索赔选项卡,然后添加索赔。将其命名为“组”或“角色”,并将其包含在ID令牌中。将值类型设置为“Groups”,并将筛选器设置为“.*”的正则表达式(以包含所有值)

然后,您应该能够使用以下内容:

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
    return http
            .authorizeExchange()
            .pathMatchers("/*").permitAll()
            .pathMatchers("/admin").hasAuthority("ROLE_ADMIN");
}

您还应该能够使用中提到的
@PreAuthorize

这篇文章应该更适合您:谢谢您的回答,但在这篇文章中是简单的配置,没有基于角色的访问控制。我已经做到了,但这不是基于角色的访问控制我更新了答案,加入了基于角色的访问控制。我以前试过。当我尝试加载“/admin”时,我将获得“需要身份验证”弹出窗口。我想被重定向到okta登录页面。您可能需要将
@EnableWebFluxSecurity
添加到您的配置类中。也可能是
和().oauth2Login()
。有关示例,请参见此类:
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
    return http
            .authorizeExchange()
            .pathMatchers("/*").permitAll()
            .pathMatchers("/admin").hasAuthority("ROLE_ADMIN");
}