转: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登录页的更多相关文章

  1. Spring学习笔记--spring+mybatis集成

    前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...

  2. Spring学习笔记--Spring IOC

    沿着我们上一篇的学习笔记,我们继续通过代码学习IOC这一设计思想. 6.Hello类 第一步:首先创建一个类Hello package cn.sxt.bean; public class Hello ...

  3. Spring学习笔记—Spring之旅

    1.Spring简介     Spring是一个开源框架,最早由Rod Johnson创建,并在<Expert One-on-One:J2EE Design and Development> ...

  4. Spring学习笔记--Spring配置文件和依赖注入

    Spring配置文件 1.alias:设置别名,为bean设置别名,并且可以设置多个别名; <!-- 设置别名 --> <alias name="user" al ...

  5. Spring学习笔记——Spring中的BeanFactory与FactoryBean

    BeanFactory BeanFactory是Spring的org.springframework.beans.factory下的一个接口,是Spring IOC所遵守的基本编程规范.他的实现类有D ...

  6. Spring学习笔记--Spring简介

    1.spring:给软件行业带来了春天; 2.spring的理念:spring框架的初衷是使的现有的更加实用,spring不是创造轮子(技术或框架),而是使现有的轮子更好的运转;spring本身是一个 ...

  7. Spring学习笔记——Spring依赖注入原理分析

    我们知道Spring的依赖注入有四种方式,各自是get/set方法注入.构造器注入.静态工厂方法注入.实例工厂方法注入 以下我们先分析下这几种注入方式 1.get/set方法注入 public cla ...

  8. Spring学习笔记--Spring表达式语言SpEL

    Spring3引入了Spring表达式语言(Spring Expression Language,SpEL).SpEL是一种强大的.简洁的装配Bean的方式,它通过运行期执行的表达式将值装配到Bean ...

  9. Spring学习笔记——Spring中lazy-init与abstract具体解释

    Spring的懒载入的作用是为了避免无谓的性能开销,就是当真正须要数据的时候才去运行数据的载入操作.不只在Spring中.我们在实际的编码过程中也应该借鉴这种思想,来提高我们程序的效率. 首先我们看一 ...

随机推荐

  1. [USACO Mar08] 牛跑步

    http://www.cogs.pro/cogs/problem/problem.php?pid=133 ★★★   输入文件:cowjog.in   输出文件:cowjog.out   简单对比时间 ...

  2. POJ 3255 Roadblocks (次短路模板)

    Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS   Memory Limit: 65536K       Descriptio ...

  3. Centos 6 FTP 配置

    How to configure ftp server on centos 6 Posted  krizna  Centos FTP – File transfer protocol is used ...

  4. Java程序运行时的几个区域

    Java运行时涉及到的区域 几个基本概念: 1.Java对象     2.Java方法    3.一个编译好的类,以class文件的形式出现 4.Java的本地方法   5.线程私有和线程共有   一 ...

  5. NightMare2(SCU4527+dijkstra+二分)

    题目链接:http://acm.scu.edu.cn/soj/problem.action?id=4527 题目: 题意:最短路的每条边除了边权之外还会有一个限制(财富,身上带的财富大于这个值则不能通 ...

  6. bzoj 1483 链表启发式合并

    首先我们可以比较容易的在n的时间内算出来开始的答案,我们维护一些链表,分别表示不同的颜色,那么我们在计算答案的时候,只需要扫一遍所有的链表,判断链表相邻两项是否在序列中相邻,不相邻的话肯定在这其中的一 ...

  7. 腻子脚本polyfill

    腻子脚本 具体是指一段可以给老版本浏览器(ie9以前的版本)带来新特性的javascript脚本代码.如轻量级的脚本代码或Modernizr,Modernizr除了能让ie支持html5新元素之外,还 ...

  8. STM32-内存管理

    转载:http://www.cnblogs.com/guozhikai/p/6031904.html #ifndef __MALLOC_H #define __MALLOC_H #include &q ...

  9. ftrace 简介【转】

    转自:http://www.ibm.com/developerworks/cn/linux/l-cn-ftrace/index.html Trace 对于软件的维护和性能分析至关重要,ftrace 是 ...

  10. printk一些技巧【转】

    转自:http://haohetao.iteye.com/blog/1147791 转自:http://blog.csdn.net/wbd880419/article/details/73530550 ...