![](/img/trans.png)
[英]Spring boot oauth2 response with redirect url as underlying service instead of proxy (Gateway)
[英]Spring Boot and OAuth2: redirect url over reverse proxy
美好的一天。
我正在开发在特定端口下运行的spring-boot服务,比方说服务器中的主机名:8080。 用户通过apache代理url.com/application访问此服务
现在已配置apache:
ProxyRequests Off
ProxyPreserveHost Off
ProxyPass /application http://hostname:8080
ProxyPassReverse /application http://hostname:8080
我使用oauth2进行身份验证,并且身份验证发生在authserver.com中
Application.yml:
oauth2:
client:
clientId: username
clientSecret: 123123
accessTokenUri: http://authserver.com/oauth/token
userAuthorizationUri: http://authserver.com/oauth/authorize
clientAuthenticationScheme: form
resource:
userInfoUri: http://authserver.com/oauth/user
preferTokenInfo: false
如何在Spring Boot配置中修改redirect_url? 它应该是url.com/application/login。
pom.xml中:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-ws</artifactId>
<version>1.3.0.M2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependecies>
安全配置:
@EnableAutoConfiguration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public static class WebSecurityConfig extends OAuth2SsoConfiguration {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/health", "/status", "/ws", "/ws/*", "/mappings", "/beans")
.anonymous();
http.csrf().disable();
super.configure(http);
}
}
使用Spring Cloud Security 1.0.0.RC3
请按照此答案。 它解决了我的一天。
1.您必须在Apache代理上设置标头:
<VirtualHost *:443>
ServerName www.myapp.org
ProxyPass / http://127.0.0.1:8080/
RequestHeader set X-Forwarded-Proto https
RequestHeader set X-Forwarded-Port 443
ProxyPreserveHost On
... (SSL directives omitted for readability)
</VirtualHost>
2.您必须告诉Spring Boot应用程序使用这些标头。 因此,将以下行放在application.properties中(或Spring Boots可以理解属性的任何其他位置):
server.use-forward-headers=true
根据OAuth2规范:
3.1.2。 重定向端点
在完成与资源所有者的交互之后,授权服务器将资源所有者的用户代理定向回客户端。 授权服务器将用户代理重定向到客户端在注册过程中或发出授权请求时与授权服务器先前建立的客户端重定向端点。
重定向端点URI必须是[RFC3986] 4.3节所定义的绝对URI。 端点URI可以包括格式为“ application / x-www-form-urlencoded”(按附录B)的查询组件([RFC3986] 3.4节),在添加其他查询参数时必须保留该组件。 端点URI不得包含片段组件。
有2个选项如何处理重定向
我可以看到您正在通过将redirect_url传递到OAuth服务器authserver.com/oath/authorize?client_id=username&redirect_url=url.com/login
使用第二种方法
但是触发此重定向的是url.com/application
。 因此,您需要更改/配置url.com/application
以将请求重定向到authserver.com/oath/authorize?client_id=username&redirect_url=url.com/application/login
。
如果您在url.com/application
中使用spring security,请更改登录页面配置url。
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("authserver.com/oath/authorize?client_id=username&redirect_url=url.com/application/login")
.permitAll()
.and()
.logout()
.permitAll();
}
由于您没有显示您的应用程序代码,因此上述代码可能有效也可能无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.