首页 > 解决方案 > Springboot 2 Oauth2 无法重定向到 SSO 客户端

问题描述

我目前正在研究 Springboot 2.x oauth2 的实现。但是我遇到了一些棘手的问题。

该项目包括 auth-server 和 sso-client(底部提供 GitHub 链接)。问题是:当我输入一个受保护的 URL(例如 localhost:9000/)时,它将被重定向到在 auth-server 中配置的登录页面。但是,成功登录后它不会重定向回 sso-client。

auth-server 的授权服务器配置:

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    private AuthenticationManager authenticationManager;

    public AuthorizationServerConfig(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        this.authenticationManager = authenticationConfiguration.getAuthenticationManager();
}

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        super.configure(security);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
            .withClient("client")
            .secret("secret")
            .authorizedGrantTypes("authorization_code")
            .scopes("all")
            .autoApprove(true);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.authenticationManager(authenticationManager);
    }
}

auth-server 的安全配置:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("root")
            .password(passwordEncoder().encode("root"))
            .roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/**")
            .authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .and()
            .csrf().disable();
    }
}

sso 客户端的安全配置:

@Configuration
@EnableOAuth2Sso
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/**")
            .authorizeRequests()
            .anyRequest().authenticated();
    }
}

sso 客户端的 application.yml:

auth-server: http://localhost:9090

server:
  port: 9000

security:
  oauth2:
    client:
      client-id: client
      client-secret: secret
      scope: all
      user-authorization-uri: ${auth-server}/oauth/authorize
      access-token-uri: ${auth-server}/oauth/token
    resource:
      token-info-uri: ${auth-server}/oauth/check_token
      preferTokenInfo: false

这是该项目的链接:https ://github.com/paul8263/SpringBoot2Oauth2

PS:我使它与 Spring Boot 1.5.8 一起工作:https ://github.com/paul8263/SsoDemo2

我将代码与 Springboot2(第一个链接)进行了比较,但我几乎没有注意到任何明显的区别。

有人可以通过使简单的演示工作来帮助我解决这个问题吗?非常感谢。

标签: springspring-bootspring-securityoauthspring-security-oauth2

解决方案


推荐阅读