(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. day7 字符集转换

    python2 默认 ASCLL 不支持中文 转换比较麻烦.需要先解码成unicode然后在编码成想转换的语言 s = "特斯拉" s_to_unicode = s.decode( ...

  2. 洛谷 P1078 文化之旅 解题报告

    P1078 文化之旅 题目描述 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超过一次(即如果他学习了某种文化,则他就不能到达其他有这种文化的国家).不同的国家可能 ...

  3. 监控(2)-php-fpm进程监控 shell

    #!/bin/bash #监控的网页地址url="http://dev2.jwsmed.com" #fastcgi启动/重启/停止脚本路径PROG=/data/fistsoft/p ...

  4. bzoj5281/luogu4377 Talent Show (01分数规划+背包dp)

    就是01分数规划的思路,只不过当把w[i]-r*t[i]>0的选完以后如果w值还没达到要求,那就再01背包dp一下就好了(dp时w值>W的时候就存在W里就不会爆内存了). (跑得很慢..大 ...

  5. Luogu 1941 【NOIP2014】飞扬的小鸟 (动态规划)

    Luogu 1941 [NOIP2014]飞扬的小鸟 (动态规划) Description Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度 ...

  6. [HEOI2013]SAO ——计数问题

    题目大意: Welcome to SAO ( Strange and Abnormal Online).这是一个 VR MMORPG, 含有 n 个关卡.但是,挑战不同关卡的顺序是一个很大的问题. 有 ...

  7. 洛谷P4383 林克卡特树

    题意:树上最长不相交k条链. #include <cstdio> #include <algorithm> #include <cstring> typedef l ...

  8. 运行vb写的程序,有些电脑上会弹出一个与office相关的窗口

    到网上搜索了一下,找了几个解决方案,不过我的电脑上没出现过,所以先收藏一下,或许以后能用上. 来自:http://bbs.csdn.net/topics/380204412 今天将注册表的项一个个删除 ...

  9. 收藏:c语言的多线程同步

    1.<秒杀多线程第一篇 多线程笔试面试题汇总> 2.<秒杀多线程第二篇 多线程第一次亲密接触 CreateThread与_beginthreadex本质区别> 3.<秒杀 ...

  10. MATLAB:图像滤波,绝对值差(filter2,imabsdiff函数)

    下面是对图像进行滤波,以及求滤波后的图像与原图像的绝对值差的实现过程,涉及到的函数有filter2,imabsdiff函数: close all; %关闭当前所有图形窗口,清空工作空间变量,清除工作空 ...