Spring 5 Google OAuth2需要https重定向URL,而不是http

jjhzyzn0  于 12个月前  发布在  Spring
关注(0)|答案(2)|浏览(102)

我有一个托管网站与Apache httpd(端口80和443)和Wildfly 15(端口8080)。我的Web服务器设置为处理http和https,并设置为使用这些命令与Wildfly通信

ProxyPass        / http://myhost.com:8080/
ProxyPassReverse / http://myhost.com:8080/

我有一个Java 11 Spring 5应用程序作为WAR文件部署到Wildfly。它设置为使用Google OAuth,以便用户可以在我的应用程序上进行身份验证。我正在尝试使用一些需要特殊“范围”的YouTube API,这意味着在我的“授权重定向URI”列表中只有https。但是,我现在需要http://myhost.com:8080/login/oauth2/code/google在该列表中包含“www.example.com“,以便用户成功登录,这违反了YouTube API的要求。
我理解为什么URI中有“http”和“8080”,因为请求来自我在Wildfly中部署的应用程序。但是我需要把它改成“https://myhost.com/login/oauth2/code/google“。
我该怎么做呢?
下面是一些进一步的配置,但它们似乎做得不够。

@PropertySource("classpath:application.properties")
@Configuration
@EnableWebSecurity
public class SecurityConfiguration
{
  @Value("${spring.security.oauth2.client.registration.google.redirect-uri}")
  private String             redirectUri;

  @Bean
  public SecurityFilterChain filterChain(
    HttpSecurity http) throws Exception
  {
    http.cors().and().csrf().disable()//

    .authorizeRequests()//
    .antMatchers("/secure/**").authenticated()//
    .antMatchers("/api/**", "/login/**").permitAll()// for redirectUri
    .antMatchers("/**").permitAll()//
    .antMatchers("/logout").permitAll()//

    .and()//
    .sessionManagement()//
    .sessionCreationPolicy(SessionCreationPolicy.ALWAYS)

    .and()//
    .userDetailsService(userDetailsManager())//
    .oauth2Login()// because we need to authorize our users, not just be a web client
    //.oauth2Client()// not this
    .redirectionEndpoint().baseUri(this.redirectUri)//
    .and()//
     .clientRegistrationRepository(this.securityService.getClientRegistrationRepository())//
    .authorizedClientService(this.securityService.getAuthorizedClientService())//
    .loginPage(SecurityController.LOGIN_PAGE_MAPPING)//
    .defaultSuccessUrl(loginSuccessUrl)//
    //.successHandler(this.loginSuccessHandler)//
    .failureUrl("/login-failure-page")//

    .and()//
    .logout()//
    .clearAuthentication(true)//
    .invalidateHttpSession(true)//
    .deleteCookies("JSESSIONID")//
    .logoutSuccessUrl(SecurityController.LOGOUT_PAGE_MAPPING).permitAll()

    // Gotta get back to the correct port after login.
    .and()//
    .requestCache().requestCache(requestCache()); // port mapper here

    // For Firefox and h2-console
    http.headers().frameOptions().disable();

    return http.build();
  }
}

网址:application.properties

spring.security.oauth2.client.registration.google.redirect-uri=https://myhost.com/login/oauth2/code/google
spring.security.oauth2.client.registration.google.redirectUri=https://myhost.com/login/oauth2/code/google
spring.security.oauth2.client.registration.google.preEstablishedRedirectUri=https://myhost.com/login/oauth2/code/google
spring.security.oauth2.client.use-current-uri=false

我也为CORS定义了这个

@Bean
public WebMvcConfigurer corsConfigurer()
{
  return new WebMvcConfigurer()
  {
    @Override
    public void addCorsMappings(
      CorsRegistry registry)
    {
      registry.addMapping("/**")//
        .allowedOrigins(/*"http://myhost.com:8080",*/ "https://myhost.com",
            "https://myhost.com:8080")//
        .allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH");
    }
  };
}
ikfrs5lh

ikfrs5lh1#

您可以通过使用server.ssl属性为Sping Boot 应用程序提供SSL服务。
如果您还没有myhost.com的证书(例如在您的开发环境中),您可能会发现我托管的there对于生成自签名SSL证书并将其添加到JDK/JRE cacerts文件很有用。您还可以找到将此证书添加到操作系统受信任根证书的说明。
如果您正在使用servlet应用程序,我建议您在使用https时使用any port but 8080

评论跟进

上面的选项允许服务https://myhost.com:8080而不是http://myhost.com:8080(但是如上所述,由于历史原因,在servlet应用程序中使用https://myhost.com:8443会更容易)。
如果您不希望Spring应用的流量受到保护,并且希望保留http://myhost.com:8080,则必须将授权代码重定向URI配置为https://myhost.com/login/oauth2/code/google,就像您在属性中尝试做的那样。问题是,由于你只提供了你的conf的一部分,我们无法发现为什么这不起作用。

简单解决方案

您暴露的配置非常混乱和不安全(例如,具有OAuth2登录的应用程序会暴露于CSRF攻击,因此禁用CSRF保护是一个很大的错误)。有了我写的这个启动器,你的conf可以像这样简单:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>

<dependency>
    <groupId>com.c4-soft.springaddons</groupId>
    <artifactId>spring-addons-starter-oidc</artifactId>
    <version>7.1.8</version>
</dependency>
@Configuration
@EnableMethodSecurity
public class SecurityConfig {
}
oauth2-redirect-socket: https://myhost.com
oauth2-issuer: https://accounts.google.com
oauth2-client-id: change-me.apps.googleusercontent.com
oauth2-client-secret: change-me

spring:
  security:
    oauth2:
      client:
        provider:
          oauth2:
            issuer-uri: ${oauth2-issuer}
        registration:
          authorization-code:
            authorization-grant-type: authorization_code
            client-id: ${oauth2-client-id}
            client-secret: ${oauth2-client-secret}
            provider: oauth2
            scope:
            - openid
            - profile
            - email
            - offline_access

com:
  c4-soft:
    springaddons:
      oidc:
        ops:
        - iss: ${oauth2-issuer}
          # skipping issuer configuration which is not used in your case
        client:
          # this forces to build redirect URIs pointing to the SSL gateway
          client-uri: ${oauth2-redirect-socket}
          security-matchers:
          - /**
          permit-all:
          # what is not listed below requires requests to be authorized
          - /login/**
          - /oauth2/**
          - /
          cors:
          - path: "/**"
            allowed-origin-patterns:
            - ${oauth2-redirect-socket}

如果你不想使用“我的”启动器,请参考README的这一段。它包含了一些教程的链接,这些教程可以帮助你自己编写安全配置文件。

utugiqy6

utugiqy62#

@ch4mp给了我所需要的信息,但我要把我必须做的事情放在这里,让它工作。首先,我没有考虑过让Wildfly安全,但你可以,我做到了。我让我的托管公司从我的Apache服务器安装证书到Wildfly。
然后我修改了我的Apache配置文件来使用这个:

ProxyPass        / https://hostname.com:8443/
ProxyPassReverse / https://hostname.com:8443/

但另一个变化是增加了以下内容,因为没有它,什么都不起作用。

SSLProxyEngine on

我仍然有OAuth2安全配置要清理,但ch4mp给了我很多工作。我认为这是让Oauth2重定向URI使用https而不是http的第二个问题。

相关问题