spring-boot - 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。我不想硬编码它。
我的桌子是:
- 用户:id,用户名,电子邮件,密码
- 角色:id ,role_name
- user_role: id,user_id,role_id
- 组:group_id,group_name
- 角色组:ID,角色ID,组ID
我想保护我的方法
@PreAuthorize("hasRole('ROLE_USER')")
public String userAccess() {
return ">>> User Contents!";
}
在这里,我希望在用户登录后动态@PreAuthorize("hasRole('ROLE_USER')")
显示@PreAuthorize("hasRole(DynamicRome )")
角色是 ADMIN 还是 User。我不想硬编码它。谢谢。
解决方案
推荐阅读
- javascript - 我如何使用 Promise.all 使用此参数执行多个请求?
- java - 在anyRequest之后无法配置antMatchers?如何解决
- clojure - 使用“Clojure”的基本计算器
- python - ImportError 的原因:No module named requests
- python - 更新了 Anaconda 但它坏了,所以卸载并再次安装但它不起作用?
- javascript - 如何在不使用 cookie 或 service worker 的情况下在每个请求中发送令牌?
- python - 从句子中删除第一个单词并返回剩余的字符串
- javascript - Promise 安排和语法
- dart - Dart:使用 mixin 将抽象方法的实现外包
- wordpress - 在所有设备上强制桌面视图 Wordpress