【发布时间】:2017-08-07 18:13:48
【问题描述】:
为了保护我的 Web 服务,我已经实现了基本的 Spring Boot 安全性。我知道您可以仅向某些用户角色授予对某些服务的访问权限,但是否也可以向指定用户授予访问权限(用户可以是动态的)?
假设我们有一个社交应用,每个用户都有自己的个人资料。使用以下休息服务,他们应该是唯一能够编辑配置文件的人:
@RestController
public class UserController {
@RequestMapping(method = RequestMethod.PUT, path = "/user/{userId}", ...)
public UserDetails updateUserDetails(@PathVariable("userId") String userId) {
// code for updating the description for the specified user
}}
}
我如何使用 spring security 确保只有用户本身才能更新他的个人资料?任何其他用户都应该被拒绝。有没有一种优雅的方式,如何配置这种行为?
我试图在我的 WebSecurityConfig 中找到一种方法,但没有成功。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// configure authorization for urls
.authorizeRequests()
// grant access to all users for root path and /home
//.antMatchers("/", "/home").permitAll()
// here i would like to grant access in the way, that only the user is allowed to perform this request by calling url with his userId
.antMatchers(HttpMethod.PUT,"/user/<userId>").and().httpBasic();
}
什么是实现这种行为的好方法?
【问题讨论】:
标签: spring spring-boot spring-security