[转自:http://haohaoxuexi.iteye.com/blog/2154299]

首先我们为Spring Security专门建立一个Spring的配置文件,该文件就专门用来作为Spring Security的配置。使用Spring Security我们需要引入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了。首先我们来定义一个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”表示请求的用户应当具有ROLE_USER角色。“ROLE_”前缀是一个提示Spring使用基于角色的检查的标记。

有了权限控制的规则了后,接下来我们需要定义一个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配置文件的配置就完成了。完整配置文件将如下所示。

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

之后我们告诉Spring加载这个配置文件。通常,我们可以在web.xml文件中通过context-param把它指定为Spring的初始配置文件,也可以在对应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来加载和初始化的,上述代码中的applicationContext.xml文件就是对应的Spring的配置文件,如果没有可以不用配置。接下来我们还需要在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>

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

因为我们的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(01)——初体验的更多相关文章

  1. 215.Spring Boot+Spring Security:初体验

    [视频&交流平台] SpringBoot视频:http://t.cn/R3QepWG Spring Cloud视频:http://t.cn/R3QeRZc SpringBoot Shiro视频 ...

  2. Spring boot缓存初体验

    spring boot缓存初体验 1.项目搭建 使用MySQL作为数据库,spring boot集成mybatis来操作数据库,所以在使用springboot的cache组件时,需要先搭建一个简单的s ...

  3. Spring MVC + Security 4 初体验(Java配置版)

    spring Version = 4.3.6.RELEASE springSecurityVersion = 4.2.1.RELEASE Gradle 3.0 + Eclipse Neno(4.6) ...

  4. Spring For Android初体验

    Spring For Android是Spring框架的一个扩展,其主要目的在乎简化Android本地应用的开发,这其中包括了你可以使用该项目提供的 RestTemplate来为你的Android客户 ...

  5. Spring Security 01

    环境搭建 maven依赖jar包 <!-- spring-security --> <dependency> <groupId>org.springframewor ...

  6. Spring MVC开发初体验

    1.目标实现Spring MVC : Hello World! 2.工程创建步骤 new : Dynamic Web Project lib引入Spring框架libs/*.jar touch web ...

  7. Spring Cloud Alibaba 初体验(六) Seata 及结合 MyBatis 与 MyBatis-Plus 的使用

    一.下载与运行 本文使用 Seata 1.1.0:https://github.com/seata/seata/releases Windows 环境下双击 bin/seata-server.bat ...

  8. Spring Data JPA 初体验

    一,JPA相关的概念 JPA概述 全称是:JavaPersistence API.是SUN公司推出的一套基于ORM的规范. Hibernate框架中提供了JPA的实现. JPA通过JDK 5.0注解或 ...

  9. spring mvc 之初体验

    Spring MVC最简单的配置 配置一个Spring MVC只需要三步: 在web.xml中配置Servlet: 创建Spring MVC的xml配置文件: 创建Controller和View &l ...

  10. Spring Freamwork 开发初体验

    工具 eclipse 版本:Neon.3 Release (4.6.3) Spring Freamwork 版本:4.0.4.RELEASE 下载地址:http://repo.springsource ...

随机推荐

  1. lucene 索引创建步骤

    一.步骤: 1.存储位置:1)文件: Directory dir= FSDirectory.open(new File("D:\\LuceneIndex")); 2)内存: new ...

  2. finally语句包含return的情况

    结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  3. bootstraptable表格基本

    function tableint(){   $("#tableFromData").bootstrapTable({    url:BASE_URL+"/do/fron ...

  4. Android中截取当前屏幕图片

    该篇文章是说明在Android手机或平板电脑中如何实现截取当前屏幕的功能,并把截取的屏幕保存到SDCard中的某个目录文件夹下面.实现的代码如下: [html] view plaincopy   /* ...

  5. C语言_error_MSB8031

    关于Visual Studio 2013 编译 multi-byte character set MFC程序出现 MSB8031 错误的解决办法 Visual Studio 2013 编译旧的 mul ...

  6. 在线协作沟通工具DesignBoard帮助设计团队更有效地进行沟通与版本管理

    设计稿呈现缺乏整体性.远程沟通效率低.多版本管理混乱,这可能是很多创业团队都面临的问题,并且这些问题都将直接影响到产品开发进度.国内创业团队彩程设计也曾面临同样的问题,后来他们在做某个新项目时尝试把设 ...

  7. 数据库NULL和 ‘’ 区别

    NULL判断时 : IS NOT NULL ''判断时: !=''

  8. PHP数学函数试题

    1.求绝对值的函数是什么? 2.在任意进制之间转换数字的函数是什么? 3.二进制转换为十进制,十进制转换为二进制,十六进制转换为十进制,十进制转换为十六进制,八进制转换为十进制,十进制转换为八进制的函 ...

  9. php中header函数参数的 Cache-control:private,no-cache,must-revalidate,max-age 使用方法

    网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private.no-cache.max-age.must-revalidate等,默认为private.其作用根据 ...

  10. C# 将字符串转为&#2345;这种的 html实体编码

    1.字符串转为html实体编码 private string GetHtmlEntities(string str) { string r = string.Empty; ; i < str.L ...