您需要使用 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 OIDC“Web”应用程序并将您的设置复制到 src/main/resources/application.yml。
spring:
security:
oauth2:
client:
provider:
okta:
issuer-uri: https://dev-737523.oktapreview.com/oauth2/default
registration:
login:
okta:
client-id: {clientId}
client-secret: {clientSecret}
scope: openid email profile
重新启动您的应用程序,转到http://localhost:8080,您应该会被重定向到 Okta 进行登录。输入有效的凭据,成功登录后您将被重定向回您的应用程序。
要根据角色限制访问权限,您需要为您的用户创建组。
创建一个 ROLE_ADMIN 和 ROLE_USER 组(Users > Groups > Add Group)并向其中添加用户。您可以使用您注册的帐户,也可以创建一个新用户(Users > Add Person)。导航到 API > Authorization Servers,点击 Authorization Servers 选项卡并编辑默认选项。点击声明标签和添加声明。将其命名为“组”或“角色”,并将其包含在 ID 令牌中。将值类型设置为“组”,并将过滤器设置为“.*”的正则表达式(以包括所有这些)。
那么你应该可以使用类似的东西:
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
return http
.authorizeExchange()
.pathMatchers("/*").permitAll()
.pathMatchers("/admin").hasAuthority("ROLE_ADMIN");
}
您还应该能够使用@PreAuthorize 中提到的this blog post。