首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止客户端更新敏感字段

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

Stack Overflow用户
提问于 2016-02-25 09:07:49
回答 1查看 26关注 0票数 1

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

代码语言:javascript
复制
class User{
    String username;
    String email;
    String passowrd;


    boolean active;
    Set<Role> roles;
}

目前支持以下操作:

代码语言:javascript
复制
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

代码语言:javascript
复制
@RequestMapping(value = "", method = RequestMethod.POST)
protected Result create(@Valid @RequestBody User user, BindingResult bindingResult) {
    .....
}

到目前为止,正常的工作流程很好,但考虑到有人(不是管理员用户)发送以下内容:

代码语言:javascript
复制
/user  HTTP/Update

{
    "username":"jk",
    "active":true,
    "roles":[{
        id:"role_admin_id"
    }]
}

如果此请求集被接受,则用户jk将具有super_admin角色,这不是预期的角色。

你怎么保护它呢?

EN

回答 1

Stack Overflow用户

发布于 2016-02-27 01:44:46

首先,您发送的@RequestBody用户用户只是一个您想要更新的常规对象。这不是Spring Security用户。如果您想将用户定义为spring安全用户,则必须实现UserDetails。你已经有正确的spring安全设置了吗?我不知道您使用的是xml还是java配置。如果您使用的是java配置,您可以通过如下方式控制角色的访问权限:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35616378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档