【发布时间】:2017-06-28 00:26:09
【问题描述】:
在 Spring Security 中,什么时候添加 "ROLE_" 前缀比较合适?在使用@PreAuthorize("hasRole('ROLE_USER')") 的示例中,确实如此。但在这个例子中,它没有:
http
.httpBasic()
.and()
.authorizeRequests()
.antMatchers(HttpMethod.POST, "/books").hasRole("ADMIN")
接下来呢?
SecurityContext securityContext = new SecurityContextImpl();
final Properties users = new Properties();
users.put("joe","secret,ADMIN,enabled"); <-- here
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(users);
和
Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN")); <-- here
AnonymousAuthenticationToken anonymousAuthenticationToken = new AnonymousAuthenticationToken("test", manager.loadUserByUsername("joe"), grantedAuthorities);
securityContext.setAuthentication(anonymousAuthenticationToken);
SecurityContextHolder.setContext(securityContext);
有没有具体的使用规则?
【问题讨论】:
标签: java spring spring-security