Spring 引导:OAuth 端点重定向到 8443
Spring Boot: OAuth endpoint redirects to 8443
我在端口 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 安全保护,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 - it redirects me to https://localhost:8443/oauth2/authorization/oauth 所以,出于某种原因,端口 8080 被 8443 覆盖。
据我从类似问题中了解到,发生这种情况是因为 Tomcat 试图将用户从普通端点 (http) 重定向到支持 SSL 的端点 (https)。但是我的端点已经启用了 SSL,所以我真的不明白为什么会这样。如果我转到任何其他端点,它可以使用 https 和 8080 端口 - 因此只有受保护的端点有问题。
我尝试使用 ConnectorCustomizers
自定义 TomcatServletWebServerFactory
并将重定向端口设置为 8080
,但没有帮助。
关于如何禁用这个无用的重定向有什么想法吗?
根据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()
//...
;
}
我在端口 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 安全保护,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 - it redirects me to https://localhost:8443/oauth2/authorization/oauth 所以,出于某种原因,端口 8080 被 8443 覆盖。
据我从类似问题中了解到,发生这种情况是因为 Tomcat 试图将用户从普通端点 (http) 重定向到支持 SSL 的端点 (https)。但是我的端点已经启用了 SSL,所以我真的不明白为什么会这样。如果我转到任何其他端点,它可以使用 https 和 8080 端口 - 因此只有受保护的端点有问题。
我尝试使用 ConnectorCustomizers
自定义 TomcatServletWebServerFactory
并将重定向端口设置为 8080
,但没有帮助。
关于如何禁用这个无用的重定向有什么想法吗?
根据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()
//...
;
}