【问题标题】:Keycloak spring-security Adapter: antMatchers("/**").authenticated() does not start authentication processKeycloak spring-security Adapter: antMatchers("/**").authenticated() 没有启动认证过程
【发布时间】:2020-02-27 16:12:12
【问题描述】:

我使用的是 spring-security 5.2,在配置中我写了以下http.csrf().disable().authorizeRequests() .antMatchers("/login*", "/js/**", "/css/**", "/vendors/**", "/images/**").permitAll() .antMatchers("/**").authenticated();

应用程序 url 类似于http://localhost:8080/myApp 问题是当我调用应用程序 url 时,不会调用身份验证入口点。

仅当我将最后一行修改为 .antMatchers("/main*").authenticated();然后拨打http://localhost:8080/myApp/main一切正常。

我正在使用 Keycloak Spring Security Adapter 配置 Keycloak SSO 服务器 (7.0.1) +。所以我想知道,为什么在 .antMatchers("/**").authenticated() 的情况下没有调用 Keycloak 入口点,并且只有在我写了类似 .antMatchers("/main*").authenticated( )。

谁能解释我的原始代码有什么问题?

谢谢

【问题讨论】:

标签: spring-security


【解决方案1】:

您的 spring 安全配置类扩展了 KeycloakWebSecurityConfigurerAdapter 并且您调用了 super.configure(http)?如果是,那么您的登录页面不是 /login 而是 /sso/login。并且注销成功页面映射到/。当一个登出成功页面被映射时,Spring 会自动为其设置安全性为 permitAll。因此,您可以尝试将注销成功页面更改为另一个 url,以检查它是否可以解决您的问题 类似的东西

    http.csrf().disable().authorizeRequests()
    .antMatchers("/js/**", "/css/**", "/vendors/**", "/images/**").permitAll()
    .antMatchers("/**").authenticated()
    .anyRequest().permitAll()
    .and().logout().logoutSuccessUrl("/loggedout");

【讨论】:

  • 谢谢,但在我的情况下,我没有使用 formLogin,因为我正在使用 Keycloak Spring Security Adapter 配置 Keycloak SSO 服务器。我想知道,为什么在 .antMatchers("/**").authenticated() 的情况下不调用 Keycloak 入口点,并且只有在我编写类似 .antMatchers("/main*").authenticated() 的情况下才会调用它.但问题似乎出在 .antMatchers("/login*",...).permitAll() 部分。如果 .antMatchers("/**").authenticated() 重定向到 /login 并且我的代码声明 /login 具有访问权限 permitAll,那么不调用身份验证入口点是合乎逻辑的
  • 您的 spring 安全配置类扩展了 KeycloakWebSecurityConfigurerAdapter 并且您调用了 super.configure(http)?如果是,那么您的登录页面不是 /login 而是 /sso/login。并且注销成功页面映射到/。当一个登出成功页面被映射时,Spring 会自动为其设置安全性为 permitAll。因此,您可以尝试将注销成功页面更改为另一个 url,以检查它是否可以解决您的问题
  • 谢谢,更改登出成功网址确实解决了问题
猜你喜欢
  • 2013-12-30
  • 2020-08-04
  • 2019-11-20
  • 2017-03-22
  • 2021-10-25
  • 2019-09-27
  • 1970-01-01
  • 2018-10-28
  • 2016-11-26
相关资源
最近更新 更多