转:Spring学习笔记---Spring Security登录页
转: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" value="登陆"></p>
</form>
</div>
</div>
</body>
</html>
注意其中的 form 表单, j_spring_security_check , j_username , j_password 这些都是我们会用到的。
在 applicationContext-security.xml 中,需要这样配置:
<http auto-config="true" use-expressions="true">
<headers>
<frame-options policy="SAMEORIGIN" />
</headers>
<csrf disabled="true" />
<form-login login-page="/login.jsp" default-target-url="/index.html" login-processing-url="/j_spring_security_check" authentication-failure-url="/login.jsp?error" username-parameter="j_username" password-parameter="j_password" />
<intercept-url pattern="/login.jsp" access="permitAll()" />
</http>
登录前的页面是login.jsp,登录后的页面是index.html,如果账号密码错误则跳转到 login.jsp?error ,由于我们没设置就默认是跳到login.jsp了。
用户管理
applicationContext-security.xml
<authentication-manager>
<authentication-provider>
<user-service>
<user name="xb" password="hahaha" authorities="ROLE_USER,ROLE_ADMIN" />
<user name="admin" password="123456" authorities="ROLE_ADMIN" />
</user-service>
</authentication-provider>
</authentication-manager>
这样一个简单的登录页面就完成了。注意在具体实现中,不可能将代码存储在该文件中。
转:Spring学习笔记---Spring Security登录页的更多相关文章
- Spring学习笔记--spring+mybatis集成
前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...
- Spring学习笔记--Spring IOC
沿着我们上一篇的学习笔记,我们继续通过代码学习IOC这一设计思想. 6.Hello类 第一步:首先创建一个类Hello package cn.sxt.bean; public class Hello ...
- Spring学习笔记—Spring之旅
1.Spring简介 Spring是一个开源框架,最早由Rod Johnson创建,并在<Expert One-on-One:J2EE Design and Development> ...
- Spring学习笔记--Spring配置文件和依赖注入
Spring配置文件 1.alias:设置别名,为bean设置别名,并且可以设置多个别名; <!-- 设置别名 --> <alias name="user" al ...
- Spring学习笔记——Spring中的BeanFactory与FactoryBean
BeanFactory BeanFactory是Spring的org.springframework.beans.factory下的一个接口,是Spring IOC所遵守的基本编程规范.他的实现类有D ...
- Spring学习笔记--Spring简介
1.spring:给软件行业带来了春天; 2.spring的理念:spring框架的初衷是使的现有的更加实用,spring不是创造轮子(技术或框架),而是使现有的轮子更好的运转;spring本身是一个 ...
- Spring学习笔记——Spring依赖注入原理分析
我们知道Spring的依赖注入有四种方式,各自是get/set方法注入.构造器注入.静态工厂方法注入.实例工厂方法注入 以下我们先分析下这几种注入方式 1.get/set方法注入 public cla ...
- Spring学习笔记--Spring表达式语言SpEL
Spring3引入了Spring表达式语言(Spring Expression Language,SpEL).SpEL是一种强大的.简洁的装配Bean的方式,它通过运行期执行的表达式将值装配到Bean ...
- Spring学习笔记——Spring中lazy-init与abstract具体解释
Spring的懒载入的作用是为了避免无谓的性能开销,就是当真正须要数据的时候才去运行数据的载入操作.不只在Spring中.我们在实际的编码过程中也应该借鉴这种思想,来提高我们程序的效率. 首先我们看一 ...
随机推荐
- Linux系统iptables查看、设置、保存、备份和恢复
不同linux系统,相关软件是否安装,会让iptables的某些命令不能执行,这里收集了大多数iptables命令,不管是Ubuntu还是Centos,都能找到相关的修改.查询.保存命令. 仅允许某些 ...
- [技巧篇]05.关于eclipse模版
<?xml version="1.0" encoding="UTF-8" standalone="no"?><templa ...
- hdu4085 Peach Blossom Spring
Peach Blossom Spring http://acm.hdu.edu.cn/showproblem.php?pid=4085 Time Limit: 10000/5000 MS (Java/ ...
- bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍 ——中位数排序
Description 经过多年的积蓄,农夫JOHN决定造一个新的牛舍.他知道所有N(2 <= N <= 10,000)头牛的吃草位置,所以他想把牛舍造在最方便的地方. 每一头牛吃草的位置 ...
- B. Complete the Word(Codeforces Round #372 (Div. 2)) 尺取大法
B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- base--AuditObject
//参考base-4.0.2.jarpublic class AuditObject extends HashMap<String, Object> implements TimeRefe ...
- C++学习之路(七):以const,enum,inline替换#define
这篇博文主要是编程中的一些问题和技巧.如题目所示,这些关键字的作用不再进行描述.直接描述功能和实例代码. 首先,在头文件中对类进行定义,是不会为类分配内存空间的,在这一点上类定义可以和普通变量类型的声 ...
- 对list对象进行排序
List<LjlSevOrdersVO> list = ljlSevOrdersService.findSevForOrders(ljlSevOrdersVO); //查出所有是自愿者的订 ...
- golang相关问题
[转载][翻译]Go的50坑:新Golang开发者要注意的陷阱.技巧和常见错误[1] Golang作为一个略古怪而新的语言,有自己一套特色和哲学.从其他语言转来的开发者在刚接触到的时候往往 ...
- 刷题中熟悉Shell命令之Tenth Line和Transpose File [leetcode]
首先介绍题目中要用的4个Shell命令 sed awk head tail的常用方法.(打好地基,才能建成高楼!) sed:(转自:http://www.cnblogs.com/barrychiao/ ...