作者:SingleXu
链接:https://www.jianshu.com/p/8212a559d633
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
 简介
Spring Security是Spring生态系统中的一员,提供安全机制的(软件的安全、程序的安全,非硬件安全) Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。 就帮我们解决两个问题 认证:验证用户名和密码是否合法(是否系统中用户),这是第一关;
授权:是系统用户不代表你能使用某些功能,因为你可能没有权限,这是第二关;
Spring Security底层使用的是过滤器,针对url进行的拦截,对应到java中也就是类; 因此被称为粗粒度授权验证,就是验证url,你当前用户有没有这个url的权限。 快速入门
省略父工程创建步骤,见:https://www.jianshu.com/p/8853bc384758 2.1 创建web项目
.png
.png
2.2 导入依赖
图片.png
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mytest_parent</artifactId>
<groupId>com.mytest</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../mytest_parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.</modelVersion> <artifactId>spring_security_01</artifactId> <packaging>war</packaging>
<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port></port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build> <dependencies> <!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency> <!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency> </dependencies> </project>
2.3 配置applicationContext_security.xml, 配置认证和授权信息
图片.png
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
">
<!--
配置拦截的规则
auto-config="使用SpringSecurity自带的页面"
use-expressions="是否使用spel表达式",如果使用表达式:hasRole('ROLE_USER')
-->
<security:http auto-config="true" use-expressions="false">
<!--intercept-url 表示拦截页面
/* 表示的是该目录下的资源,只包括本级目录不包括下级目录
/** 表示的是该目录以及该目录下所有级别子目录的资源
配置拦截的请求地址,任何请求地址都必须有ROLE_USER的权限 -->
<security:intercept-url pattern="/**" access="ROLE_USER"/>
</security:http> <!-- 配置认证信息 -->
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<!--noop是一个标识,表示密码不能明文,密码往往会有很多其他方式,这里默认编码就是不编码-->
<security:user name="admin" password="{noop}admin" authorities="ROLE_USER"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
2.4 配置过滤器
(这里的web.xml和index.jsp文件是手动创建的,名称路径一致即可) 图片.png
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name>
<!--Spring监听器指定配置文件位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext_security.xml</param-value>
</context-param> <!--配置委派代理过滤器-->
<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> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.5测试
打开tomcat插件测试
图片.png
访问localhost/index.jsp页面,如果当前用户没有登录认证的话,则跳转到SpringSecurity的内置登录页面 图片.png
输入配置文件中配置好的账户名admin密码admin,会出现如下404 图片.png 配置文件中增加,对网页缩略图的不拦截后,重启tomcat 图片.png
<security:http pattern="/favicon.ico" security="none"/>
即可访问到index.jsp页面 图片.png
3 配置自定义的登录页面
图片.png
3.1 登录页面login.jsp
<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>xxx系统登录页</title> <style>
div{
width:300px;
height:100px;
position: absolute;
top:50%;
left:50%;
margin-top:-50px;
margin-left:-150px;
}
</style>
</head>
<body>
<div>
<form method="post" action="${pageContext.request.contextPath}/login">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit"/></td>
</tr>
</table>
</form>
</div> </body>
</html>
3.2 成功页面: success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
登录成功,欢迎!
</body>
</html>
3.3 失败页面: error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
登录失败!!!
</body>
</html>
3.4 配置
图片.png 图片.png 图片.png
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
"> <!-- 把登陆页面不拦截 -->
<security:http pattern="/login.jsp" security="none"/>
<security:http pattern="/error.jsp" security="none"/>
<security:http pattern="/favicon.ico" security="none"/> <!--===================授权验证: 验证角色的访问权限 ===================-->
<!--
配置拦截的规则
auto-config="使用SpringSecurity自带的页面"
use-expressions="是否使用spel表达式",如果使用表达式:hasRole('ROLE_USER')
-->
<security:http auto-config="true" use-expressions="false">
<!-- 配置拦截的请求地址,任何请求地址都必须有ROLE_USER的权限 -->
<security:intercept-url pattern="/**" access="ROLE_USER"/>
<!--配置自定义登录相关页面-->
<security:form-login
login-page="/login.jsp"
login-processing-url="/login"
default-target-url="/success.jsp"
authentication-failure-url="/error.jsp"
username-parameter="username"
password-parameter="password"
/>
<!--关闭跨站请求伪造-->
<security:csrf disabled="true"/>
</security:http> <!--=========================授权认证: 校验用户名和密码的合法性====================-->
<!-- 配置认证信息 -->
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<!--noop是一个标识,表示密码不能明文,密码往往会有很多其他方式,这里默认编码就是不编码-->
<security:user name="admin" password="{noop}admin" authorities="ROLE_USER"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
3.5 测试1: 访问登录页面(localhost/login.jsp) 且 用户名和密码正确
http://localhost/login.jsp
图片.png 图片.png
3.6 测试2:访问localhost/index.jsp, 用户名和密码正确:
关闭浏览器重新访问
http://localhost/index.jsp 图片.png 图片.png
3.7 测试3: 访问index.jsp, 用户名或密码错误
http://localhost/index.jsp 图片.png
图片.png
6人点赞
日记本
" 作者:SingleXu
链接:https://www.jianshu.com/p/8212a559d633
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

SpringSecurity快速入门的更多相关文章

  1. spring boot入门教程——Spring Boot快速入门指南

    Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...

  2. Web Api 入门实战 (快速入门+工具使用+不依赖IIS)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...

  3. SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)

     SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...

  4. 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)

    今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...

  5. 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  6. 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  7. Mybatis框架 的快速入门

    MyBatis 简介 什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果 ...

  8. grunt快速入门

    快速入门 Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. Grunt 0.4.x 必须配合Node.js >= 0.8.0版本使用.:奇数版本 ...

  9. 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

随机推荐

  1. bat 获取当前文件夹的文件名

    bat 获取当前文件夹的文件名 @echo off pushd %1 & for %%i in (.) do set curr=%%~ni echo %curr% pause

  2. Debian常见命令总结

    dpkg学习: (1)dpkg是一个较底层的安装工具,处理.deb文件,软件包依赖性需要更高层的前端工具是apt:    dpkg本身是不能解决依赖的.    dpkg是dpkg-deb的前端,如果d ...

  3. nagios监控oracle

    本人最近在弄nagios,想用nagios监控oracle,看了网上的很多教程,步骤都是如下.1.由于 nagios 脚本需要读取 oracle 相关文件.所以运行 nagios 的用户需要定义为 o ...

  4. ACM-ICPC 2017 南宁赛区现场赛 M. The Maximum Unreachable Node Set(二分图)

    题目链接:https://nanti.jisuanke.com/t/19979 题意:给出一个 n 个点,m 条边的 DAG,选出最大的子集使得其中结点两两不能到达. 题解:参考自:https://b ...

  5. 题解 [51nod1607] 卷积和

    题面 解析 神仙LZF随机找出的毒瘤题. 一开始读题过于草率导致\(naive\)了. step 1 看上去特别像数位DP(实际上也有一点). 先预处理出有\(i\)位的数(最高位不为\(0\))的数 ...

  6. 36. ClustrixDB 使用ClustrixDB加密连接

    ClustrixDB使用sha256_password插件支持SSL和身份验证. 一些安全规则要求对存储在数据库中的用户密码进行更强的保护.与默认的mysql_native_password插件相比, ...

  7. JAVA日期格式转换---让人不得不说的故事

    链接:https://my.oschina.net/xinxingegeya/blog/394821 这是给我自己参考的,大家不惜勿喷 1.举例使用 2.各种作用 3.坑(默认中文日期,加上这个就是英 ...

  8. pat 甲级 1034 ( Head of a Gang )

    1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's pho ...

  9. 小米 oj 发奖励(思维)

     发奖励 序号:#75难度:有挑战时间限制:1000ms内存限制:10M 描述 小明老师准备给一些得到小红花的小朋友发糖果做为奖励. 假设有n个小朋友,每个小朋友拥有的小红花为m(n)个,他让这n个小 ...

  10. 【转载】C++ STL priority_queue用法

    priority_queue 对于基本类型的使用方法相对简单.他的模板声明带有三个参数,priority_queue<Type, Container, Functional> Type 为 ...