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