Spring Security 3.0的配置介绍

Spring security是基于spring的一个用来进行身份验证与授权的框架,它提供了一套Web应用安全性的完整的解决方案。

我先简单的谈一下,用Spring security这个框架的好处,虽然说我只在两个项目中应用了Spring Security,但是我觉得好处还是显而易见的:

1.提供了一套可行的便捷的权限框架。

2.提供了大量的身份验证,不需要手动编写过多的验证代码,节约了很多的开发时间与精力。

3.提供了用户角色的判断的功能。

4.提供了很多常见的配置,login-form,rember-me,session过期等等的控制。

当然对于一个非常复杂的权限管理系统,Spring Security这个框架还是有很多的缺点,很多硬伤让它几乎无法应用于这类系统,这个在我们了解基本的Spring Security的使用之后在详细的分析。


以spring-security3.0.7为例

这里可以获取到spring security http://static.springsource.org/spring-security/site/downloads.html

首先说一下spring security的版本,latest已经到了3.1.0,但是按照3.0.x版本之前的东西,3.1.0已经发生很多改动,比如说标签库的使用,所以不用最新的3.1.0进行说明。

web的安全主要包括了两个主要的操作

第一个被称作“认证”,就是为用户建立一个他所声明的主题,这个主体一般是用户、设备或者可以在你系统执行动作的其他系统。

第二个被称作“授权”,就是为指一个用户能否在你创建的应用中执行某一步操作,在到达授权判断之前,身份的主题已经经过了身份验证的过程了。

在身份验证层面,Spring Security广泛支持各种身份验证模式,同时也有自己的一套验证功能。

Spring Security 目前支持认证一体化如下认证技术:

HTTP BASIC authentication headers (一个基于IEFT RFC 的标准)
HTTP Digest authentication headers (一个基于IEFT RFC 的标准)
HTTP X.509 client certificate exchange (一个基于IEFT RFC 的标准)
LDAP (一个非常常见的跨平台认证需要做法,特别是在大环境)
Form-based authentication (提供简单用户接口的需求)
OpenID authentication
Computer Associates Siteminder
JA-SIG Central Authentication Service (CAS,这是一个流行的开源单点登录系统)
Transparent authentication context propagation for Remote Method Invocation and HttpInvoke


一个最简单的例子

解压spring-security-3.0.7.RELEASE

在dist文件夹中取出我们所需要的jar包,只选取非resorces包,导入我们的工程


web.xml配置

服务启动时,需要加载spring security的配置文件,以及spring的配置文件,spring-security的配置文件我们一般用 applicationContext-security.xml进行配置所以在web.xml我们需要写以下代码

 1     <context-param>
2
3 <param-name>contextConfigLocation</param-name>
4
5 <param-value>classpath:applicationContext.xml,
6
7 classpath:applicationContext-security.xml,
8
9 </param-value>
10
11 </context-param>



配置过滤链

在xml配置filter的地方添加如下代码。

1       <filter>
2 <filter-name>springSecurityFilterChain</filter-name>
3 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
4 </filter>
5 <filter-mapping>
6 <filter-name>springSecurityFilterChain</filter-name>
7 <url-pattern>/*</url-pattern>
8 </filter-mapping>



在传统的JavaEE安全性编程模型中,开发者需要在web.xml中表达HTTP安全性需求,如果安全性需求非常复杂而且灵活多变,尤其是要外化在管理它们时,比如借助mysql数据库动态管理安全性体系,则web.xml显然不能够满足此类场景。Spring Security借助过滤器链(javax.servlet.FilterChain)描述和承载HTTP安全性需求。过滤器连是标准JavaEE行为,它由若干过滤器组成,不同的过滤器可以实现不同的HTTP访问控制目的,比如判断HttpSession是否已经创建,当前用户是否创建了其他的HttpSession,所以接触与过滤器链,我们可以实现复杂的HTTP安全性需求。

applicationContext-security进行配置

applicationContext-security是spring-security的配置文件,下面我贴出一个最简单的范例

 1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <beans:beans xmlns="http://www.springframework.org/schema/security"
4 xmlns:beans="http://www.springframework.org/schema/beans"
5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7 http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
8 <http use-expressions="true">
9 <intercept-url pattern="/**" access="permitAll" />
10 <form-login login-page="/index"/>
11 <logout />
12 <remember-me />
13
14 </http>
15
16 <authentication-manager>
17 <authentication-provider>
18 <user-service>
19 <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />
20 <user name="user" password="user" authorities="ROLE_USER" />
21 </user-service>
22 </authentication-provider>
23 </authentication-manager>
24
25 </beans:beans>

这个配置文件的描述主要有以下几个方面配置了常用的用户
19                 <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />  
20 <user name="user" password="user" authorities="ROLE_USER" />
下面几行配置了最基本的http配置
 8     <http use-expressions="true">
9 <intercept-url pattern="/**" access="permitAll" />
10 <form-login login-page="/index"/>
11 <logout />
12 <remember-me />
13
14 </http>
如果我们需要进行加密,我们需要在authentication-manager的authentication-provider中进行配置,比如我们需要进行MD5加密并配置盐值,我们需要编写如下代码在applicationContext-security.xml中
1     <authentication-manager>
2 <authentication-provider>
3 <password-encoder hash="md5"/>
4 <salt-source user-property="saltVal"/>
5 </password-encoder>
6 </authentication-provider>
7 </authentication-manager>
当然我们也可以通过配置数据库连接池,配置数据库作为用户库,进行权限及身份管理,之后我们会说怎么来具体的操作。
最简单的登录页面index.jsp
这个登录页面需要提供一个login-form,也就是之前我们在applicationContext-security.xml中配置的login-form,我实际是吧index.jsp配置成一个用index action访问的页
样例页面的全部代码
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11
12 <title>My JSP 'index.jsp' starting page</title>
13 <meta http-equiv="pragma" content="no-cache">
14 <meta http-equiv="cache-control" content="no-cache">
15 <meta http-equiv="expires" content="0">
16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17 <meta http-equiv="description" content="This is my page">
18 <!--
19 <link rel="stylesheet" type="text/css" href="styles.css">
20 -->
21 </head>
22
23 <body>
24 <form action="<%=path %>/j_spring_security_check" method="post">
25 USERNAME:<input type="text" name="j_username" value="${sessionScope['SPRING_SECURITY_LAST_USERNAME']}" /><br/>
26 PASSWORD:<input type="password" name="j_password" value="" /><br/>
27 <input type="checkbox" name="_spring_security_remember_me" />两周之内不必登陆<br/>
28 <input type="submit">
29 </form>
30
31 <br>
32 </body>
33 </html>

通过上面的配置,我们初步把spring-security整合到项目中,测试一下效果
效果图
启动项目后,会直接进入我们所配置的页面

用我们配置好的用户名密码进行登录,登录成功,大家可以自己测试一下,之后我会跟大家更详细的分享一些spring-security3的使用







转载于:https://www.cnblogs.com/richardhu1991/archive/2011/10/10/2200558.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值