如何防止客户端更新敏感字段

时间:2016-02-25 01:07:49

标签: spring-mvc authentication shiro

例如,在网络应用程序中,我有一个用户模型:

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的角色,这不是预期的。

你如何保护它?

1 个答案:

答案 0 :(得分:0)

首先,您发送的@RequestBody用户用户只是您想要更新的常规对象。它不是Spring Security用户。如果要在弹簧安全用户中定义用户,则可以使用&#39;我必须实现UserDetails。您是否已正确安装弹簧安全装置?我不知道你是否使用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

相关问题