技术标签: spring SPRING系列 SpringSecurity
权限管理
是我们项目中必不可少的一环,实际项目中我们可以自己设计权限管理模块,也可以使用市面上成熟的权限管理框架,比如 shiro
或者 SpringSecurity
等,前面已经详细的介绍过了 shiro 的使用,本文开始就给大家详细的来介绍下SpringSecurity的使用。
内容包括
权限管理,一般指根据系统设置的安全规则或者安全策略,用户可以访问而且只能访问自己被授权的资源。权限管理几乎出现在任何系统里面,前提是需要有用户和密码认证的系统。
在权限管理的概念中,有两个非常重要的名词:
认证
:通过用户名和密码成功登陆系统后,让系统得到当前用户的角色身份。
授权
:系统根据当前用户的角色,给其授予对应可以操作的权限资源。
用户
:主要包含用户名,密码和当前用户的角色信息,可实现认证操作。
角色
:主要包含角色名称,角色描述和当前角色拥有的权限信息,可实现授权操作。
权限
:权限也可以称为菜单,主要包含当前权限名称,url地址等信息,可实现动态展示菜单。
注
:这三个对象中,用户与角色是多对多的关系,角色与权限是多对多的关系,用户与权限没有直接关系,二者是通过角色来建立关联关系的。
Spring Security是spring采用AOP
思想,基于servlet过滤器
实现的安全框架。它提供了完善的认证机制和方法级的授权功能。是一款非常优秀的权限管理框架。
入门案例我们是通过spring+springmvc环境来搭建的。所以需要先准备项目环境
通过idea工具创建一个基于maven的web项目
<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>
相关的配置文件有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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://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.xsd
http://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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://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.xsd
http://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, stdout
log4j.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
在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/javaee
http://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>
准备好Spring+SpringMVC的环境后我们就可以整合SpringSecurity了
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>
我们需要在容器启动的时候加载相关的过滤器,所以需要在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>
单独添加一个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/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://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.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://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>
SpringSecurity的配置文件需要加载到Spring容器中,所以可以通过import来导入
<import resource="spring-security.xml"></import>
我们通过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>
搞定~
下边的文章是我遇到的总结sql知识点,并且做成了思维导图做的非常全的。推荐大家点击看。https://blog.csdn.net/Dream_Weave/article/details/83590808 还有一篇是mysql基础的sql语句的思维导图,也很不错。https://blog.csdn.net/slh2016/article/details/5314880...
机器学习和深度学习有什么区别?让我们从本文中寻找答案。目标本文中,我们将深度学习与机器学习作比较。我们将逐一了解他们。我们还会讨论他们在各个方面的不同点。除了深度学习和机器学习的比较,我们还将研究它们未来的趋势。对比介绍深度学习和机器学习一. 什么是机器学习?通常,为了实现人工智能,我们会使用机器学习。我们有几种用于机器学习的算法。例如: Find-S 决策...
几个ffmpeg 测试命令将264裸流文件封装成mp4文件ffmpeg -f h264 -i data.264 -vcodec copy out.mp4使用ffmpeg自带的examples 编码脚本其中576 和1024是宽、高./doc/examples/vaapi_encode 576 1024 f21067050654.nv21 out.264另外,vaapi_encode编译的方法是 configure执行之后,直接make examples使用vaapi进行硬转码
1.数据预处理的步骤。数据预处理有四个步骤:数据清理、数据集成、数据规约、数据变换。数据清理: 现实世界的数据一般是不完整的、有噪声的盒不一致的。数据清理例程试图填充缺失的值、光滑噪声并识别离群点、纠正数据中的不一致。...
一、选择题1.B;2.D;3.AC;4.AC;5.C三、编程题/** * 1. 编写 Java 程序用于显示人的姓名和年龄。 * 定义一个人类Person。 * 该类中应该有两个私有属性: 姓名 (name) 和年龄 (age) 。 * 定义构造方法用来初始化数据成员。 * 再定义显示(display()) 方法将姓名和年龄打印出来。 * 在 main 方法中创建人类的实例然...
argparse模块argparse是python用于解析命令行参数和选项的标准模块,用于代替已经过时的optparse模块使用步骤import argparse # 导入模块,这个没什么说的parser = argparse.ArgumentParser() # 实例化一个对象,默认参数一堆,只有description参数可以设置一下parser.add_argument() ...
介绍一本发布在Pytorch官网上的深度学习开源书,《Deep Learning with PyTorch》,上面共有五个章节,包括了深度学习与PyTorch库、张量、如何用张量表示真实世界的数据、学习机制、用神经网络拟合数据等方面的内容,据称核心是指导读者使用Python 和 PyTorch 实现深度学习算法。著者信息Eli Stevens,软件工程师,过去7年担任一开发...
1、使用idea打开项目 2、选择项目,选中之后项目文件会变红 选中之后项目文件会变红3、右键项目选择git---》add,add之后项目文件会变绿add之后项目文件会变绿 4、点击idea右边的对钩√,默认会选中所有文件,添加注释,选择commit and push 5、确定commit and push 6、复制粘贴git地址7、直接push即可,去Git仓库查看是否上传成功
1、不使用adb命令android studio是识别不出来海马玩模拟器的,打开cmd,输入:adb connect 127.0.0.1:26944。如下:C:\Users\Administrator>adb connect 127.0.0.1:26944connected to 127.0.0.1:2694412这样就代表连接上了。我用的是0.10.3的,端口号是269...
简介使用Python jieba / wordcloud 等对聊天记录进行整理和分析,并生成词云聊天记录准备和提取QQ导出聊天记录在QQ聊天框的消息记录中,打开消息管理,右击打开导出消息记录,选择txt形式打开大概是这个亚子正则表达式提取消息文本发现里面有空行,有消息日期昵称等,而且并不是你一句我一句,所以用正则表达式进行提取首先读入文件(注意utf-8中文...
今天在网上闲逛,无意间发现了这一篇好文,原文作者是PayPal高级工程总监Anil Madan,文章对当前大数据领域用到的一些技术、框架等都做了一遍梳理。通过阅读本文,可以对当前大数据领域有一个很好的认识,如果需要深入了解某项技术,可以阅读文章中所给的文章或论文的相关链接,都是不可多得的好资源。开源(Open Source)用之于大数据技术,其作用有二:一方面,在大数据技术变革之路上,开源
HAL中断方式实现串口通信