启用SpELl表达式:在http元素里添加:
use-expressions="true"
如: <http auto-config="true" use-expressions="true"> ...</http>
Spring Security 3 提供的 SpEL 方法和伪属性在以下的表格中进行了描述。要注意的是没有被标明“ web only ”的方法和属性可以在保护其他类型的资源中使用,如在保护方法调用时。示例表示的方法和属性是使用在<intercept-url> 的 access 声明中。
方法 |
Web only? |
描述 |
示例 |
hasIpAddress
(ipAddress) |
Yes |
用于匹配一个请求的 IP 地址或一个地址的网络掩码 |
access="hasIpAddress('
162.79.8.30')"
access="hasIpAddress('
162.0.0.0/224')" |
hasRole(role) |
No |
用于匹配一个使用GrantedAuthority 的角色(类似于 RoleVoter ) |
access="hasRole('ROLE
USER')" |
hasAnyRole(role) |
No |
用于匹配一个使用GrantedAuthority 的角色列表。用户匹配其中的任何一个均可放行。 |
access="hasRole('ROLE_
USER','ROLE_ADMIN')" |
除了以上表格中的方法,在 SpEL 表达式中还有一系列的方法可以作为属性。它们不需要圆括号或方法参数。
属性 |
Web only? |
描述 |
例子 |
permitAll |
No |
任何用户均可访问 |
access="permitAll" |
denyAll |
NO |
任何用户均不可访问 |
access="denyAll" |
anonymous |
NO |
匿名用户可访问 |
access="anonymous" |
authenticated |
NO |
检查用户是否认证过 |
access="authenticated" |
rememberMe |
No |
检查用户是否通过remember me 功能认证的 |
access="rememberMe" |
fullyAuthenticated |
No |
检查用户是否通过提供完整的凭证信息来认证的 |
access="fullyAuthenticated" |