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

这样一个简单的登录页面就完成了。注意在具体实现中,不可能将代码存储在该文件中。

最新文章

  1. 使用php-cs-fixer格式化你的代码
  2. nodejs+sequelize操作mysql数据库
  3. [Android] 环境配置之正式版Android Studio 1.0
  4. [深入Python]sys.modules
  5. 复杂对象的本地化(以Person为例)
  6. iOS prototype Cells
  7. Inno Setup 打包工具总结
  8. uboot start.S分析
  9. COJ 2110 Day7-例3
  10. HDU 1240 Asteroids!
  11. 一个基于Asp.net MVC的博客类网站开源了!
  12. Web服务器(Apache)与Servlet容器(Tomcat)
  13. spring整合junit进行测试
  14. Asp.net MVC 权限过滤器实现方法的最佳实践
  15. Web重温系列(三):OracleDependency实现监听数据库变化
  16. LeetCode--006--Z字型变换(java)
  17. 金币(NOIP2015)
  18. JavaScript语法详解:运算符和表达式
  19. App store 应用审核由于 IPv6 网络问题被拒的一点分析
  20. hammer使用: 代码:捏合、捏开、图片放大 的一个手机图片“放大缩小可拖动”的小效果

热门文章

  1. java mysql 连接
  2. Hexo&amp;Github-Pages搭建个人博客
  3. Linux中 设置apache,mysql 开机启动
  4. 【Foreign】Bumb [模拟退火]
  5. Achain 钱包部署
  6. UIPageControl---iOS-Apple苹果官方文档翻译
  7. http状态响应码对照表
  8. 003 CopyOnWriteArrayList原理
  9. 【NOIP2016】组合数问题
  10. vue-router 基础