spring security 简单入门示例

一、概述

Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架 。

其中最主要的安全操作有两个。

认证:是为用户建立一个他所声明的主体 ,就是完成用户的登录

授权:指的是一个用户能否在应用中执行某个操作。在进行授权之前已经完成了用户的认证。

二、快速入门案例

1.新建一个java web工程

使用idea+maven创建一个java web工程,目录如下

并创建好登录的页面,登录失败的页面,和登录成功的页面,login.html,success.html,failed.html,还有工程的首页index.jsp

2.导入依赖

pom文件的内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.lyy</groupId>
  6. <artifactId>web_03_security_quicklystart</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <name>web_03_security_quicklystart Maven Webapp</name>
  10. <!-- FIXME change it to the project's website -->
  11. <url>http://www.example.com</url>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <spring.version>5.0.2.RELEASE</spring.version>
  15. <spring.security.version>5.0.1.RELEASE</spring.security.version>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-core</artifactId>
  21. <version>${spring.version}</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-web</artifactId>
  26. <version>${spring.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-webmvc</artifactId>
  31. <version>${spring.version}</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-context-support</artifactId>
  36. <version>${spring.version}</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework</groupId>
  40. <artifactId>spring-test</artifactId>
  41. <version>${spring.version}</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework</groupId>
  45. <artifactId>spring-jdbc</artifactId>
  46. <version>${spring.version}</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework.security</groupId>
  50. <artifactId>spring-security-web</artifactId>
  51. <version>${spring.security.version}</version>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.springframework.security</groupId>
  55. <artifactId>spring-security-config</artifactId>
  56. <version>${spring.security.version}</version>
  57. </dependency>
  58. <dependency>
  59. <groupId>javax.servlet</groupId>
  60. <artifactId>javax.servlet-api</artifactId>
  61. <version>3.1.0</version>
  62. <scope>provided</scope>
  63. </dependency>
  64. </dependencies>
  65. <build>
  66. <plugins>
  67. <plugin>
  68. <groupId>org.apache.tomcat.maven</groupId>
  69. <artifactId>tomcat7-maven-plugin</artifactId>
  70. <version>2.1</version>
  71. <configuration>
  72. <port>80</port>
  73. <path>/</path>
  74. <uriEncoding>UTF-8</uriEncoding>
  75. <server>tomcat7</server>
  76. </configuration>
  77. </plugin>
  78. </plugins>
  79. </build>
  80. </project>

3.创建spring security的配置文件

spring-security.xml的内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:security="http://www.springframework.org/schema/security"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/security
  8. http://www.springframework.org/schema/security/spring-security.xsd">
  9. <!--spring-security的入门配置-->
  10. <!--配置哪些资源不会被拦截 /xxx表示根路径下的某个资源-->
  11. <security:http security="none" pattern="/login.html"/>
  12. <security:http security="none" pattern="/failed.html"/>
  13. <security:http auto-config="true" use-expressions="false">
  14. <!-- 配置链接地址,表示任意路径都需要ROLE_USER权限 -->
  15. <security:intercept-url pattern="/**" access="ROLE_USER"/>
  16. <!--自定义登录页面-->
  17. <security:form-login login-page="/login.html" login-processing-url="/login"
  18. username-parameter="username" password-parameter="password"
  19. authentication-failure-forward-url="/failed.html"
  20. default-target-url="/success.html" authentication-success-forward-url="/success.html"
  21. />
  22. <!--关闭csrf,默认是开启的-->
  23. <security:csrf disabled="true"/>
  24. </security:http>
  25. <security:authentication-manager>
  26. <security:authentication-provider>
  27. <!--这里配置了两个用户,分别具有USER和ADMIN的权限-->
  28. <security:user-service>
  29. <security:user name="user" password="{noop}user"
  30. authorities="ROLE_USER"/>
  31. <security:user name="admin" password="{noop}admin"
  32. authorities="ROLE_ADMIN"/>
  33. </security:user-service>
  34. </security:authentication-provider>
  35. </security:authentication-manager>
  36. </beans>

这个配置文件中的主要内容如下:

(1) 配置security不进行权限控制的资源,如登录和失败页面

  1. <!--配置哪些资源不会被拦截 /xxx表示根路径下的某个资源-->
  2. <security:http security="none" pattern="/login.html"/>
  3. <security:http security="none" pattern="/failed.html"/>

(2) 配置任意路径都需要ROLE_USER权限

(3) 配置使用自定义的登录页面

(4) 配置两个用户,分别具有USER和ADMIN的权限

注意配置路径的访问权限时必须带上ROLE_前缀

4. 在web.xml中配置spring security的过滤器

  1. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  4. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. version="3.0">
  6. <display-name>Archetype Created Web Application</display-name>
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:spring-security.xml</param-value>
  10. </context-param>
  11. <listener>
  12. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  13. </listener>
  14. <filter>
  15. <filter-name>springSecurityFilterChain</filter-name>
  16. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  17. </filter>
  18. <filter-mapping>
  19. <filter-name>springSecurityFilterChain</filter-name>
  20. <url-pattern>/*</url-pattern>
  21. </filter-mapping>
  22. <welcome-file-list>
  23. <welcome-file>index.html</welcome-file>
  24. <welcome-file>index.htm</welcome-file>
  25. <welcome-file>index.jsp</welcome-file>
  26. <welcome-file>default.html</welcome-file>
  27. <welcome-file>default.htm</welcome-file>
  28. <welcome-file>default.jsp</welcome-file>
  29. </welcome-file-list>
  30. </web-app>

注意springSecurityFilterChain这个过滤器的名称不能更改

5.启动工程

启动工程,输入localhost进行访问,会出现如下的登录页面

使用user:user和admin:admin这两个账户都可以完成登录,登录成功后会跳转到登录成功页面

需要注意的是:

配置文件中配置的是所有资源都要ROLE_USER权限才能访问,所以如果使用user登录成功后,可以访问到工程中的其他资源,比如首页;但使用admin登录后,因为只有ROLE_ADMIN权限,所以不能访问工程中的其他资源

spring security 简单入门的更多相关文章

  1. Spring Security框架入门

    1.Spring Security框架入门 1.1 Spring Security简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框 ...

  2. spring security简单教程以及实现完全前后端分离

    spring security是spring家族的一个安全框架,入门简单.对比shiro,它自带登录页面,自动完成登录操作.权限过滤时支持http方法过滤. 在新手入门使用时,只需要简单的配置,即可实 ...

  3. spring security简单登录的认证

    一.思路 1.先导入相关配置(使用spring security校验之后,登录拦截的配置) 2.创建一个 WebSecurityConfig 继承 WebSecurityConfigurerAdapt ...

  4. Spring.Net 简单入门学习

    Spring.NET IoC容器的用法. 通过简单的例子学习Spring.Net 1.先创建一个控制台程序项目. 2.添加IUserInfoDal 接口. namespace Spring.Net { ...

  5. spring AOP简单入门

    AOP(aspect oriented programming)面向切面编程. 大致意思是在方法的执行过程中织入其他要执行的方法. 项目结构图 先介绍一下通过代理的方式实现aop,几个文件和上一篇一样 ...

  6. spring IOC简单入门

    spring的核心是ioc和aop 先介绍一下IOC(inverse of control控制反转)又叫DI(Dependency injection依赖注入) 个人理解为把对象的控制权由类转移到配置 ...

  7. spring security 简单应用

    Pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:// ...

  8. Spring AOP 简单入门笔记 (转)

    分享一个自己写的最为简单的Spring AOP的应用,其实,本人也是学习Spring不久,只是把一些个人的理解分享下,供参考.可能很多人刚开始不太理解到底啥是AOP,其实它也是相对 OOP来说的,类似 ...

  9. Spring Cloud简单入门教程

    原文地址:http://www.cnblogs.com/skyblog/p/5127690.html 按照官方的话说:Spring Cloud 为开发者提供了在分布式系统(如配置管理.服务发现.断路器 ...

随机推荐

  1. GUI tkinter (Entry) 输入框篇

    """1.其他函数不常用,这里只说get函数,get函数使用的时候不需要任何参数,它的返回值就是该输入框的内容.""" from tkint ...

  2. 《深入理解Java虚拟机》-----第9章 类加载及执行子系统的案例与实战

    概述 在Class文件格式与执行引擎这部分中,用户的程序能直接影响的内容并不太多, Class文件以何种格式存储,类型何时加载.如何连接,以及虚拟机如何执行字节码指令等都是由虚拟机直接控制的行为,用户 ...

  3. shark恒破解笔记6-BC++假自效验

    这小节介绍了查壳(peid) 查软件编写语言(die)以及用esp定律脱aspack壳,最后是破解bc++的自校验部分 目标: 首先查看软件 peid查壳 有壳 ,但是不知道是什么语言写的,这里使用D ...

  4. Ubuntu php安装xdebug

    1.安装xdebug扩展: sudo apt-get install php-xdebug 2.找到扩展的路径: 3.编辑php.ini文件,末尾加入,保存退出: [xdebug] zend_exte ...

  5. Codeforces 986B - Petr and Permutations

    Description\text{Description}Description Given an array a[], swap random 2 number of them for 3n or  ...

  6. JS中==运行机制

    1. 判断两边是否有NaN,如果有则一律返回false 2.判断两边是否含有布尔值,如果有的话则将true转化为1,false转化为0. 3.遇到null或者undefined,则不会进行类型转换,它 ...

  7. ESP8266开发之旅 网络篇④ Station——ESP8266WiFiSTA库的使用

    1. 前言     在前面的篇章中,博主给大家讲解了ESP8266的软硬件配置以及基本功能使用,目的就是想让大家有个初步认识.并且,博主一直重点强调 ESP8266 WiFi模块有三种工作模式: St ...

  8. CocosCreator中_worldMatrix到底是什么(下)

    Cocos Creator 中 _worldMatrix 到底是什么(下) 1. 摘要 上篇介绍了矩阵的基本知识以及对应图形变换矩阵推倒.中篇具体介介绍了对应矩阵转换成cocos creator代码的 ...

  9. 基础安全术语科普(六)——exploit

    exploit (漏洞利用) 利用漏洞存在两种攻击形式: 1.Remote(远程):利用系统漏洞来获得访问权限. 2.local(本地):需要对系统进行物理访问来实现攻击. 如何发现漏洞? 利用逆向工 ...

  10. 【MongoDB详细使用教程】五、MongoDB的数据库管理

    目录 1.数据库安全 1.1.创建管理员账号和密码 1.2.设置服务状态为需要验证用户 1.3.创建用户账户和密码 1.4.忘记密码/修改密码 2.主从服务器 2.1.创建服务器目录,用于分别存放主从 ...