权限管理是我们项目中必不可少的一环,实际项目中我们可以自己设计权限管理模块,也可以使用市面上成熟的权限管理框架,比如 shiro或者 SpringSecurity等,前面已经详细的介绍过了 shiro 的使用,本文开始就给大家详细的来介绍下SpringSecurity的使用。
内容包括

  1. spring+springmvc基于配置的方式详细介绍SpringSecurity
  2. springboot整合SpringSecurity
  3. OAuth2的使用

一、权限管理概念

  权限管理,一般指根据系统设置的安全规则或者安全策略,用户可以访问而且只能访问自己被授权的资源。权限管理几乎出现在任何系统里面,前提是需要有用户和密码认证的系统。
在权限管理的概念中,有两个非常重要的名词:
认证:通过用户名和密码成功登陆系统后,让系统得到当前用户的角色身份。
授权:系统根据当前用户的角色,给其授予对应可以操作的权限资源。

二、完成权限管理需要三个对象

用户:主要包含用户名,密码和当前用户的角色信息,可实现认证操作。
角色:主要包含角色名称,角色描述和当前角色拥有的权限信息,可实现授权操作。
权限:权限也可以称为菜单,主要包含当前权限名称,url地址等信息,可实现动态展示菜单。
:这三个对象中,用户与角色是多对多的关系,角色与权限是多对多的关系,用户与权限没有直接关系,二者是通过角色来建立关联关系的。

三、初识Spring Security

1 Spring Security概念

  Spring Security是spring采用AOP思想,基于servlet过滤器实现的安全框架。它提供了完善的认证机制和方法级的授权功能。是一款非常优秀的权限管理框架。

2 快速入门案例

  入门案例我们是通过spring+springmvc环境来搭建的。所以需要先准备项目环境

2.1环境准备

2.1.1 创建web项目

  通过idea工具创建一个基于maven的web项目

2.1.2 导入相关的依赖

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.1.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.25</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency>
</dependencies>

2.1.3 创建相关配置文件

  相关的配置文件有spring的,springmvc的和log4j.properties
spring:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!-- 配置扫描路径<context:component-scan base-package="com.dpb.security"use-default-filters="true"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" /></context:component-scan>--></beans>

springmvc:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!-- 配置扫描路径--><context:component-scan base-package="com.dpb.security.controller"use-default-filters="false"><context:include-filter type="annotation"expression="org.springframework.stereotype.Controller" /></context:component-scan>
</beans>

log4j.properties

log4j.rootCategory=INFO, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n

2.1.4 web.xml设置

  在web.xml文件中配置spring和SpringMVC容器的加载

<!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 version="2.5" id="WebApp_ID" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name>Archetype Created Web Application</display-name><!-- 初始化spring容器 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- post乱码过滤器 --><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 前端控制器 --><servlet><servlet-name>dispatcherServletb</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServletb</servlet-name><!-- 拦截所有请求jsp除外 --><url-pattern>/</url-pattern></servlet-mapping></web-app>

2.2 SpringSecurity整合

准备好Spring+SpringMVC的环境后我们就可以整合SpringSecurity了

2.2.1.相关jar作用介绍

spring-security-core.jar:核心包,任何SpringSecurity功能都需要此包
spring-security-web.jar:web工程必须,包含过滤器和相关的web安全基础结构代码
spring-security-config.jar:用于解析xml配置文件,用到SpringSecurity的xml配置文件的就要用到此包
spring-security-taglibs.jar:SpringSecurity提供的动态标签库,jsp页面可以用

因为maven项目的依赖传递性,我们在项目中只需要设置 config和taglibs这两个依赖即可

<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-config</artifactId><version>5.1.5.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-taglibs</artifactId><version>5.1.5.RELEASE</version>
</dependency>

2.2.2.过滤器配置

  我们需要在容器启动的时候加载相关的过滤器,所以需要在web.xml中添加如下配置

<!-- 配置过滤器链 springSecurityFilterChain名称固定-->
<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>

2.2.3.SpringSecurity配置文件设置

  单独添加一个SpringSecurity的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:security="http://www.springframework.org/schema/security"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsdhttp://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd"><!--auto-config="true" 表示自动加载SpringSecurity的配置文件use-expressions="true" 使用Spring的EL表达式--><security:http auto-config="true" use-expressions="true"><!--拦截资源pattern="/**" 拦截所有的资源access="hasAnyRole('role1')" 表示只有role1这个角色可以访问资源--><security:intercept-url pattern="/**" access="hasAnyRole(ROLE_USER)"></security:intercept-url></security:http><!-- 设置认证用户来源  noop:SpringSecurity中默认 密码验证是要加密的  noop表示不加密 --><security:authentication-manager><security:authentication-provider><security:user-service><security:user name="zhang" password="{noop}123" authorities="ROLE_USER"></security:user><security:user name="lisi" password="{noop}123" authorities="ROLE_ADMIN"></security:user></security:user-service></security:authentication-provider></security:authentication-manager></beans>

2.2.4.导入SpringSecurity的配置文件

SpringSecurity的配置文件需要加载到Spring容器中,所以可以通过import来导入

<import resource="spring-security.xml"></import>

2.2.5.启动项目测试

  我们通过tomcat插件来启动项目

<plugins><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><port>8080</port> <!-- 访问端口--><path>/</path>    <!-- 访问路径--></configuration></plugin>


搞定~

SpringSecurity超详细入门介绍相关推荐

  1. 推荐系统[一]:超详细知识介绍,一份完整的入门指南,解答推荐系统相关算法流程、衡量指标和应用,以及如何使用jieba分词库进行相似推荐,业界广告推荐技术最新进展

    搜索推荐系统专栏简介:搜索推荐全流程讲解(召回粗排精排重排混排).系统架构.常见问题.算法项目实战总结.技术细节以及项目实战(含码源) 专栏详细介绍:搜索推荐系统专栏简介:搜索推荐全流程讲解(召回粗排 ...

  2. GoJS超详细入门(插件使用无非:引包、初始化、配参数(json)、引数据(json)四步)...

    GoJS超详细入门(插件使用无非:引包.初始化.配参数(json).引数据(json)四步) 一.总结 一句话总结:插件使用无非:引包.初始化.配参数(json).引数据(json)四步. 1.goj ...

  3. arcgis 地图_ArcGIS超详细入门操作:ArcGIS矢量化地图详细步骤

    今天给大家带来的干货是[ArcGIS超详细入门操作:ArcGIS矢量化地图详细步骤],欢迎大家收藏查阅! 在桌面上新建一个文件夹,打开ArcCatalog, "文件"--" ...

  4. TypeScript超详细入门教程(上)

    TypeScript超详细入门教程(上) 01 开篇词:Hello~TypeScript 01 开篇词:Hello~TypeScript 更新时间:2019-10-30 13:49:46 既然我已经踏 ...

  5. Faster RCNN超详细入门 02 网络细节与训练方法

    文章目录 前言 论文结构 Abstract Introduction Related Work Region Proposal Network Experiments Conclusion 网络架构 ...

  6. YOLO 超详细入门02 v2 (含代码及原文)

    文章目录 前言 背景 总结 一.YOLOv2改进之框架 1.1 网络架构 1.2 Batch Normalization 二.YOLOv2改进方法之尺寸相关 2.1 High Resolution C ...

  7. 自学前端设计——【开源骚客】FPGA超详细入门视频教程

    前言 本文基于[开源骚客]FPGA超详细入门视频教程,简单做个笔记 00. FPGA开发软件的安装 Quartus II 13.1 Modelsim Notepad++ Vim 01. 我的第一个FP ...

  8. bert下游_原来你是这样的BERT,i了i了! —— 超详细BERT介绍(三)BERT下游任务...

    原来你是这样的BERT,i了i了! -- 超详细BERT介绍(三)BERT下游任务 BERT(Bidirectional Encoder Representations from Transforme ...

  9. 有幸一睡鸿蒙窍,《鸿蒙之初》超详细内容介绍附带隐藏彩蛋解锁方法

    原标题:<鸿蒙之初>超详细内容介绍附带隐藏彩蛋解锁方法 <鸿蒙之初>超详细内容介绍附带隐藏彩蛋解锁方法 开局选好英雄后,把新手全部接了.但是螃蟹任务一定要注意哦,几率获得两个新 ...

最新文章

  1. SWAP使用情况以及muma介绍
  2. 35+ Top Apache Tomcat Interview Questions And Answers【转】
  3. mysql计算某一天所在周或月的第一天和最后一天
  4. python的subprocess模块执行shell命令
  5. yum安装mysql5.6
  6. [python opencv 计算机视觉零基础到实战] 十二 直方图
  7. Springcloud 高效率本地加Redis双级缓存
  8. [洪流学堂]Hololens修改图标icon
  9. win10子系统编译android,基于win10子系统ijkplayer全量编译(支持所有格式)流程
  10. python参数注解
  11. 支付宝AI大幅提升细粒度图像分类识别精度,一眼看穿万物细微差异
  12. php 音频上传大小限制,WordPress最大上传文件大小限制修改 | Stay Curious
  13. SQL必知必会第五版笔记
  14. 因子分析python代码_用Python实现因子分析
  15. openstack镜像格式转换
  16. ambari mysql 开机自动启动_ambari的服务启动顺序如何设置
  17. ZOJ 3939。规律题
  18. 配置MyBatis Plus 的乐观锁功能
  19. FFT运算的加深理解——FFT的增益
  20. 千寻位置 开发demo_「千寻新姿势」如何接入千寻位置高精度位置服务

热门文章

  1. Pytorch 安装(CPU)
  2. Android美化插件,Android控件美化Shape
  3. 什么是全量数据、增量数据?如何统一一套系统?
  4. 计算机远程桌面连接连接不上,电脑远程桌面时常出现连接不上问题,怎么处理问题...
  5. Linux(一)之相关介绍与安装
  6. linux系统中开机自启的三种方式
  7. OSI七层的基础概念
  8. java web之javascript(js)解析
  9. python自动聊天机器人_用python实现的一个自动聊天的机器人
  10. 美元指数K线图怎么看?