java - 在 Spring Security 配置中,所有人都允许的 Url 不可访问并重定向到登录
问题描述
在下面的配置中,我认为我没有做错任何事情。我允许所有人的 URL 将我重定向到登录页面。具有角色 USER 的用户也存在同样的问题。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/**").hasRole("ADMIN")
.antMatchers("/new/**", "/edit/**", "/create/**", "/save/**").hasAnyRole("USER", "ADMIN")
.antMatchers("/", "/registration/**", "/view/**",).permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login").permitAll()
.defaultSuccessUrl("/")
.and()
.logout().invalidateHttpSession(true)
.clearAuthentication(true)
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/loggingOut").permitAll();
}
如果您可以提供任何有助于更好理解的资源。我是春天的新手,任何帮助将不胜感激。
解决方案
我认为问题出在您的角色层次结构上。试试这个。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/new/**", "/edit/**", "/create/**", "/save/**").hasAnyRole("USER", "ADMIN")
.antMatchers("/", "/registration/**", "/view/**",).permitAll()
.antMatchers("/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login").permitAll()
.defaultSuccessUrl("/")
.and()
.logout().invalidateHttpSession(true)
.clearAuthentication(true)
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/loggingOut").permitAll();
}
如果这不起作用,请尝试不同的组合。这篇文章解释了角色层次结构,它可以帮助你。
推荐阅读
- cisco - 组取件场景 - CALL_STATE.CS_CONNECTED 未被触发
- regex - 我可以匹配任意长度字符串中另一个字符正下方的字符吗?
- php - 谷歌 API 分析 PHP 每天
- r - 从两个向量创建列表,每个向量的每个组合
- vuejs2 - 如何在 Vuetify v-data-table 中将选择复选框列移动到末尾?
- reactjs - 如何从 Redux 状态添加和删除属性
- c - 十六进制格式的 printf 浮点数返回随机值
- bash - 如何确定是否为 BASH 子外壳定义了 EXIT 陷阱?
- stl - STL 中的优先级队列实现
- flutter - Flutter:如何左对齐复选框