java - Spring Security 中 Ant 匹配器到 "default"/

标签 java spring spring-security keycloak

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

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

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

最佳答案

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

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

另请参阅:

关于java - Spring Security 中 Ant 匹配器到 "default"/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50698893/

相关文章:

java - "this"java 查询点

java - StreamTokenizer 中可能为 'look ahead' 吗?

java - 服务的任何公共(public)方法的 AOP 切入点表达式

java - 如何遍历每一行,计算差异然后对所有内容进行平均?

Spring Security 与 XACML 集成(或任何其他基于策略的解决方案)

java - 使用 jdbcAuthentication 时 Spring Security 登录不工作

java - 修改表内 libGDX 堆栈收集的 actor 的维度属性

java - 初始化二维数组

spring - HazelCast IMap.values() 在 Tomcat 上给出 OutofMemory

java - Spring安全认证: migration from xml to annotations