`
hanqunfeng
  • 浏览: 1526423 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

SpringSecurity3.X--SpEL 表达式

 
阅读更多

使用 Spring 表达式语言配置访问控制,要实现这一功能的直接方式是在<http>配置元素上添加 use-expressions 属性:

 

<http auto-config="true"  use-expressions="true">

这样就会在投票器中自动增加一个投票器:org.springframework.security.web.access.expression.WebExpressionVoter

 

 

但是,如果显示声明了accessDecisionManager,则需要手工加上这个投票器,参考:http://hanqunfeng.iteye.com/blog/1155226

我们以此为基础进行配置:

1.声明WebExpressionVoter

 

<!-- 启用表达式 为了后面的投票器做准备 -->
	<beans:bean class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler" id="expressionHandler"/>
	<beans:bean class="org.springframework.security.web.access.expression.WebExpressionVoter" id="expressionVoter">		
		<beans:property name="expressionHandler" ref="expressionHandler"/>	
	</beans:bean>

 2.在accessDecisionManager中增加WebExpressionVoter

 

 

<beans:bean id="accessDecisionManager"
		class="org.springframework.security.access.vote.AffirmativeBased">
		<beans:property name="decisionVoters">
			<beans:list>
				<beans:bean class="org.springframework.security.access.vote.RoleVoter" />
				<beans:bean
					class="org.springframework.security.access.vote.AuthenticatedVoter" />
				<beans:bean class="com.netqin.common.security.DynamicRoleVoter" />
				<beans:ref bean="expressionVoter"/>
			</beans:list>
		</beans:property>
	</beans:bean>

 

 

3.在http标签中增加use-expressions="true"

 

4.修改拦截规则为SpEL表达式

 

<intercept-url pattern="/demo.do*" access="authenticated" />
<intercept-url pattern="/**/*.do*" access="hasRole('HODLE')" />

 

 

ok,以上就配置完成了。

 

SpEL表达式说明

用于匹配一个请求的IP 地址或一个地址的网络掩码

access="hasIpAddress('162.79.8.30')"

access="hasIpAddress('162.0.0.0/224')"

 

用于匹配一个使用GrantedAuthority 的角色(类似于 RoleVoter)

access="hasRole('ROLE_USER')"

 

用于匹配一个使用GrantedAuthority 的角色列表。用户匹配其中的任何一个均可放行。

access="hasRole('ROLE_USER','ROLE_ADMIN')"

 

任何用户均可访问  

access="permitAll"

 

任何用户均不可访问  

access="denyAll"

 

匿名用户可访问  

access="anonymous"

 

检查用户是否认证过 

access="authenticated"

 

检查用户是否通过remember me 功能认证的  

access="rememberMe"

 

检查用户是否通过提供完整的凭证信息来认证的 

access="fullyAuthenticated"

 

在 SpEL 中,使用 and,or 以及 not 作为逻辑操作符,例如:

access="hasRole('ROLE_USER') and fullyAuthenticated"

access="rememberMe or fullyAuthenticated"

0
0
分享到:
评论

相关推荐

    spring-boot-annotation-spel.zip

    SpringBoot 自定义注解,属性支持SPEL表达式。介绍了SPEL表达式的解析

    spring-boot-reference.pdf

    24.6.3. Multi-profile YAML Documents 24.6.4. YAML Shortcomings 24.7. Type-safe Configuration Properties 24.7.1. Third-party Configuration 24.7.2. Relaxed Binding 24.7.3. Merging Complex Types 24.7.4. ...

    spring4.1核心包

    7. spring-expression-4.1.1.RELEASE.jar spring表达语言 SpEL以"#{...}"进行标识。 8. spring-instrument-4.1.1.RELEASE.jar Spring对服务器的代理接口 9.spring-instrument-tomcat-4.1.1.RELEASE.jar Spring对...

    spring spEL 表达式详解

    spring spEL 表达式详解 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖

    spring-expression.zip

    Spring Expression Language(简称 SpEL)是一个支持查询和操作运行时对象导航图功能的强大的表达式语言,它的语法类似于传统 EL(如jsp中的EL表达式),但提供额外的功能,最出色的就是函数调用和简单字符串的模板...

    Spring_SpEl表达式使用用例

    Spring_SpEl表达式使用用例 只是一个简单的demo,有需要的可以看看

    Spring表达式语言中文参考手册.docx

    Spring Expression Language (SpEL)中文文档。基于Spring4.x。

    Getting.started.with.Spring.Framework.2nd.Edition1491011912.epub

    - SpEL (Spring Expression Language) - Caching using Spring's cache abstraction - Sending and receiving JMS messages using Spring - Aspect-oriented programming support in Spring - Sending emails using ...

    xbean-spring-3.15.zip

    spring-framework.zip,spring表达式语言(spel)spring框架

    spring-jms-3.2.4.RELEASE.zip

    spring-framework.zip,spring表达式语言(spel)spring框架

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    跟开涛学Spring

    1.15 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3 . . . . . . . . . . . . . . .177 1.16 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3 . . . . . . . . . . . . ....

    dynamic-datasource-spring-boot-starter-v3.5.1.zip

    Dynamic-Datasource (opens new window)- 基于 SpringBoot 的多数据源组件,功能强悍,支持 Seata ...提供使用 spel动态参数 解析数据源方案。内置spel,session,header,支持自定义。 支持 多层数据源嵌套切换 。

    spring-framework-5.0.5.RELEASE 漏洞修复

    Spring框架中的spring-messaging模块提供了一种基于WebSocket的STOMP协议实现,STOMP消息代理在处理客户端消息时存在SpEL表达式注入漏洞,因此攻击者可以通过构造恶意的消息来实现远程代码执行。 Windows平台...

    spring-beans-4.0.4.RELEASE.zip

    spring-framework.zip,spring表达式语言(spel)spring框架

    springframework.4.3.22.RELEASE

    Spring JavaMailSenderImpl does not show proper message when recipient list is empty [SPR-17540] #22072 Potential resource leak in DataSourceUtils.doGetConnection [SPR-17559] #22091 SpEL, error parsing...

    springboot参考指南

    SpEL表达式条件 xvi. 37. WebSockets xvii. 38. 接下来阅读什么 6. V. Spring Boot执行器: Production-ready特性 i. 39. 开启production-ready特性 ii. 40. 端点 i. 40.1. 自定义端点 ii. 40.2. 健康信息 iii. 40.3...

    dynamic-datasource-spring-boot-starter-v3.5.1.tar.gz

    Dynamic-Datasource (opens new window)- 基于 SpringBoot 的多数据源组件,功能强悍,支持 Seata ...提供使用 spel动态参数 解析数据源方案。内置spel,session,header,支持自定义。 支持 多层数据源嵌套切换 。

    Spring spel表达式使用方法示例

    主要介绍了Spring spel表达式使用方法示例,通过一些实例向大家展示了spel表达式的用法,需要的朋友可以了解下。

    spring framework4

    spring4 开发jar包 Introduction The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any ...JDK 5+ for Spring Framework 3.x

Global site tag (gtag.js) - Google Analytics