我玩了一下 spring-boot 安全性。我使用 mongodb、spring-boot-starter-data-rest、spring-boot-starter-security 和 spring-boot-starter-web。我使用了自动暴露存储库(如 REST 服务)的可能性。我有两个存储库用户和客户。用户存储库用于帐户。 像这样扩展 WebSecurityConfigurerAdapter 后:
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.authorizeRequests()
.antMatchers("/users/**")
.hasRole("ADMIN").antMatchers("/", "/customers")
.hasRole("USER")
.anyRequest().authenticated()
.and()
.formLogin().permitAll();
}
A 无法控制具有 ADMIN 角色的用户对/users/页面的访问。我在访问服务时获得了身份验证,但每个角色的每个用户都可以访问任何地方。还应该配置什么?
最佳答案
这听起来你需要看看 Method Security Expressions Spring安全的特点。特别是注解@PreAuthorize
/@PostAuthorize
。
关于java - 使用 spring-boot 对 Rest 服务进行访问控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39910658/