【问题标题】:Spring Boot: OAuth endpoint redirects to 8443Spring Boot:OAuth 端点重定向到 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,但没有帮助。

关于如何禁用这个无用的重定向的任何想法?

【问题讨论】:

    标签: spring spring-boot tomcat spring-security spring-security-oauth2


    【解决方案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()
            //...
            ;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-23
      • 2021-05-02
      • 2014-09-04
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      相关资源
      最近更新 更多