Spring Security 对Service层方法调用的权限控制

栏目: 后端 · 发布时间: 8年前

内容简介:Spring Security 对Service层方法调用的权限控制

本文以spring security自带的tutorial-xml demo讲解,本文由博主javacoder.cn原创,转载请注明出处!!

配置

为了实现spring security对service层方法调用的权限控制,需要在spring context添加如下配置

<global-method-security pre-post-annotations="enabled" />

启用对方法上的@PreAuthorize,@PostAuthorize等注解的支持

然后在需要控制的方法上添加PreAuthorize注解

public interface BankService {
@PreAuthorize("hasRole('supervisor') or "
+ "hasRole('teller') and (#account.balance + #amount >= -#account.overdraft)")
public Account post(Account account, double amount);
}

本注解表示要么当前用户拥有supervisor权限,要么拥有teller权限且被操作账户的扣款额在透支额度范围内。PreAuthorize的权限控制表达式为spring的EL表达式。

权限控制的callstack如下:

Spring Security 对Service层方法调用的权限控制

和基于URL权限控制的流程类似,MethodSecurityInterceptor作为认证的入口,然后调用AccessDecisionManager(实现类为AffirmativeBased)进行是否有权限访问进行抉择,在AffirmativeBased中,依然使用基于投票器(PreInvocationAuthorizationAdviceVoter)的方式进行判定,真正的PreAuthorize注解对应的表达式计算逻辑由ExpressionBasedPreInvocationAdvice完成。

Posted in:Spring Security


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

High Performance Python

High Performance Python

Andrew Lewis / O'Reilly Media, Inc. / 2010-09-15 / USD 34.99

Chapter 1. Introduction Section 1.1. The High Performance Buzz-word Chapter 2. The Theory of Computation Section 2.1. Introduction Section 2.2. Problems Section 2.3. Models of Computati......一起来看看 《High Performance Python》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

随机密码生成器
随机密码生成器

多种字符组合密码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具