使用SpringSecurity保护方法应用
(1)pom添加依赖
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
(2)添加相应配置类
package cn.coreqi.config; import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler;
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration; @Configuration
@EnableGlobalMethodSecurity(securedEnabled = true,jsr250Enabled = true,prePostEnabled = true) //启用基于注解的方法安全性
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
/**
* 注册自定义的表达式处理器
* @return
*/
// @Override
// protected MethodSecurityExpressionHandler createExpressionHandler() {
// DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
// expressionHandler.setPermissionEvaluator(new UserPermissionEvaluator());
// return expressionHandler;
// }
}
(3)dao使用相应的注解
仅仅作为参考
package cn.coreqi.dao.redis; import cn.coreqi.entities.User;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.stereotype.Repository; import java.util.List; @Repository
public class UserRedis { @Secured("ROLE_ADMIN") //限制只有ROLE_ADMIN权限才可以调用此方法
public List<User> getAll(){
return null;
} @PostAuthorize("returnObject.UserName == principal.username") //方法返回时执行,根据表达式结果决定是否抛出安全性异常
public User getById(int Id){
return null;
} @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SYSTEM') and #user.UserName.length() <= 16") //在方法调用前执行,如果表达式不为true则阻止方法执行
public User modify(User user){
return null;
} public void delById(int Id){
} @PostFilter("hasRole('ROLE_ADMIN') && filterObject.Age < 18") //使用表达式计算该方法返回中的每一个成员,将计算结果为false的成员移除掉。
public List<User> findMeinv()
{
return null;
} @PreFilter("hasRole('ROLE_ADMIN') && targetObject.Name == 'admin'") //在方法调用前对参数中的每一个元素进行过滤,只有满足表达式的元素才会保留到集合中
public void batchAdd(List<User> users){ }
}
(4)*(不重要)自定义表达式处理器,如何注册参考(2)
package cn.coreqi.config; import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication; import java.io.Serializable; /**
* 自定义表达式处理器
*/
public class UserPermissionEvaluator implements PermissionEvaluator {
@Override
public boolean hasPermission(Authentication authentication, Object o, Object o1) {
return false;
} @Override
public boolean hasPermission(Authentication authentication, Serializable serializable, String s, Object o) {
return false;
}
}
使用SpringSecurity保护方法应用的更多相关文章
- Spring保护方法
Spring保护方法 一.使用注解保护方法 1.@Secured 由Spring Security提供,首先需要启用基于注解的方法安全性: @EnableGlobalMethodSecurity(se ...
- ruby中的私有方法和保护方法
ruby中的私有方法是指方法只能被隐含调用,不能被显示调用.而当没有显示接收者的时候,会把self当成接收者.因此,只能在自身中调用私有方法,这也是私有方法的调用规则. ruby的私有方法机制目的是: ...
- 使用SpringSecurity保护程序安全
首先,引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- python,关于这个里边的私有方法(private)、保护方法(protected)、公开方法(public)
__foo__: 定义的是特殊方法,一般是系统定义名字 ,类似 __init__() 之类的. _foo: 以单下划线开头的表示的是 protected 类型的变量,即保护类型只能允许其本身与子类进行 ...
- 消息函数一般是私有的,因为不需要程序员显示的调用,但子类如果需要改写这个方法,则改成保护方法Protected
许多的面向对象程序设计语言都支持对消息的处理.消息处理是一种动态响应客户类发出的请求,它与过程调用不同.过程调用中,客户类必须知道服务类提供了哪些过程,以及每个过程的调用约定,并且在调用时需要明确指出 ...
- C# xml 常规 保护 方法总结
一 使用xsd模式文件验证xml文件: xml文件: <?xml version="1.0" encoding="utf-8" ?> <Boo ...
- U盘去保护方法
一.基本信息 U盘大小是16G的,估计用了2G的空间存储,没有任何开关设置,格式化或写入时提示被写保护: U盘放到任何一台电脑上都是只能读不能写,说明与电脑无关,用了各种U盘修复程序都无效: 二.一般 ...
- 分享:APK高级保护方法解析(三)
刷朋友圈.玩游戏.看新闻,智能手机正在以我们无法想象的速度飞快发展,可是随之而来的安全问题也越来越引人关注,APP二次打包.反编译.盗版的现象屡见不鲜.因此须要对APK进行加固保护. 眼下市面上常见的 ...
- 使用SpringSecurity保护你的Eureka.
因为注册中心基本上都是自己的应用在使用,应用不是特别多,可以写死,如果应用很多,那么就写入数据库把 pom <dependency> <groupId>org.springfr ...
随机推荐
- MT【219】构造二次函数
(2012北大保送)已知$f(x)$是二次函数,且$a,f(a),f(f(a)),f(f(f(a)))$是正项等比数列;求证:$f(a)=a$ 构造二次函数$f(x)=qx$,则$a,f(a),f(f ...
- 洛谷 P3253 [JLOI2013]删除物品 解题报告
P3253 [JLOI2013]删除物品 题目描述 箱子再分配问题需要解决如下问题: (1)一共有\(N\)个物品,堆成\(M\)堆. (2)所有物品都是一样的,但是它们有不同的优先级. (3)你只能 ...
- OneProxy 管理
-----client-----------haproxy---------mysql1----------mysql2------192.168.1.250 192.168.1.1 192.168. ...
- java NIO 直接与非直接缓冲区
ByteBuffer有两个创建缓冲区的方法:static ByteBuffer allocate(int capacity)static ByteBuffer allocateDirect(int c ...
- MySQL之汇总数据(AVG,COUNT,MAX,MIN,SUM)
table test Field Type Null Key Default Extra id int(11) NO PRI NULL auto_increment name char(50) NO ...
- 洛谷 P1140 相似基因(DP)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 参考资料: [1]:https://www.cnblogs.com/real-l/p/9 ...
- Celery配置实践笔记
说点什么: 整理下工作中配置celery的一些实践,写在这里,一方面是备忘,另外一方面是整理成文档给其他同事使用. 演示用的项目,同时也发布在Github上: https://github.com/b ...
- java集合复制和反转
1.for循环方法: 2.System.arraycopy()方法: 3.Arrays.copyOf()方法: 4.Object.clone()方法: + View code /** * @autho ...
- GO语言的进阶之路-面向过程式编程
GO语言的进阶之路-面向过程式编程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们在用Golang写一个小程序的时候,未免会在多个地方调用同一块代码,这个时候如何优化你的代码呢 ...
- check nginx配置文件错误:[emerg]: getpwnam(“nginx”) failed
1.错误提示: [root@server include]# /application/nginx/sbin/nginx -t -c /applications/nginx/nginx/nginx.c ...