Spring Boot:OAuth 端点重定向到 8443
Posted
技术标签:
【中文标题】Spring Boot:OAuth 端点重定向到 8443【英文标题】:Spring Boot: OAuth endpoint redirects to 8443 【发布时间】:2020-06-28 18:46:21 【问题描述】:我有一个在端口 8080 上仅使用 SSL(不允许 http)运行的网络应用:
server:
ssl:
key-store-type: PKCS12
key-store: file:$SERVER_KEYSTORE_PATH
key-store-password: $SERVER_CERT_PASSWORD
port: 8080
当我启动应用程序时,我在日志中看到:
2020-03-17 17:32:29.836 INFO 90960 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (https)
我的一个端点(实际上是根端点)受 Spring Security OAuth2 的保护:
@Override
protected void configure(HttpSecurity http) throws Exception
String baseURI = redirectURI.substring(redirectURI.lastIndexOf("/"));
http.csrf().disable()
.authorizeRequests()
.antMatchers("/").authenticated()
.antMatchers("/**").permitAll()
.and()
.oauth2Login()
.defaultSuccessUrl("/")
.redirectionEndpoint().baseUri(baseURI);
问题是当我转到https://localhost:8080 时,它会将我重定向到https://localhost:8443/oauth2/authorization/oauth 所以,出于某种原因,端口 8080 被 8443 覆盖。
据我从类似问题中了解到,这是因为 Tomcat 试图将用户从普通端点 (http) 重定向到启用 SSL 的端点 (https)。但是我的端点已经启用了 SSL,所以我真的不明白为什么会这样。如果我去任何其他端点,它适用于 https 和 8080 端口 - 所以只有受保护的端点是有问题的。
我尝试使用ConnectorCustomizers
自定义TomcatServletWebServerFactory
并将重定向端口设置为8080
,但没有帮助。
关于如何禁用这个无用的重定向的任何想法?
【问题讨论】:
【参考方案1】:根据https://github.com/spring-projects/spring-security/issues/8140#issuecomment-600980028:
@Override
protected void configure(HttpSecurity http) throws Exception
PortMapperImpl portMapper = new PortMapperImpl();
portMapper.setPortMappings(Collections.singletonMap("8080","8080"));
PortResolverImpl portResolver = new PortResolverImpl();
portResolver.setPortMapper(portMapper);
LoginUrlAuthenticationEntryPoint entryPoint = new LoginUrlAuthenticationEntryPoint(
"/login");
entryPoint.setPortMapper(portMapper);
entryPoint.setPortResolver(portResolver);
http
.exceptionHandling()
.authenticationEntryPoint(entryPoint)
.and()
//...
;
【讨论】:
以上是关于Spring Boot:OAuth 端点重定向到 8443的主要内容,如果未能解决你的问题,请参考以下文章
Spring Security OAuth2 始终重定向到具有有效 Bearer 标头的 /login 页面
使用 Twitch 进行身份验证时的 Spring-Boot OAuth2 奇怪行为
Spring boot,Security,OAuth2:是不是可以使用自定义 AuthorizationCodeResourceDetails?身份验证服务器需要重定向 url 中的特定参数
Spring Boot + Oauth 2单点登录ridirect uri错误行为