【问题标题】:Oauth2 security configuration antmatchers request filtering not working as expectedOauth2 安全配置匹配器请求过滤未按预期工作
【发布时间】:2022-01-14 13:00:10
【问题描述】:

我正在与 spring security oauth2 一起开发一个简单的 spring boot 项目,以对指定的端点使用 google 身份验证,即/google/login

通过以下安全配置,一切正常。

@Configuration
public class SecurityConfigure extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .requestMatchers().antMatchers("/**")
                .and()
                .authorizeRequests().antMatchers("/ldap/login").permitAll()
                .anyRequest().fullyAuthenticated()
                .and()
                .oauth2Login();
    }
}

但我只需要指定/google/login 端点即可使用 oauth2 进行身份验证。因此我这样指定。

@Configuration
public class SecurityConfigure extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .requestMatchers().antMatchers("/google/**")
                .and()
                .authorizeRequests().antMatchers("/ldap/**").permitAll()
                .anyRequest().fullyAuthenticated()
                .and()
                .oauth2Login();
    }
}

使用此安全配置 http://localhost:8080/google/login 端点调用重定向到另一个名为 http://localhost:8081/oauth2/authorization/google 的端点,这是我尚未定义的。

请帮助我解决这个问题。谢谢。

【问题讨论】:

  • 这看起来是意料之中的。一旦配置了 spring-security-oauth2,它就会自动创建几个端点,比如一个用于发送授权请求,一个是使用 oauth 代码。
  • 登录请求一来,它就会重定向到授权端点,通常是/oauth2/authorization/{client}。在您的情况下,它是 /oauth2/authorization/google 假设 google 在您的应用程序配置中被配置为客户端。类似地,将有一个像 /oauth2/code/{client} 这样的端点来使用身份验证代码。请在使用前阅读文档。
  • @Akash 如果可以的话,请您建议一种方法来做到这一点。我只需要使用 oauth2 验证这个“google/login”端点
  • 假设你的应用中只有一个安全配置,你可以试试 http.authorizeRequests().antMatchers("/google/login/**").authenticated().anyRequest().permitAll ().and().oauth2Login();它将允许所有未经身份验证的请求,除了 /google/login 将重定向到配置的谷歌登录。

标签: spring-boot spring-security-oauth2


【解决方案1】:

此配置对我有用。我必须允许在 Google 的身份验证过程运行时重定向的所有端点。

@Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .csrf().disable()
                    .requestMatchers().antMatchers("/google/**","/oauth2/authorization/google","/login/oauth2/code/google")
                    .and()
                    .authorizeRequests().antMatchers("/ldap/**").permitAll()
                    .anyRequest().fullyAuthenticated()
                    .and()
                    .oauth2Login();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2018-03-02
    • 1970-01-01
    • 2014-05-15
    相关资源
    最近更新 更多