带有反向代理后面的OAuth2的Spring Boot

时间:2018-11-29 10:29:55

标签: spring-boot spring-security reverse-proxy oauth2client

我是Spring Security的新手,并尝试使用主机名:8080下运行的OAuth2通过Google登录开发Spring Boot应用程序。该应用程序位于Apache反向代理服务器https://url.com后面。

Spring Boot版本2.1.0

Spring Security版本5.1.1

build.gradle:

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.security:spring-security-oauth2-client")
    implementation("org.springframework.security:spring-security-oauth2-jose")
}

application.yml:

oauth2:
  client:
    registration:
      google:
        clientId: <clientId>
        clientSecret: <clientSecret> 
        scope: profile, email, openid

server:
  use-forward-headers: true
  servlet:
    session:
      cookie:
        http-only: false

Spring Security配置:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf()
                .disable()
            .authorizeRequests()
                .anyRequest()
                    .authenticated()
            .and()
            .oauth2Login();
    }
  }
  1. 我请求https://url.com
  2. 重定向到https://accounts.google.com/signin/oauth/
  3. 通过身份验证后,重定向到 https://url.com/login/oauth2/code/google?state={state}&code={code}&scope=openid+email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.me&authuser=0&session_state={session_state}&prompt=none发生错误,超时:
  

[invalid_token_response]尝试执行错误时   检索POST上的OAuth 2.0访问令牌响应:I / O错误   请求“ https://www.googleapis.com/oauth2/v4/token”:连接   超时(连接超时);嵌套异常为   java.net.ConnectException:连接超时(连接超时)

此错误是由代理服务器设置或启动应用引起的吗?感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

已解决。我必须设置JVM参数:

https.proxyHost=[host]
https.proxyPort=[port] 

http.proxyHost=[host]
http.proxyPort=[port]