Ant匹配器"默认" /在Spring Security中

时间:2018-06-05 11:24:10

标签: java spring spring-security keycloak

如何将安全约束映射到defult页面?例如我正在使用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");

但它们都不起作用......

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

1 个答案:

答案 0 :(得分:2)

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

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

另见: