[英]okta oauth2 Spring security all protected pages redirect to login
我正在尝试在我的应用程序中使用 spring 安全性来实现 okta(custom-provider) oauth2 sso 功能。 应用程序成功提示用户登录并授权用户。 我的 spring 安全配置如下所示。 这里的问题是除了登录名和一些其他被赋予 permitAll() 的 url,所有受保护的页面都被重定向到下面给出的 application.yml 中配置的“redirect-uri”(即登录 url)。 我不明白为什么会发生这种情况,我也找不到 okta 文档中给出的任何解决方案。 我在日志中没有看到任何错误消息。 每次我访问受保护或受保护的页面时,重定向都会发生在 http://localhost:8080/login 这是重定向 uri 但我需要重定向到 go 到用户选择的相应页面。 我在这里错过了什么吗?
安全配置.java
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**").authorizeRequests()
.antMatchers("/","/login**","/vendor/**", "https://fonts.googleapis.com/**", "/css/**",
"https://www.thymeleaf.org", "/js/**").permitAll()
.anyRequest().authenticated()
.and()
.oauth2Login();
}
}
应用程序.yml
security:
oauth2:
client:
registration:
custom-client:
client-id:
client-secret:
scope: ["openid", "profile", "email", "address", "phone", "groups"]
provider: custom-provider
state: xoxoxo
redirect-uri: http://localhost:8080/login
client-authentication-method: basic
authorization-grant-type: authorization_code
filter-order: 3
provider:
custom-provider:
token-uri: https://did.oktapreview.com/oauth2/v1/token
authorization-uri: https://did.oktapreview.com/oauth2/v1/authorize
user-info-uri: https://did.oktapreview.com/oauth2/v1/authorize
user-name-attribute: name
有人可以吗
是的,当然,有一个错误的事情。
重定向uri:http://localhost:8080/login
redirect-uri 应该是指向您的 OAuth2.0 客户端的回调端点的链接
就 Spring Security OAuth2.0 框架而言,它应该是以下模式:
重定向 uri:“{baseUrl}/login/oauth2/code/{registrationId}”
其中“baseUrl”是您的 URL(http://localhost),“registrationId”是注册名称,在您的情况下是“custom-client”
实际上,您可以将没有特定 registrationId 和 baseUrl 的模式指定为“redirect-uri”参数,这样您的配置应该如下所示:
security:
oauth2:
client:
registration:
custom-client:
client-id:
client-secret:
scope: ["openid", "profile", "email", "address", "phone", "groups"]
provider: custom-provider
state: xoxoxo
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
client-authentication-method: basic
authorization-grant-type: authorization_code
filter-order: 3
provider:
custom-provider:
token-uri: https://did.oktapreview.com/oauth2/v1/token
authorization-uri: https://did.oktapreview.com/oauth2/v1/authorize
user-info-uri: https://did.oktapreview.com/oauth2/v1/authorize
user-name-attribute: name
另外,请注意您的凭据,应该填写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.