本文介绍了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); } 问题是,当我转到localhost:8080时,它会将我重定向到localhost:8443/oauth2/authorization/oauth,因此,出于某种原因,端口8080被8443覆盖。 根据我对类似问题的理解,这是因为Tomcat试图将用户从普通端点(Http)重定向到启用了SSL的端点(HTTPS)。但是我的端点已经启用了SSL,所以我真的不明白为什么会发生这种情况。如果我转到任何其他端点,它都可以使用HTTPS和8080端口--所以只有受保护的端点才有问题。我尝试使用ConnectorCustomizers自定义TomcatServletWebServerFactory并将那里的重定向端口设置为8080,但无济于事。
有关如何禁用此无用重定向的建议?
推荐答案根据github/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
发布评论