我正在尝试将 Spring Security 应用到我的项目中,它封装了登录名,对数据库的访问是正确的,但是当我进入我的项目时,它允许我访问所有的 URL,而当我真正指定的时候可以访问的和不能访问的,但它允许我访问所有这些。
此外,当我尝试指定具有不同权限的不同用户组时,它会产生
StackOverflow
错误并且不允许我访问任何特定站点。
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests((authz) -> authz
.requestMatchers(staticResources).permitAll()
)
.authorizeHttpRequests((requests) -> requests
.requestMatchers("/person").hasAnyAuthority("recepcio", "neteja", "staff")
.requestMatchers("/**").hasAnyAuthority("staff")
.anyRequest().authenticated()
)
.formLogin((form) -> form
.loginPage("/login")
.permitAll()
)
.exceptionHandling((exception) -> exception
.accessDeniedPage("/errors/error403")
)
.build();
}
我希望能够为一组用户组分配某些 URL 的权限,以限制彼此可以做什么,我没有分配任何类型的权限,如果我只放一行,它允许我访问任何地方:
.requestMatchers("/person").hasAnyAuthority("recepcio", "neteja", "staff")
如果我把这两行放在顶部,它会创建一个循环并且不允许我通过登录:
.requestMatchers("/person").hasAnyAuthority("recepcio", "neteja", "staff")
.requestMatchers("/**").hasAnyAuthority("staff")
作为一名大三学生,我不是很擅长寻找信息,我将不胜感激。
修改建议应用。谢谢你的推荐,@dur!