作者: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. Python: sqlite3模块

    sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块 SQLite 是一个C语言库,它可以提供一种轻量级的基于磁盘的数据库,这种数据库不需要独立的服务器进程,也允许需要使用一种 ...

  2. iOS RAC使用补充

    1  延迟执行 [[RACScheduler mainThreadScheduler] afterDelay: schedule:^{ NSLog(@"延迟执行.."); }]; ...

  3. HDU - 5557 Matching Compressed String (自动机+倍增+表达式计算)

    题意是给你一个自动机和一个字符串的括号表达式,问自动机能否接受这个字符串. 我一想,这不就是个模拟栈计算表达式+倍增么? 再一想,复杂度200*1000*10000*log(1e9),不对啊! 交上去 ...

  4. PHP函数相关知识点

    回调函数 <?php function myfunc($funcname,$name) { $name = "喜欢".$name; $funcname($name); } f ...

  5. ZOJ4019——贪心&&DP

    题目 链接 大意:有一个容量为$c$的背包,有$n$个$s_1$类物体,价值都为$k_1$,体积分别为$s_{1,1}, s_{1,2}, \cdots, s_{1,n}$,有$m$个$s_2$类物体 ...

  6. Java中的集合Collections工具类(六)

    操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集 ...

  7. springboot 详解RestControllerAdvice(ControllerAdvice)(转)

    springboot 详解RestControllerAdvice(ControllerAdvice)拦截异常并统一处理简介 @Target({ElementType.TYPE}) @Retentio ...

  8. MFC、API、C++三者的区别

    MFC(Microsoft Foundation Class)是微软的基础类库,只能用于Windows系统. API(Application Programming Interface)是应用程序编程 ...

  9. 关于pe结构

    每一种操作系统它最重要的格式就是它的可执行文件格式, 因为操作系统就是为了支持这些文件而生成的,内核里面有很多机制,也是配合这种文件格式设计的. 换句话说,这种文件格式也是适合操作系统设计的. 比如: ...

  10. vector 与 array

    vector STL中的模板数组(在堆中分配内存空间,通过new delete管理内存) 使用包涵头文件#include <vector> vector<ElmentTpye> ...