【发布时间】:2021-06-28 09:32:02
【问题描述】:
我在我的 UI 应用程序中通过 Oauth2 代码授权类型启用了 Spring Security 5。
UI 应用程序的基本或上下文 uri 是“/”,重定向 URI 是“BASE_URI/welcome/”
当我将重定向 URI 模板配置为“https://:/welcome/login/oauth2/code/myAuthProvider”时 它作为无效的重定向 URI 给出错误。
出现此错误是因为 Spring Security 正在尝试查找“/welcome/login/oauth2/code/myAuthProvider”而不是“/login/oauth2/code/myAuthProvider”
以下文档建议如何更改默认重定向 uri。但是,我需要解决方案来告诉 Spring Security 忽略重定向端点中的“/welcome/”。如果我的理解不正确,请提出任何方法或指导我。
application.yml
spring:
application:
name: My Client Application
main:
allow-bean-definition-overriding: true
security:
oauth2:
client:
provider:
myAuthProvider:
token-uri: https://someserver.com/as/token.oauth2
authorization-uri: https://someserver.com/as/authorization.oauth2
registration:
myAuthProvider:
client-name: myAuthProvider
client-id: ABCID
client-secret: XYZSECRET
client-authentication-method: basic
authorization-grant-type: authorization_code
redirect-uri: https://localhost:8080/welcome/login/oauth2/code/myAuthProvider
WebClient 为
@Configuration
public class WebClientConfig {
@Bean
WebClient authProviderWebClient(ClientRegistrationRepository clientRegistrations,
OAuth2AuthorizedClientRepository authorizedClients) {
var oauth = new ServletOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations,authorizedClients);
oauth.setDefaultOAuth2AuthorizedClient(true);
oauth.setDefaultClientRegistrationId("myAuthProvider");
return WebClient.builder()
.apply(oauth.oauth2Configuration())
.build();
}
}
WebSecurityConfig 为
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.oauth2Login();
}
}
控制器作为
@控制器 @RequestMapping("/欢迎") 公共类 WelcomeController {
private static final String WELCOME_PAGE = "welcome";
@GetMapping("/")
public String homePage() {
....
return WELCOME_PAGE;
}
}
【问题讨论】:
-
请向我们展示您的配置 yml/properties 文件和 WebSecurityConfigurerAdapter 实现。
-
@Vladimir 按要求提供了代码
标签: java spring-boot spring-security oauth-2.0 spring-security-oauth2