(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保护方法应用的更多相关文章

  1. Spring保护方法

    Spring保护方法 一.使用注解保护方法 1.@Secured 由Spring Security提供,首先需要启用基于注解的方法安全性: @EnableGlobalMethodSecurity(se ...

  2. ruby中的私有方法和保护方法

    ruby中的私有方法是指方法只能被隐含调用,不能被显示调用.而当没有显示接收者的时候,会把self当成接收者.因此,只能在自身中调用私有方法,这也是私有方法的调用规则. ruby的私有方法机制目的是: ...

  3. 使用SpringSecurity保护程序安全

    首先,引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  4. python,关于这个里边的私有方法(private)、保护方法(protected)、公开方法(public)

    __foo__: 定义的是特殊方法,一般是系统定义名字 ,类似 __init__() 之类的. _foo: 以单下划线开头的表示的是 protected 类型的变量,即保护类型只能允许其本身与子类进行 ...

  5. 消息函数一般是私有的,因为不需要程序员显示的调用,但子类如果需要改写这个方法,则改成保护方法Protected

    许多的面向对象程序设计语言都支持对消息的处理.消息处理是一种动态响应客户类发出的请求,它与过程调用不同.过程调用中,客户类必须知道服务类提供了哪些过程,以及每个过程的调用约定,并且在调用时需要明确指出 ...

  6. C# xml 常规 保护 方法总结

    一 使用xsd模式文件验证xml文件: xml文件: <?xml version="1.0" encoding="utf-8" ?> <Boo ...

  7. U盘去保护方法

    一.基本信息 U盘大小是16G的,估计用了2G的空间存储,没有任何开关设置,格式化或写入时提示被写保护: U盘放到任何一台电脑上都是只能读不能写,说明与电脑无关,用了各种U盘修复程序都无效: 二.一般 ...

  8. 分享:APK高级保护方法解析(三)

    刷朋友圈.玩游戏.看新闻,智能手机正在以我们无法想象的速度飞快发展,可是随之而来的安全问题也越来越引人关注,APP二次打包.反编译.盗版的现象屡见不鲜.因此须要对APK进行加固保护. 眼下市面上常见的 ...

  9. 使用SpringSecurity保护你的Eureka.

    因为注册中心基本上都是自己的应用在使用,应用不是特别多,可以写死,如果应用很多,那么就写入数据库把 pom <dependency> <groupId>org.springfr ...

随机推荐

  1. bzoj 2212 : [Poi2011]Tree Rotations (线段树合并)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2212 思路:用线段树合并求出交换左右儿子之前之后逆序对的数量,如果数量变小则交换. 实现 ...

  2. 删除linux下的指定文件

    要求:删除linux下2天前的指定文件 find 文件问题:在 tmp 目录下有大量包含 picture_* 的临时文件,每天晚上 2:00 对一天前的文件进行清理.之前在 crontab 下跑如下脚 ...

  3. 【转】安全加密(三):RFID标签防伪为生活开启安全模式

    本文导读 随着RFID技术的快速发展和RFID电子标签的生产成本不断降低,RFID标签防伪技术的应用也得到了极大的普及,逐步出现在各行各业当中,如交通出行.票务安全.商品防伪等领域. RFID技术简介 ...

  4. poj1038 Bugs Integrated,Inc. (状压dp)

    题意:N*M的矩阵,矩阵中有一些坏格子,要在好格子里铺2*3或3*2的地砖,问最多能铺多少个. 我的方法好像和网上流传的方法不太一样...不管了.... 由数据范围很容易想到状压dp 我们设某个状态的 ...

  5. CF1114B Yet Another Array Partitioning Task(贪心,构造题)

    我至今不敢相信我被这么一道简单的题卡了这么久……看来还是太弱了…… 题目链接:CF原网 题目大意:定义一个序列的“美丽度”为这个序列前 $m$ 大的数的和.现在有一个长度为 $n$ 的序列,你需要把它 ...

  6. 使用fiddler模拟http请求

    概述  与httpwath相比,fiddler能模拟http请求.能断点调试.http分析统计吸引了我,使用之后感觉这个工具非常不错,这篇文章只单介绍一下fiddler工作原理,简单介绍一下它的重要功 ...

  7. pthread_cond_wait() 函数的使用

    1. 首先pthread_cond_wait 的定义是这样的 The pthread_cond_wait() and pthread_cond_timedwait() functions are us ...

  8. JAVA8给我带了什么——lambda表达

    这此年来我一直从事.NET的开发.对于JAVA我内心深处还是很向往的.当然这并不是说我不喜欢.NET.只是觉得JAVA也许才是笔者最后的归处.MK公司是以.NET起家的.而笔者也因为兄弟的原因转行.N ...

  9. 常用服务器构建 ftp

    ftp服务器1.安装vsftpd服务器sudo apt-get install vsftpd2.配置vsftpd.conf文件sudo vi /etc/vsftpd.conf添加下面设置anonymo ...

  10. 受限玻尔兹曼机(Restricted Boltzmann Machine,RBM)

    这篇写的主要是翻译网上一篇关于受限玻尔兹曼机的tutorial,看了那篇博文之后感觉算法方面讲的很清楚,自己收获很大,这里写下来作为学习之用. 原文网址为:http://imonad.com/rbm/ ...