spring-boot - 在反向代理后面使用 OAuth2 的 Spring Boot
问题描述
我是 Spring Security 的新手,并尝试使用在主机名:8080 下运行的 OAuth2 开发带有 Google 登录的 Spring Boot 应用程序。这个应用程序位于 Apache 反向代理服务器https://url.com后面。
Spring Boot 2.1.0 版
Spring Security 5.1.1 版
构建.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")
}
应用程序.yml:
oauth2:
client:
registration:
google:
clientId: <clientId>
clientSecret: <clientSecret>
scope: profile, email, openid
server:
use-forward-headers: true
servlet:
session:
cookie:
http-only: false
弹簧安全配置:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.oauth2Login();
}
}
- 我请求https://url.com
- 被重定向到https://accounts.google.com/signin/oauth/
- 经过身份验证后,将重定向回 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] 尝试检索 OAuth 2.0 访问令牌响应时出错:“ https://www.googleapis.com/oauth2/v4/token ”的 POST 请求出现 I/O 错误:连接超时(连接超时); 嵌套异常是 java.net.ConnectException: Connection timed out (Connection timed out)
此错误是由代理服务器设置或启动应用程序引起的吗?感谢帮助。
解决方案
解决了。我必须设置 JVM 参数:
https.proxyHost=[host]
https.proxyPort=[port]
http.proxyHost=[host]
http.proxyPort=[port]
推荐阅读
- android - 模拟器有问题,因为启动活动需要很长时间
- javascript - '在发送到客户端后无法设置标题' - 我正在设置标题,但我找不到在哪里
- flutter - 我需要在标签中添加一些东西我在哪里添加代码
- cmake - 如何使用 HTTPS URL 请求 GIT_REPOSITORY 的密码
- angular - 我如何知道项目中使用了哪个版本的 Angular?
- javascript - 使用 React 更改 onClick() 时的颜色
- typescript - Angular - 修改订阅内的对象
- html - Angular Tile 布局(使用 Flex 的间隙)
- visual-c++ - 如何在 DirectX12 中获取适配器驱动程序版本?
- mysql - 从头开始在 nodejs/react 中实现 ABAC