例如,在web应用程序中,我有一个用户模型:
class User{
String username;
String email;
String passowrd;
boolean active;
Set<Role> roles;
}目前支持以下操作:
1 guest can register(create a new user)
2 user can upate its info
3 user with role of admin can set the `active` and `roles`在服务器端,我们使用SpringMVC直接获取模型User:
@RequestMapping(value = "", method = RequestMethod.POST)
protected Result create(@Valid @RequestBody User user, BindingResult bindingResult) {
.....
}到目前为止,正常的工作流程很好,但考虑到有人(不是管理员用户)发送以下内容:
/user HTTP/Update
{
"username":"jk",
"active":true,
"roles":[{
id:"role_admin_id"
}]
}如果此请求集被接受,则用户jk将具有super_admin角色,这不是预期的角色。
你怎么保护它呢?
发布于 2016-02-27 01:44:46
首先,您发送的@RequestBody用户用户只是一个您想要更新的常规对象。这不是Spring Security用户。如果您想将用户定义为spring安全用户,则必须实现UserDetails。你已经有正确的spring安全设置了吗?我不知道您使用的是xml还是java配置。如果您使用的是java配置,您可以通过如下方式控制角色的访问权限:
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/resources/**", "/signup", "/about").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
.anyRequest().authenticated()
.and()
// ...
.formLogin();
}参考:http://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html#authorize-requests
https://stackoverflow.com/questions/35616378
复制相似问题