繁体   English   中英

Spring Boot和OAuth2:通过反向代理重定向URL

[英]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
  1. 我要求url.com/application
  2. 我被重定向到基于表单的身份验证authserver.com/oath/authorize?client_id=username&redirect_url=url.com/login&...
  3. 通过身份验证后,我将重定向回url.com/login,该步骤在上一步中作为GET参数给出。 结果为404。

如何在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

我可以看到您正在通过将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM