(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. codeforces 793B. Igor and his way to work

    B. Igor and his way to work time limit per test 3 seconds memory limit per test 256 megabytes input ...

  2. FieldGroup绑定ItemDataSource

    FieldGroup可以直接绑定一个数据源DataSource.但如果想绑定某个值,并没有直接作为数据库中的一个字段存在.而是最后转为json串保存在数据库中.这样的话相当于key-value模式的D ...

  3. JVM 内存区域 (运行时数据区域)

    JVM 内存区域 (运行时数据区域) 链接:https://www.jianshu.com/p/ec479baf4d06 运行时数据区域 Java 虚拟机在执行 Java 程序的过程中会把它所管理的内 ...

  4. wordpress 页面显示指定分类文章

    首页显示指定分类备份主题文件夹中的 index.php 文件,修改index.php找到如下一行代码:<?php if (have_posts()) : ?>在上面这行代码的前面加上:&l ...

  5. 【LOJ#572】Misaka Network 与求和(莫比乌斯反演,杜教筛,min_25筛)

    [LOJ#572]Misaka Network 与求和(莫比乌斯反演,杜教筛,min_25筛) 题面 LOJ \[ans=\sum_{i=1}^n\sum_{j=1}^n f(gcd(i,j))^k\ ...

  6. 【BZOJ3879】SvT(后缀自动机,虚树)

    [BZOJ3879]SvT(后缀自动机,虚树) 题面 BZOJ 题解 看着这个东西,询问若干个前缀两两之间的\(lcp\)? 显然\(lcp\)就是\(SAM\)构建出来的\(parent\)数上的\ ...

  7. rt-thread learning notes

    rt-thread learning notes 2018-01-15 > 001 具有相同优先级的线程,每个线程的时间片大小都可以在初始化或创建该线程时指定 rt_thread_t rt_th ...

  8. LoadRunner回放乱码

    乱码的原因和服务器字符集编码相关,默认LR是gbk解码,如果服务器是utf8,那么需要改为utf8解码,这样中文就显示正常了. 通过查看源码,可以知道服务器的编码方式:<meta charset ...

  9. nowcoder172A 中位数 (二分答案)

    二分一下答案,假设是x. 我们把大于x的看成1,小于x的看成-1,等于x的看成0 那某个区间的和如果是正的,就说明这个区间中位数大于x:如果是0,就等于x:如果是负的,就小于x: 这样的话,做一个前缀 ...

  10. php关于Session和cookie总结

    什么是 Cookie? cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie.通过 PHP,能够创建并取回 c ...