首页 > 解决方案 > Spring Security 中基于角色的访问

问题描述

@CrossOrigin(origins = "*", maxAge = 3600)

@RestController
public class RbacAccess {

@GetMapping("/api/user")
@PreAuthorize("hasRole('ROLE_USER')")
public String userAccess() {
    return ">>> User Contents!";
}

@GetMapping("/api/admin")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public String adminAccess() {
    return ">>> Admin Contents";
}

}

我想保护我的方法

@PreAuthorize("hasRole('ROLE_USER')") 
public String userAccess() { 
    return ">>> User Contents!";
} 

在这里,我希望在用户登录后动态@PreAuthorize("hasRole('ROLE_USER')")显示@PreAuthorize("hasRole(DynamicRome )")角色是 ADMIN 还是 User。我不想硬编码它。

我的桌子是:

我想保护我的方法

@PreAuthorize("hasRole('ROLE_USER')") 
public String userAccess() { 
    return ">>> User Contents!"; 
}

在这里,我希望在用户登录后动态@PreAuthorize("hasRole('ROLE_USER')")显示@PreAuthorize("hasRole(DynamicRome )")角色是 ADMIN 还是 User。我不想硬编码它。谢谢。

标签: spring-bootspring-security

解决方案


推荐阅读