桂林做网站建设的公司,天津房产网,太原论坛2021,网站开发的代码注解使用
Secured
判断用户是否具有角色#xff0c;可以访问方法#xff0c;另外需要注意的是这里匹配的字符串需要添加前缀“ROLE_“。
使用注解先要开启注解功能#xff01;
启动类(配置类)开启注解
EnableGlobalMethodSecurity(securedEnable true) 在controller的…注解使用
Secured
判断用户是否具有角色可以访问方法另外需要注意的是这里匹配的字符串需要添加前缀“ROLE_“。
使用注解先要开启注解功能
启动类(配置类)开启注解
EnableGlobalMethodSecurity(securedEnable true) 在controller的方法上面使用注解设置角色 RestController
RequestMapping(/test)
public class TestController {GetMapping(hello)public String hello(){return hello security;}GetMapping(index)public String index(){return hello index;}GetMapping(update)Secured({ROLE_sale,ROLE_manager})public String update(){return hello update;}
}userDetailsService设置用户角色 测试 PreAuthorize
适合进入方法前的权限验证
启动类开启注解 在controller的方法上面添加注解 userDetailsService设置用户角色 测试 PostAuthorize
在方法执行后再进行权限验证适合验证带有返回值的权限.
开启注解
EnableGlobalMethodSecurity(prePostEnabled true)
在controller的方法上面添加注解 测试 可以看到没有访问权限但是控制台还是输出了update…说明在方法执行后再进行权限验证
PostFilter
方法返回数据进行过滤
PostFilter 权限验证之后对数据进行过滤 留下用户名是 admin1 的数据
表达式中的 filterObject 引用的是方法返回值 List 中的某一个元素
测试
package com.atguigu.securitydemo1.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;Data
AllArgsConstructor
NoArgsConstructor
public class Users {private Integer id;private String username;private String password;}GetMapping(getAll)PostAuthorize(hasAnyAuthority(admins))PostFilter(filterObject.username admin1)public ListUsers getAllUser(){ArrayListUsers list new ArrayList();list.add(new Users(11,admin1,6666));list.add(new Users(21,admin2,888));System.out.println(list);return list;} PreFilter
传入方法数据进行过滤
PreFilter: 进入控制器之前对数据进行过滤