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
{
@Autowired
public 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
{
@Override
public void addBook(Book book)
{
System.out.println("You have successfully added book.");
} @Override
public Book getBook()
{
Book book = new Book("B", "user");
System.out.println("return " + book.getOwner());
return book;
} @Override
public 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);
这些调用序列,只要有一个不满足权限,后面的方法不会再调用
spring security method security的更多相关文章
- Spring Security 4 Method security using @PreAuthorize,@PostAuthorize, @Secured, EL--转
原文地址:http://websystique.com/spring-security/spring-security-4-method-security-using-preauthorize-pos ...
- 初识Spring security-添加security
请先查看 初识Spring security-无Security的SpringMVC 在pom.xml文件中添加包 <!-- Spring Security --> <depende ...
- Spring Security(二十二):6.4 Method Security
From version 2.0 onwards Spring Security has improved support substantially for adding security to y ...
- Spring Security(十七):5.8 Method Security
From version 2.0 onwards Spring Security has improved support substantially for adding security to y ...
- Spring boot Security Disable security
When I use security.basic.enabled=false to disable security on a Spring Boot project that has the fo ...
- 41.4 Method Security方法安全性
41.4.1 <global-method-security> 这个元素是为Spring Security beans上的安全方法添加支持的主要手段.可以通过使用注释(在接口或类级别定义) ...
- spring boot + thymeleaf +security自定义规则 的简单使用
1.前言 以前开发一直使用 springMVC模式开发 ,前端页面常使用 JSP ,现在html5淘汰了 ,要么使用html ,要么使用vue , 现在使用spring boot ,有必要总结一下 ...
- 【JavaEE】SSH+Spring Security自定义Security的部分处理策略
本文建立在 SSH与Spring Security整合 一文的基础上,从这篇文章的example上做修改,或者从 配置了AOP 的example上做修改皆可.这里主要补充我在实际使用Spring Se ...
- Spring Cloud:Security OAuth2 自定义异常响应
对于客户端开发或者网站开发而言,调用接口返回有统一的响应体,可以针对性的设计界面,代码结构更加清晰,层次也更加分明. 默认异常响应 在使用 Spring Security Oauth2 登录和鉴权失败 ...
随机推荐
- 2014年5月份第3周51Aspx源码发布详情
HGM简单连连看游戏源码 2014-5-19 [VS2010]源码描述:这是一款基于WinForm窗体程序的简单水果连连看的小游戏.界面比较美观, 功能如下:该游戏可以显示当前关卡,还有剩余时间.重 ...
- 2014年5月份第2周51Aspx源码发布详情
Reapter手写分页控件源码 2014-5-12 [VS2010]源码描述:实现repeater控件分页,方便好用,界面设计也很漂亮.数据库是Access,可直接运行.入口是RepeaterTes ...
- int a
系统编译之后所有的变量都存储到符号表中,并且每个表项被分配一个符号ID,一般也是数字的,可以根据该符号的ID直接访问符号的值内存中的数据都是二进制的,没有ASCII值ASCII只在为便于人的理解,对二 ...
- 2014-04-09 互联网Web安全职位面试题目汇总
Domain 解释一下同源策略 同源策略,那些东西是同源可以获取到的 如果子域名和顶级域名不同源,在哪里可以设置叫他们同源 如何设置可以跨域请求数据?jsonp是做什么的? Ajax Ajax是否遵循 ...
- android基础(四)service
Service的两种启动方式:startService()与bindService() statService:生命周期:[onCreate()- >onStartCommand()-&g ...
- 团队开发——冲刺2.a
冲刺阶段二(第一天) 1.今天准备做什么? 收集游戏图片:开始.暂停.继续.重新开始.退出……为了界面的后期美工做准备. 2.遇到什么困难? 网上的图片很多,但是比较难找到统一风格的.
- UVALive 7302 (最短路)
Probelm Terrorists 题目大意 给一张n个点,m条边的无向图.共有q个询问,每次询问u到v的最短路. n <= 100000 , n-1 <= m <= n + 5 ...
- 这些 Git 技能够你用一年了
这些 Git 技能够你用一年了 原文出处: Pyper 用git有一年了,下面是我这一年来的git使用总结,覆盖了日常使用中绝大多数的场景.嗯,至少是够用一年了,整理出来分享给大家,不明白的地方可以回 ...
- iOS常用设计模式和机制之Block简单使用
Block :block 实际上就是 Objective-C语言对闭包的实现 闭包(Closure):闭包就是一个函数,或者一个指向函数的指针,加上这个函数执行的非局部变量.闭包允许一个函数访问声明该 ...
- 数论 UVA 11889
有关数论的题目,题目大意是给你两个数a和c,c为a和另一个数b的最小公倍数,要求你求出b的最小值.由最大公约数gcd(a,b)和最小公倍数lcm(a,b)之间的关系可知,lcm(a,b)*gcd(a, ...