小编And*_*ndy的帖子

如何配置Spring Boot Security,以便仅允许用户更新自己的配置文件

我已经实现了基本的Spring Boot Security东西,以保护我的Web服务。我知道您只能向某些用户角色授予对某些服务的访问权限,但是也可以向特定用户授予访问权限(用户可以是动态的)吗?

假设我们有一个社交应用,每个用户都有自己的个人资料。使用以下REST服务,他们应该是唯一可以编辑配置文件的人:

@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
    }}
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能确保春季安全,只有用户自己才能更新其个人资料?任何其他用户都应被拒绝。有没有一种优雅的方法,如何配置这种行为?

我试图在我的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 …
Run Code Online (Sandbox Code Playgroud)

spring spring-security spring-boot

3
推荐指数
2
解决办法
682
查看次数

@OneToOne 映射与 Hibernate 共享主键用户 - 帐户

我试图让这个例子适用于我的情况。Primus 是用户,Secundus 是帐户。用户应与帐户共享主键。一切正常,直到我尝试级联坚持。:

User user = new User();
user.setName("Andy");
this.uDao.create(user);
Run Code Online (Sandbox Code Playgroud)

没问题,工作正常,但是......

User user = new User();
user.setName("Andy");
Account account = new Account();
account.setUsername("xyz");
user.setAccount(account);
this.uDao.create(user);
Run Code Online (Sandbox Code Playgroud)

给出错误:

01:14:35,844 INFO  [stdout] (ServerService Thread Pool -- 80) Hibernate: insert into user (name) values (?)

01:14:35,860 INFO  [stdout] (ServerService Thread Pool -- 80) Hibernate: insert into account (username, user_id) values (?, ?)

01:14:35,861 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (ServerService Thread Pool -- 80) SQL Error: 1452, SQLState: 23000
01:14:35,861 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (ServerService Thread Pool -- …
Run Code Online (Sandbox Code Playgroud)

java orm jpa

2
推荐指数
1
解决办法
5975
查看次数

标签 统计

java ×1

jpa ×1

orm ×1

spring ×1

spring-boot ×1

spring-security ×1