spring security method security

spring security method security

参考

Spring Security 官方文档

http://www.concretepage.com/spring/spring-security/preauthorize-postauthorize-in-spring-security

方法调用安全

对应的注解@EnableGlobalMethodSecurity,该注解放在GlobalMethodSecurityConfiguration的子类上方

@EnableGlobalMethodSecurity(prePostEnabled = true)

使用的Voter

org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter

有俩对对应的注解

@PreAuthorize 决定方法是否可以被调用

@PostAuthorize 决定方法是否可以返回该值

@PreFilter

@PostFilter

如下:

package com.jiangchong.methodsecurity;import org.springframework.security.access.method.P;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;public interface IBookService
{@PreAuthorize("hasRole('ROLE_ADMIN')")public void addBook(Book book);// PostAuthorize,决定这个值是否可以被返回,使用returnObject/** Less commonly, you may wish to perform an access-control check after the* method has been invoked. This can be achieved using the @PostAuthorize* annotation. To access the return value from a method, use the built-in* name returnObject in the expression.*/@PostAuthorize("returnObject.owner == authentication.name")public Book getBook();// PreAuthorize,决定这个方法是否可以被调用/** @P单个参数的方法*/@PreAuthorize("#b.owner == authentication.name")public void deleteBook(@P("b") Book book);/** @Param放在至少有一个参数的方法的上* * @PreAuthorize("#n == authentication.name") Contact* findContactByName(@Param("n") String name)*/// springEL/** @PreAuthorize("#contact.name == authentication.name") public void* doSomething(Contact contact);*/}

测试的Demo,基于Spring Boot

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.jiangchong</groupId><artifactId>methodsecurity</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>methodsecurity</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.2.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><scope>compile</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-core</artifactId></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-web</artifactId><scope>compile</scope></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-config</artifactId></dependency></dependencies>
</project>

App.class

package com.jiangchong.methodsecurity;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/****/
@RestController
@SpringBootApplication
public class App
{@Autowiredpublic IBookService bookService;public static void main(String[] args){SpringApplication.run(App.class, args);}@RequestMapping("/")public Map<String, String> test(){Book b1 = new Book("A", "admin");bookService.addBook(b1);bookService.getBook();System.out.println("user return");Book b2 = new Book("B", "user");bookService.deleteBook(b2);return null;}/** @RequestMapping("/admin") public Map<String, String> testAdmin() {* Map<String, String> map = new HashMap<>(); map.put("admin", "admin");* return map; }* * @RequestMapping("/user") public Map<String, String> testUser(String name)* { Map<String, String> map = new HashMap<>(); map.put("user", "user");* return map; }* * @RequestMapping("/resource/test") public Map<String, String>* testResouce() { Map<String, String> map = new HashMap<>();* map.put("test", "resource"); return map; }*/
}

Book.class

package com.jiangchong.methodsecurity;public class Book
{private String name;private String owner;public Book(String name, String owner){this.name = name;this.owner = owner;}public String getName(){return name;}public void setName(String name){this.name = name;}public String getOwner(){return owner;}public void setOwner(String owner){this.owner = owner;}
}

BookService.class

package com.jiangchong.methodsecurity;import org.springframework.stereotype.Service;@Service
public class BookService implements IBookService
{@Overridepublic void addBook(Book book){System.out.println("You have successfully added book.");}@Overridepublic Book getBook(){Book book = new Book("B", "user");System.out.println("return " + book.getOwner());return book;}@Overridepublic void deleteBook(Book book){System.out.println("Books deleted");}}

IBookService

package com.jiangchong.methodsecurity;import org.springframework.security.access.method.P;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;public interface IBookService
{@PreAuthorize("hasRole('ROLE_ADMIN')")public void addBook(Book book);// PostAuthorize,决定这个值是否可以被返回,使用returnObject/** Less commonly, you may wish to perform an access-control check after the* method has been invoked. This can be achieved using the @PostAuthorize* annotation. To access the return value from a method, use the built-in* name returnObject in the expression.*/@PostAuthorize("returnObject.owner == authentication.name")public Book getBook();// PreAuthorize,决定这个方法是否可以被调用/** @P单个参数的方法*/@PreAuthorize("#b.owner == authentication.name")public void deleteBook(@P("b") Book book);/** @Param放在至少有一个参数的方法的上* * @PreAuthorize("#n == authentication.name") Contact* findContactByName(@Param("n") String name)*/// springEL/** @PreAuthorize("#contact.name == authentication.name") public void* doSomething(Contact contact);*/}

MethodSecurityConfig

package com.jiangchong.methodsecurity;import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration
{protected void configure(AuthenticationManagerBuilder auth)throws Exception{auth.inMemoryAuthentication();}}

WebSecurityConfig

package com.jiangchong.methodsecurity;import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{protected void configure(HttpSecurity http) throws Exception{http.authorizeRequests().anyRequest().authenticated().and().formLogin().loginProcessingUrl("/login").permitAll();}public void configure(WebSecurity web) throws Exception{web.ignoring().antMatchers("/resource/**");}protected void configure(AuthenticationManagerBuilder auth)throws Exception{auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN").and().withUser("user").password("user").roles("USER");}}
    Book b1 = new Book("A", "admin");bookService.addBook(b1);bookService.getBook();System.out.println("user return");Book b2 = new Book("B", "user");bookService.deleteBook(b2);
这些调用序列,只要有一个不满足权限,后面的方法不会再调用
posted on 2016-02-22 01:35 好吧,就是菜菜 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/5206000.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/398893.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android浏览器速度测试,Android平台浏览器网页加载速度对比评测

参与测试的浏览器手机上网已经渐渐成为人们的一种习惯&#xff0c;无论在等公交时、乘地铁时、吃饭等餐时&#xff0c;很多朋友都习惯掏出手机简单浏览一下微博、人人、新闻网站等页面。移动网络的确为我们带来了莫大的便利&#xff0c;但网速和稳定性却是移动网络的硬伤。针对…

操作系统随笔(一)

你好朋友&#xff0c;当你点进来这份读书笔记时&#xff0c;我相信你不是无意中点进来就是对这一部分饶有兴趣&#xff0c;可惜的是&#xff0c;我也只是个普通的大学生&#xff0c;有时候对知识的见解获取没有屏幕前的你那么有天赋&#xff0c;所以在阅读完这篇文章的同时点个…

安装Fedora后

更新操作系统版本&#xff1a; https://fedoraproject.org/wiki/DNF_system_upgrade 靠谱&#xff1b; 设置ssh&#xff1a;ssh生成公钥私钥、默认root(.ssh/config)、chmod -R 600 .ssh (.ssh/config)Host * User root Protocol 2 ServerAliveInterval 30 shadowsocks客户端…

codeforces 483B Friends and Presents 解题报告

题目链接&#xff1a;http://codeforces.com/problemset/problem/483/B 题目意思&#xff1a;有两个 friends&#xff0c;需要将 cnt1 个不能整除 x 的数分给第一个friend&#xff0c;cnt2 个不能整除 y 的数分给第二个friend。x 和 y 都是素数来的。要求求出最小的 v&#xff…

并行计算随笔(一)

如果觉得本篇文章对你有所启发&#xff0c;请给我点个赞好吗&#xff0c;这对我很重要&#xff0c;谢谢 文章目录1 并行计算基础1.1 什么是并行计算1.1.1 对计算速度的需求1.1.2 并行计算1.1.3 并行计算的基本条件1.1.4 平行计算和分布式计算1.2 为什么需要并行计算1.3 并行计算…

命令行的形式运行php

转自&#xff1a;http://www.cnblogs.com/myjavawork/articles/1869205.html 注意&#xff1a;在安装php时需要将php 的安装目录加到环境变量 PATH 中 (右击我的电脑->属性->高级->环境变量, 如果存在 PATH 则在原来的 PATH 中加入你的PHP安装目录, 如果不存在则新建一…

LeetCode-18-4Sum

一、问题描述 给定一个数组S&#xff0c;和一个int类型的数target&#xff0c;在S中寻找四个数&#xff0c;这四个数之和为target。返回一个vector<vector<int>> 例子&#xff1a;S{1, 0, -1, 0, -2, 2}&#xff0c;target 0.返回结果为{{-1&#xff0c;0&#xf…

Ubuntu安装qwt步骤

1、svn获取代码 svn checkout https://svn.code.sf.net/p/qwt/code/trunk/qwt 2、生产makefile qmake 3、编译(确保已经安装了qopengl再进行这一步&#xff0c;不然失败) make 4、安装 make install转载于:https://www.cnblogs.com/larkin/p/4058959.html

idea android 模块,IntelliJ IDEA 12 - 新的Android应用程序模块向导失败,“无法找到模块的资源目录”...

我面临的问题是一个非常简单的问题... 我无法使用IntelliJ IDEA 12来帮助我创建一个Android应用程序模块(项目)。IntelliJ IDEA 12 - 新的Android应用程序模块向导失败&#xff0c;“无法找到模块的资源目录”成功安装的IntelliJ IDEA 12和使IDE和二者的SDK(Java和Android设备)…

4. time datetime 时间模块

1) 时间表示方式 •时间戳(timestamp)&#xff1a;表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”&#xff0c;返回的是float类型。 •结构化的时间(struct_time)&#xff1a;struct_time元组共有9个元素共九个元素:(年&#xff0c;月&am…

JQuery常用知识点汇总

2019独角兽企业重金招聘Python工程师标准>>> 0、JQuery的基本属性标识&#xff1a; $(".xxx")&#xff1a;标签的class属性&#xff1b; $("#xxx")&#xff1a;标签的id属性&#xff1b; $("xxx")&#xff1a; 标签名&#…

数据库杂谈(三)——关系代数

3 形式化关系查询语言 摘要&#xff1a;关系代数是一种抽象的查询语言&#xff0c;用对关系的运算来表达查询&#xff0c;作为研究关系数据语言的数学工具。在本文中&#xff0c;我们不仅谈论关系代数的知识点&#xff0c;而且还配备了对应的练习题。 文章目录3 形式化关系查询…

android native.js,Android Native与JS通信互调

写在最前&#xff1a;看Android最新技术总结&#xff0c;关注公众号&#xff1a;最近因为App与H5交互逻辑太乱&#xff0c;所以抽空梳理了下&#xff1b;对目前App与H5的各种交互通信做个总结&#xff0c;自取适合自己的交互方式。一、H5调用原生的native方法1、拦截shouldOver…

SharePoint2013 Excel导出好的代码

C#Excel操作类ExcelHelper.cs 来源&#xff1a;http://www.hellocsharp.com/article/67.aspx C#源码世界 发布于&#xff1a; 2014-09-12使用本类之前必须在本机安装了office excel组件或直接下载Microsoft.Office.Interop.Excel.dll文件引用到项目目录下&#xff1a; 然后复制…

基于PMOS的电源防反接电路

如下图所示&#xff0c;是来自TI的参考设计TIDA-00982中的一个电路&#xff0c;功能主要是防止输入反接&#xff0c;R6和C6形成吸收回路&#xff0c;可以对上电过冲有一定的抑制作用。电源正常接入时&#xff0c;PMOS导通&#xff0c;给负载供电&#xff0c;由于VDS的存在&…

计算机组成原理随笔(一)

1 计算机体系结构 计算机革命发展得非常快速&#xff0c;以至于使用老式计算机的很多电影现在看起来十分有年代感&#xff0c;有的电影甚至无法预料后来的计算机是什么样的。 计算机各个组成部分的技术发展非常不均衡&#xff0c;各部分性能差异非常大。在计算机的发展前期&a…

Mongo读书笔记1 -- GridFS

一个Mongo文档最大4M. GridFS不依赖于MongoDB, 其他符合规范的驱动都可以访问它。 GridFS包含两部分&#xff1a;一部分存储文件名和其他metadata; 另一部分存储实际的文件&#xff0c;通常分成一个个大小为256k的小块。 这两个部分通常被命名为files和chunks,在fs命名空间下&a…

简单搭建一个SSM项目(一)

简单搭建一个用户管理的SSM项目框架&#xff0c;虽然也能用servletjdbc搭建更简单的&#xff0c;不过个人感觉工作中更多用的ssm框架项目&#xff0c;这里就简单用ssm来搭建需要的项目吧。 准备工具&#xff1a;eclipse、jdk1.7、Mysql、maven、tomcat。&#xff08;请先确定计…

httpd2.4源码编译

系统版本&#xff1a;RedHat6.5内核版本&#xff1a;2.6.32-431.el6.x86_641、安装httpd2.4版本的软件&#xff0c;首先需要安装apr和apr-util这两个软件包&#xff1b;注意:apr和apr-util的版本&#xff0c;必须在1.5以上&#xff0c;否则会报错[rootnode1 ~]# ls apr-1.5.2.t…

Hadoop随笔(一)

文章目录1 大数据概述1.1 什么是大数据1.2 大数据技术背后的核心思想1.2.1 把数据分发到多个节点1.2.2 把计算逻辑移到数据附近1.2.3 计算节点进行本地数据处理1.2.4 优选顺序读&#xff0c;次之随机读1.2.5 例子1.3 大数据的编程模型1.3.1 大规模并行处理数据库系统1.3.2 内存…