使用 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"
相关推荐
SpringBoot 自定义注解,属性支持SPEL表达式。介绍了SPEL表达式的解析
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. ...
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 表达式详解 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖
Spring Expression Language(简称 SpEL)是一个支持查询和操作运行时对象导航图功能的强大的表达式语言,它的语法类似于传统 EL(如jsp中的EL表达式),但提供额外的功能,最出色的就是函数调用和简单字符串的模板...
Spring_SpEl表达式使用用例 只是一个简单的demo,有需要的可以看看
Spring Expression Language (SpEL)中文文档。基于Spring4.x。
- 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 ...
spring-framework.zip,spring表达式语言(spel)spring框架
spring-framework.zip,spring表达式语言(spel)spring框架
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
1.15 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3 . . . . . . . . . . . . . . .177 1.16 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3 . . . . . . . . . . . . ....
Dynamic-Datasource (opens new window)- 基于 SpringBoot 的多数据源组件,功能强悍,支持 Seata ...提供使用 spel动态参数 解析数据源方案。内置spel,session,header,支持自定义。 支持 多层数据源嵌套切换 。
Spring框架中的spring-messaging模块提供了一种基于WebSocket的STOMP协议实现,STOMP消息代理在处理客户端消息时存在SpEL表达式注入漏洞,因此攻击者可以通过构造恶意的消息来实现远程代码执行。 Windows平台...
spring-framework.zip,spring表达式语言(spel)spring框架
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...
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 (opens new window)- 基于 SpringBoot 的多数据源组件,功能强悍,支持 Seata ...提供使用 spel动态参数 解析数据源方案。内置spel,session,header,支持自定义。 支持 多层数据源嵌套切换 。
主要介绍了Spring spel表达式使用方法示例,通过一些实例向大家展示了spel表达式的用法,需要的朋友可以了解下。
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