Spring Boot:OAuth终结点重定向到8443 [英] Spring Boot: OAuth endpoint redirects to 8443
本文介绍了Spring Boot:OAuth终结点重定向到8443的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在端口8080上有一个Web应用程序,它只使用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
,但无济于事。
有关如何禁用此无用重定向的建议?
推荐答案
根据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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文