"default" / Spring 安全中的 Ant 匹配器

Ant matcher to "default" / in Spring Security

如何将安全约束映射到默认页面?例如我正在使用 Keycloak,我希望我的应用程序在用户尝试实现我的应用程序时重定向到 Keycloak 的登录页面:localhost:8080/ <- 这必须重定向到 Keycloak 的登录页面。

我尝试了以下模式:

1. super.configure(http);
    http
            .csrf()
            .disable()
            .authorizeRequests()
            .antMatchers("/*").hasRole("ADMIN");

2.super.configure(http);
    http
            .csrf()
            .disable()
            .authorizeRequests()
            .antMatchers("/").hasRole("ADMIN");


3.super.configure(http);
    http
            .csrf()
            .disable()
            .authorizeRequests()
            .antMatchers("/**").hasRole("ADMIN");

4.super.configure(http);
    http
            .csrf()
            .disable()
            .authorizeRequests()
            .anyRequest().hasRole("ADMIN");


5.super.configure(http);
    http
            .csrf()
            .disable()
            .authorizeRequests()
            .antMatchers("*").hasRole("ADMIN");

但其中 none 有效...

顺便说一句,当我输入 sub-urls 例如localhost:8080/users,一切正常,出现 Keycloak 的登录页面。

这是 Spring 安全适配器的问题。解决方法是在请求根路径时将您的用户重定向到其他路径(例如 /home.html)。在 @Controller:

@RequestMapping("/")
protected String redirect() 
{
    return "redirect:/home.html";
}

另请参阅:

  • redirect in Spring MVC