1、Spring Security 的配置文件

我们需要为 Spring Security 专门建立一个 Spring 的配置文件,该文件就专门用来作为 Spring Security 的配置。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd"> <security:http auto-config="true">
<security:intercept-url pattern="/**" access="ROLE_USER"/>
</security:http> <security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="user" password="user" authorities="ROLE_USER"/>
<security:user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager> </beans>

2、Spring Security配置文件分析

2.1、引入 Spring Security 的 NameSpace

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
</beans>

Spring Security 命名空间的引入可以简化我们的开发,它涵盖了大部分 Spring Security 常用的功能。

它的设计是基于框架内大范围的依赖的,可以被划分为以下几块。

  • Web/Http 安全:这是最复杂的部分。通过建立 filter 和相关的 service bean 来实现框架的认证机制。当访问受保护的 URL 时会将用户引入登录界面或者是错误提示界面。
  • 业务对象或者方法的安全:控制方法访问权限的。
  • AuthenticationManager:处理来自于框架其他部分的认证请求。
  • AccessDecisionManager:为 Web 或方法的安全提供访问决策。会注册一个默认的,但是我们也可以通过普通 bean 注册的方式使用自定义的 AccessDecisionManager。
  • AuthenticationProvider:AuthenticationManager 是通过它来认证用户的。
  • UserDetailsService:跟 AuthenticationProvider 关系密切,用来获取用户信息的。

引入了 Spring Security 的 NameSpace 之后我们就可以使用该命名空间下的元素来配置 Spring Security 了。

2.2、定义一个 http 元素  security:http

security 只是我们使用命名空间的一个前缀,http 元素是用于定义 Web 相关权限控制的。

   <security:http auto-config="true">
<security:intercept-url pattern="/**" access="ROLE_USER"/>
</security:http>

如上定义中,

  • intercept-url 定义了一个权限控制的规则。
  • pattern 属性表示我们将对哪些 url 进行权限控制,其也可以是一个正则表达式,如上的写法表示我们将对所有的 URL 进行权限控制;
  • access 属性表示在请求对应的 URL 时需要什么权限,默认配置时它应该是一个以逗号分隔的角色列表,请求的用户只需拥有其中的一个角色就能成功访问对应的 URL。这里的 “ROLE_USER” 表示请求的用户应当具有 ROLEUSER 角色。“ROLE” 前缀是一个提示 Spring 使用基于角色的检查的标记。

2.3、定义一个 AuthenticationManager 用于认证

有了权限控制的规则了后,接下来我们需要定义一个 AuthenticationManager 用于认证。我们先来看如下定义:

   <security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="user" password="user" authorities="ROLE_USER"/>
<security:user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>

authentication-manager 元素指定了一个 AuthenticationManager,

其需要一个 AuthenticationProvider(对应 authentication-provider 元素)来进行真正的认证,

默认情况下 authentication-provider 对应一个 DaoAuthenticationProvider,

其需要 UserDetailsService(对应 user-service 元素)来获取用户信息 UserDetails(对应 user 元素)。

这里我们只是简单的使用 user 元素来定义用户,而实际应用中这些信息通常都是需要从数据库等地方获取的,这个将放到后续再讲。

我们可以看到通过 user 元素我们可以指定 user 对应的用户名、密码和拥有的权限。user-service 还支持通过 properties 文件来指定用户信息,如:

   <security:user-service properties="/WEB-INF/config/users.properties"/>

其中属性文件应遵循如下格式:

username=password,grantedAuthority[,grantedAuthority][,enabled|disabled]

所以,对应上面的配置文件,我们的 users.properties 文件的内容应该如下所示:

#username=password,grantedAuthority[,grantedAuthority][,enabled|disabled]
user=user,ROLE_USER
admin=admin,ROLE_USER,ROLE_ADMIN

至此,我们的 Spring Security 配置文件的配置就完成了。之后我们告诉 Spring 加载这个配置文件。

3、Spring Security的启动过程

3.1、spring-security.xml配置文件加载

通常,我们可以在 web.xml 文件中通过 context-param 把它指定为 Spring 的初始配置文件。

   <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext.xml,/WEB-INF/config/spring-security.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Spring 的配置文件是通过对应的 ContextLoaderListener 来加载和初始化的,上述代码中的spring-security.xml 文件就是对应的 Spring 的配置文件。

3.2、SpringSecurity入口配置:springSecurityFilterChain

接下来我们还需要在 web.xml 中定义一个 filter 用来拦截需要交给 Spring Security 处理的请求,

需要注意的是该 filter 一定要定义在其它如 SpringMVC 等拦截请求之前。

这里我们将拦截所有的请求,具体做法如下所示:

   <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>

3.3、启动应用 - 默认的登录页面(http 元素的 auto-config=”true” 的杰作)

接下来可以启动我们的应用,然后在浏览器中访问我们的主页。你会看到如下页面。

因为我们的 spring-security.xml 文件中配置好了所有的请求都需要 “ROLE_USER” 权限,

所以当我们在请求主页的时候,Spring Security 发现我们还没有登录,Spring 会引导我们到登录界面。

使用正确的用户名和密码(如上面配置的 user/user 或 admin/admin)登录后,如果符合对应的权限我们就可以访问主页了,否则将出现 403(禁止访问)界面。

可能你会奇怪,我们没有建立上面的登录页面,为什么 Spring Security 会跳到上面的登录页面呢?

这是我们设置 http 的 auto-config=”true” 时 Spring Security 自动为我们生成的。

当指定 http 元素的 auto-config=”true” 时,就相当于如下内容的简写。

   <security:http>
<security:form-login/>
<security:http-basic/>
<security:logout/>
</security:http>

这些元素负责建立表单登录、基本的认证和登出处理。它们都可以通过指定对应的属性来改变它们的行为。

Spring Security 入门(1-2)Spring Security - 从 配置例子例子 开始我们的学习历程的更多相关文章

  1. spring boot 入门一 构建spring boot 工程

    最近在学习Spring boot,所以想通过博客的形式和大家分享学习的过程,同时也为了更好的学习技术,下面直接进入Spring boot的世界. 简介 spring boot 它的设计目的就是为例简化 ...

  2. spring Boot 入门--为什么用spring boot

    为什么用spring boot 回答这个问题不得不说下spring 假设你受命用Spring开发一个简单的Hello World Web应用程序.你该做什么? 我能想到一些 基本的需要.  一个项目 ...

  3. Spring Boot入门篇(基于Spring Boot 2.0系列)

    1:概述: Spring Boot是用来简化Spring应用的初始化开发过程. 2:特性: 创建独立的应用(jar|war形式); 需要用到spring-boot-maven-plugin插件 直接嵌 ...

  4. 使用Spring框架入门二:基于注解+XML配置的IOC/DI的使用

    一.简述 本文主要讲使用注解+xml配合使用的几种使用方式.基础课程请看前一节. 二.步骤 1.为Pom.xml中引入依赖:本例中使用的是spring-context包,引入此包时系统会自动导入它的依 ...

  5. Spring框架学习(7)spring mvc入门

    内容源自:spring mvc入门 一.spring mvc和spring的关系 spring mvc是spring框架提供的七层体系架构中的一个层,是spring框架的一部分,是spring用于处理 ...

  6. Spring Security 入门(1-1)Spring Security是什么?

    1.Spring Security是什么? Spring Security 是一个安全框架,前身是 Acegi Security , 能够为 Spring企业应用系统提供声明式的安全访问控制. Spr ...

  7. Spring Security 入门

    一.Spring Security简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配 ...

  8. SpringBoot集成Spring Security入门体验

    一.前言 Spring Security 和 Apache Shiro 都是安全框架,为Java应用程序提供身份认证和授权. 二者区别 Spring Security:重量级安全框架 Apache S ...

  9. Spring Security 入门 (二)

    我们在篇(一)中已经谈到了默认的登录页面以及默认的登录账号和密码. 在这一篇中我们将自己定义登录页面及账号密码. 我们先从简单的开始吧:设置自定义的账号和密码(并非从数据库读取),虽然意义不大. 上一 ...

  10. Spring Security 入门(一)

    当你看到这篇文章时,我猜你肯定是碰到令人苦恼的问题了,我希望本文能让你有所收获. 本人几个月前还是 Spring 小白,几个月走来,看了 Spring,Spring boot,到这次的 Spring ...

随机推荐

  1. 图之单源Dijkstra算法、带负权值最短路径算法

    1.图类基本组成 存储在邻接表中的基本项 /** * Represents an edge in the graph * */ class Edge implements Comparable< ...

  2. Git - 可视化冲突解决工具P4Merge

    P4Merge P4Merge是Git的一个第三发Diff和Merge工具(可视化冲突解决工具). 下载地址: https://www.perforce.com/downloads/visual-me ...

  3. IPFS和竞争对手们(二)

    在上一篇<IPFS和竞争对手们(一)>的开头抱怨的IPFS的开发进度,今天才得知 MaidSafe 是从2006年起步的( )这进度也是醉了. 接上一篇,今天继续讲IPFS的竞争对手们 图 ...

  4. 转载NodePort,LoadBalancer还是Ingress?我该如何选择 - kubernetes

    原文:http://mp.weixin.qq.com/s/dHaiX3H421jBhnzgCCsktg ClusterIP ClusterIP服务是Kuberntets的默认服务.它在集群内部生成一个 ...

  5. TCP和UDP协议的区别

    TCP和UDP都是传输层的协议 UDP协议的特点: UDP协议是一种无连接的.不可靠的传输层协议(尽力而为的协议) 为什么说UDP是一种无连接.不可靠的协议呢?UDP协议在传输报文之前不需要在双方之间 ...

  6. MYSQL数据库学习十三 使用MySQL常用函数

    13.1 字符串函数 对于针对字符串位置的操作,第一个位置被标记为1. 函数 功能 CONCAT(str1,str2...strn) 连接字符串str1.str2....strn INSERT(str ...

  7. 20个JS正则表达式

    1 . 校验密码强度密码的强度必须是包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间. ^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$ 2. 校验中文 ...

  8. nuxt 运行项目后 中总是报错

    报错的内容: nuxt.config.js中的图片 如果出现第一张图的中的错误: 请注释掉第二张图中extend里面的loader:'eslit-loader',或者把extend里面if下面的内容全 ...

  9. The Beginning of the Graph Theory

    The Beginning of the Graph Theory 是的,这不是一道题.最近数论刷的实在是太多了,我要开始我的图论与树的假期生活了. 祝愿我吧??!ShuraK...... poj18 ...

  10. Python中的SQLAlchemy

    在Python中,使用SQLAlchemy可以对数据库进行操作. SQLAlchemy是Python中的一个标准库. 要使用SQLAlchemy,首先要创建连接: url = mysql+pymysq ...