上一篇在一个项目里配置了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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/securityhttp://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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/securityhttp://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

spring security:第一个程序解析相关推荐

  1. 【C++教程】03.第一个程序解析

    第三章 第一个程序解析 前言 第一个程序也即是如何显示字符串"HelloWorld!",这是大多数程序员都走过的路.别提第一次成功编译时,我有多高兴,所以如果你还没编译成功,不要气 ...

  2. Spring Security –在一个应用程序中有两个安全领域

    这篇博客文章主要是关于Spring Security配置的. 更具体地说,它打算显示如何在一个Web应用程序中配置两个不同的安全领域. 第一安全领域是针对浏览器客户端的. 它使我们能够在登录页面中登录 ...

  3. SpringSecurity[1]-SpringSecurity简介以及创建Spring Security第一个项目

    主要内容 Spring Security 简介 第一个Spring Security项目 UserDetailsService详解 PasswordEncoder密码解析器详解 自定义登录逻辑 自定义 ...

  4. Spring Security OAuth2源码解析(一)

    目录 引入 AuthorizationServerEndpointsConfiguration 属性 AuthorizationEndpoint OAuth2RequestFactory Defaul ...

  5. Spring Security OAuth2源码解析(三)——单点登录。

    引入 @EnableOAuth2Client @EnableConfigurationProperties(OAuth2SsoProperties.class) @Import({ OAuth2Sso ...

  6. Spring Security OAuth2源码解析(二)

    鉴权服务器对客户端鉴权之后,会生成token,客户端使用token,就可以去资源服务器获取资源. @EnableResourceServer  @Import(ResourceServerConfig ...

  7. Spring写第一个程序HelloSpring

    要写Spring代码,那jar包肯定是少不了的,这里用Maven进行管理,下面是Maven的坐标 <dependencies><dependency><groupId&g ...

  8. Spring Boot 第一个程序

  9. Spring Security 详解与实操第一节 认证体系与密码安全

    开篇词 Spring Security,为你的应用安全与职业之路保驾护航 你好,我是鉴湘,拉勾教育专栏<Spring Cloud 原理与实战><Spring Boot 实战开发> ...

最新文章

  1. 回答跨专业考研者的几点疑问(计算机考研)
  2. MyISAM和InnoDB执行引擎的区别,为什么MyISAM查询效率高,B树和B+树的区别
  3. 获取按钮点击次数_无限次数使用,不会吧?不会吧?
  4. 西欧八国调查:25%受访者宁要AI政府也不要民选政治家
  5. 基于JAVA+SpringMVC+Mybatis+MYSQL的网上相册展示系统
  6. 【C/C++】inline函数和static函数和宏定义的比较
  7. LeetCode 116. Populating Next Right Pointers in Each Node
  8. GstElement的sink/src有什么区别?
  9. Ubuntu下PostgreSQL数据库集群(PL/Proxy)配置方法
  10. secure CRT 运行脚本
  11. 经济学常识之破窗谬论
  12. 奥城大学计算机专业,2013年美国留学硕士双录取院校一览
  13. hol中心化服务器,存储在传统的中心化服务器
  14. ctf之crypto练习二
  15. Geospatial-地理空间
  16. 01-EMC设计规范
  17. Either类java_怎样利用Either和Option进行函数式错误处理的示例
  18. Txt文档数据的写入与读取
  19. html鼠标特效怎么设置到桌面,用CSS实现鼠标单击特效
  20. python-pcl函数_Python简介,第4章-函数

热门文章

  1. ConcurrentHashMap底层原理?
  2. ora--12154 :TNS :could not resolve the connect identifier specified 错误处理
  3. SQLServer LIKE 通配符
  4. cglib实现动态代理
  5. 实验5 编写、调试具有多个段的程序
  6. 计算机基础知识总结论文,大学计算机基础总结论文
  7. 【C++深度剖析教程30】C++中抽象类和接口
  8. 统计HDFS文件数量,大小,以及在某范围大小的文件数量
  9. 树上倍增求LCA详解
  10. 1001 A+B Format (20 分)