@Secured

判断是否具有角色,另外需要注意的是这里匹配的字符串是角色 ,需要带有前缀ROLE_

用法示例:

  1. 使用@EnableGlobalMethodSecurity(securedEnabled=true)开启注解功能 ```java @SpringBootApplication @MapperScan @EnableGlobalMethodSecurity(securedEnabled = true) // 开启secured注解 public class SpringSecurity01Application {

    public static void main(String[] args) {

    1. SpringApplication.run(SpringSecurity01Application.class, args);

    }

}

  1. 2.
  2. 在对应的controller上使用@Secured注解配置需要的角色
  3. ```java
  4. @GetMapping("/secured")
  5. @Secured({"ROLE_admin", "ROLE_test"}) // 配置的是角色名,需要带有 ROLE_ 前缀
  6. public String testSecured() {
  7. return "hello secured";
  8. }

@PreAuthorize

进入方法前的权限验证,可以将登陆用户的 authority/roles/permissions 参数传到方法中。

用法示例:

  1. 使用@EnableGlobalMethodSecurity(prePostEnabled = true)开启注解功能 ```java @SpringBootApplication @MapperScan @EnableGlobalMethodSecurity(prePostEnabled = true) // 开启prePost相关注解 public class SpringSecurity01Application {

    public static void main(String[] args) {

    1. SpringApplication.run(SpringSecurity01Application.class, args);

    }

}

  1. 2.
  2. 在对应的controller上使用该注解
  3. ```java
  4. @GetMapping("/preAuthorize")
  5. @PreAuthorize("hasRole('ROLE_admin')") // value为 spEL 表达式。可以使用的函数有:hasRole/hasAnyRole/hasAuthority/hasAnyAuthority/hasPermission/hasPermisson等
  6. public String testPreAuthorize() {
  7. return "hello preAuthorize";
  8. }

@PostAuthorize

在方法执行后再进行权限验证,适合验证带有返回值的权限。

用法示例:

  1. 使用@EnableGlobalMethodSecurity(prePostEnabled = true) 开启注解功能

  2. 在对应的controller上使用该注解

    1. @GetMapping("/postAuthorize")
    2. @PostAuthorize("hasRole('ROLE_test')") // 在方法执行之后才会校验权限。所以即使用户没有权限访问页面,该方法也会被执行。
    3. public String testPostAuthorized() {
    4. System.out.println("--------方法执行了");
    5. return "test postAuthorize";
    6. }

@PostFilter

权限验证之后对数据进行过滤。

用法示例:

  1. @GetMapping("/getAllUser")
  2. @PostFilter("filterObject.username == 'test11'")
  3. public List<UserInfo> getAllUser() {
  4. List<UserInfo> userInfoList = new ArrayList<>();
  5. userInfoList.add(new UserInfo(1, "test11", "123"));
  6. userInfoList.add(new UserInfo(2, "test12", "456"));
  7. userInfoList.add(new UserInfo(3, "test13", "789"));
  8. return userInfoList;
  9. }

页面请求 /getAllUser 时,只会返回 test11这条信息。

@PreFilter

进入控制器之前对数据进行过滤。

用法示例:

  1. @PostMapping("/addUser")
  2. @PreFilter("filterObject.userid % 2 == 0")
  3. public List<UserInfo> addUser(@RequestBody List<UserInfo> list) {
  4. list.forEach(userInfo -> System.out.println(userInfo));
  5. return list;
  6. }

当页面请求传入的报文为:

  1. [
  2. {
  3. "userid":1,
  4. "username":"test11",
  5. "password":"123"
  6. },
  7. {
  8. "userid":2,
  9. "username":"test12",
  10. "password":"456"
  11. },
  12. {
  13. "userid":3,
  14. "username":"test12",
  15. "password":"456"
  16. },
  17. {
  18. "userid":4,
  19. "username":"test12",
  20. "password":"456"
  21. }
  22. ]

后台controller的参数中只接收到 userid为2、4的信息:

  1. [
  2. {
  3. "userid": 2,
  4. "username": "test12",
  5. "password": "456"
  6. },
  7. {
  8. "userid": 4,
  9. "username": "test12",
  10. "password": "456"
  11. }
  12. ]