您的位置:首页 > 编程语言 > Java开发

spring security:第一个程序解析

2016-04-10 21:27 609 查看
上一篇在一个项目里配置了spring security,这里大致说一些这些配置的作用。

pom.xml 文件解析

<!-- spring security -->
<!-- spring 安全-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>


这里配置了两个模块,web模块不用说,config模块的作用是:支持使用命名空间(NameSpace)方式配置spring security。

spring security有两种配置方式,一种是Namespace,也就是我们现在使用的。在sring-security.xml文件中我们可以看到:

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">


这里有个security命名空间,就是由config模块提供支持的。

除此之外,spring security有下面几个模块:

模块用途
Core核心基础模块,任何使用spring security 的项目都需要使用这个模块
Remoting如果你使用Spring Remoting的话,就需要引入这个模块,否则不用
Web包含很多过滤器,提供web 用户认证服务和基于URL的访问控制
Config支持spring security命名空间(namespace)配置,如果你使用的是命名空间的配置方式,那就需要引入这个模块
LDAP支持LDAP认证服务
ACL支持ACL认证服务
CAS支持CAS认证服务
OpenID支持OpenID 认证服务
对于各服务的描述,可查看原文:spring security modules

web.xml 文件解析

<!-- spring security 配置 -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-security.xml, /WEB-INF/applicationContext.xml</param-value>
</context-param>


上面的代码1)配置了一个代理Servlet过滤器 2)指出了spring security的配置文件位置。

spring借助一系列filter来提供各种安全性功能,上面配置的
springSecurityFilterChain
这个过滤器就相当去一个入口,它拦截下请求后,抛给spring security定义的各种过滤器去处理。

至于
springSecurityFilterChain
将请求抛给哪些过滤器,这个不用我们担心,我们只要在下面的spring-security.xml文件中使用
<http>
标签定义安全规则,spring security会自动调用相应的过滤器。

这个filter的名字是spring security内部定义的,不能修改。

spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<http>
<intercept-url pattern="/user/**" access="hasRole('USER')" />
<intercept-url pattern="/admin/**" access="hasRole('ADMIN')" />
<form-login />
<logout />
</http>

<authentication-manager>
<authentication-provider>
<user-service>
<user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="bob" password="bobspassword" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>

</beans:beans>


在这个文件里,我们使用了
security
作为默认命名空间,这样就不用再写security 前缀了。

文件中只有两个标签,
<http>
标签和
<authentication-manager>
标签。

1)
<http>
标签

<http>
标签定义http安全规则,
<intercept-url pattern="/user/**" access="hasRole('USER')" />
规定拦截所有
/user/
请求,并规定只有USER角色的认证用户才可以访问。

access="hasRole('USER')"
这里使用的是SPEL表达式,spring security扩展的表达式如下:

表达式用法
hasRole([role])检查用户是否属于某个角色,是的话返回true
hasAnyRole([role1,role2])检查用户是否属于一系列角色中的任意一个,是的话返回true
hasAuthority([authority])检查用户是否有指定权限,是的话返回true
hasAnyAuthority([authority1,authority2])检查用户是否有一系列权限中的任意一个,是的话返回true
principal用户的主要信息对象,包含用户的用户名,ip等等信息
authentication用户认证对象的认证信息
permitAll相当于true
denyAll相当false
isAnonymous()如果当前用户为匿名用户,则返回true
isRememberMe()如果当前用户通过remember me登录,则返回true
isAuthenticated()如果当前用户不是匿名用户,则返回true
isFullyAuthenticated()如果当前用户不是匿名用户也不是通过remember me 登录,则返回true
hasPermission(Object target, Object permission)检查当前用户是否有访问目标路径的权限,例如:
hasPermission(domainObject, 'read')
hasPermission(Object targetId, String targetType, Object permission)同上,例如:hasPermission(1, ‘com.example.domain.Message’, ‘read’)
参看:spring security built in

2)
<authentication-manager>
标签

那USER这个角色是在哪里定义的呢?
<authentication-manager>
标签就是用管理认证用户的,在这个标签内部,使用
<user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" />
标签,定义用户名为jimi的用户是一个USER角色和一个ADMIN角色,ROLE_是spring security默认的角色前缀。

<authentication-manager>
标签既是用户认证的管理者,可以管理基于内存、基于数据库、基于LDAP、OpenID等等认证方式,这里使用的就是最简单的基于内存的认证方式。

参考文档:

spring security 4.0.4 reference
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: