转:http://axuebin.com/blog/2016/06/21/spring-security/?utm_source=tuicool&utm_medium=referral。
提示:分析applicationContext-security.xml文件,了解登录过程。
本文是介绍了利用Spring Security来完成一个简单的登录页面。

添加Spring Security命名空间
首先需要在Spring的配置文件中弄个添加Spring Security命名空间,考虑到可能会有很多不同的配置文件,我们将Spring Security的配置文件单独建立一个XML文件— applicationContext-security.xml ,并在 applicationContext.xml 中加入如下语句:
<import resource="classpath:applicationContext-security.xml"/>
现在在 applicationContext-security.xml 中添加命名空间:
applicationContext-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-4.2.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-4.1.xsd">
</beans>
代理Servlet过滤器
在Spring Security中,是借助着一系列的Servlet过滤器来提供安全功能,所以就需要引入 <filter> 声明。在Spring中,我们只要简单的配置一个过滤器,就可以实现我们所需要的功能:
web.xml
<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实战(第3版)》中是这样写的:
DelegatingFilerProxy是一个特殊的Servlet过滤器,它将工作委托给一个javax.servlet.Filter实现类,这个实现类作为一个 注册在Spring应用的上下文中。
springSecurityFilterChain本身是一个特殊的过滤题,它可以链接任意一个或多个其它的过滤器,Spring Security依赖一系列Servlet过滤器来提供不同的安全特性。但是,你几乎不需要知道这些细节。
也就是说,我们只需要配置一个这样简单的过滤器就可以使用Spring Security中的相应功能。
简单的登陆页面
我们只需要一个简单的jsp页面,就像这样:
<%@ page contentType="text/html; charset=UTF-8" language="java"%>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>
<html>
<head>
<title></title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
	<div class="container">
		<div class="login">
			<h1>用户登录</h1>
			<form method="POST" action="j_spring_security_check">
				<p><input type="text" name="j_username" value="" placeholder="请输入用户名"></p>
				<p><input type="password" name="j_password" placeholder="请输入密码"></p>
				<p><input class="btn btn-default" name="sumbit" type="submit"  
                     
                    
                 
                    
                