环境搭建

maven依赖jar包

<!-- spring-security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.2.3.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>4.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>

note: spring security jar的具体解析见https://blog.csdn.net/sun_Leaf/article/details/78954501

applicationContext-security.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd"> <!-- 配置不过滤的资源(静态资源及登录相关).是忽略拦截某些资源的意思,主要是针对静态资源 -->
<http pattern="/**/*.css" security="none"></http>
<http pattern="/**/*.jpg" security="none"></http>
<http pattern="/**/*.jpeg" security="none"></http>
<http pattern="/**/*.gif" security="none"></http>
<http pattern="/**/*.png" security="none"></http>
<http pattern="/js/*.js" security="none"></http> <http pattern="/login.jsp" security="none"></http>
<http pattern="/getCode" security="none" /><!-- 不过滤验证码 -->
<http pattern="/test/**" security="none"></http><!-- 不过滤测试内容 --> <http auto-config="true">
<!-- 表示访问app.jsp时,需要ROLE_SERVICE权限 -->
<intercept-url pattern="/adminpage.jsp" access="hasRole('ROLE_ADMIN')"></intercept-url>
<!--表示访问任何资源都需要ROLE_ADMIN权限。-->
<intercept-url pattern="/**" access="hasRole('ROLE_USER')"></intercept-url>
</http> <authentication-manager>
<authentication-provider>
<!-- 用户的权限控制 -->
<user-service>
<user name="admin" password="123" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="user" password="123" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>

web.xml配置

<!-- 加载配置文件 -->
<context-param>
<!-- 配置文件的路径 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-security.xml</param-value>
</context-param>
<!-- 先由web容器加载为k-v,在通过spring security监听器监听获取 -->
<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>

定义访问页面

  • adminpage.jsp
<html>
<body>
<h2>this is admin page!</h2>
</body>
</html>
  • index.jsp
<html>
<body>
<h2>this is index page!</h2>
</body>
</html>
  • adminpage.jsp,需要具有ROLE_ADMIN权限的用户才能访问

    index.jsp,需要具有ROLE_USER权限的用户才能访问

Spring Security 01的更多相关文章

  1. spring security进阶 使用数据库中的账户和密码认证

    目录 spring security 使用数据库中的账户和密码认证 一.原理分析 二.代码实现 1.新建一个javaWeb工程 2.用户认证的实现 3.测试 三.总结 spring security ...

  2. [转] Spring Security(01)——初体验

    [转自:http://haohaoxuexi.iteye.com/blog/2154299] 首先我们为Spring Security专门建立一个Spring的配置文件,该文件就专门用来作为Sprin ...

  3. Spring Security(01)——初体验

    (注:本文是基于Spring Security3.1.6所写) (注:原创文章,转载请注明出处.原文地址:http://elim.iteye.com/blog/2154299) (注:本文是基于Spr ...

  4. 01 spring security入门篇

    1. 环境搭建 使用SpringBoot搭建开发环境,只需在pom.xml添加如下依赖即可. <?xml version="1.0" encoding="UTF-8 ...

  5. 01 - 快速体验 Spring Security 5.7.2 | 权限管理基础

    在前面SpringBoot 2.7.2 的系列文章中,已经创建了几个 computer 相关的接口,这些接口直接通过 Spring Doc 或 POSTMAN 就可以访问.例如: GET http:/ ...

  6. Spring Security控制权限

    Spring Security控制权限 1,配置过滤器 为了在项目中使用Spring Security控制权限,首先要在web.xml中配置过滤器,这样我们就可以控制对这个项目的每个请求了. < ...

  7. Spring Security笔记:Hello World

    本文演示了Spring Security的最最基本用法,二个页面(或理解成二个url),一个需要登录认证后才能访问(比如:../admin/),一个可匿名访问(比如:../welcome) 注:以下内 ...

  8. Spring Security笔记:自定义Login/Logout Filter、AuthenticationProvider、AuthenticationToken

    在前面的学习中,配置文件中的<http>...</http>都是采用的auto-config="true"这种自动配置模式,根据Spring Securit ...

  9. 浅谈spring security 403机制

    403就是access denied ,就是请求拒绝,因为权限不足 三种权限级别 一.无权限访问 <security:http security="none" pattern ...

随机推荐

  1. git Windows终端安装教程

    1.下载网址:https://gitforwindows.org/ 2.双击压缩包出现: 3.点击下一步后,选择安装路径: 根据自己的需求选择路径 4.选择安装的组件,建议全选 [每一条解析:] Ad ...

  2. Floyd(弗洛伊德)算法(C语言)

    转载:https://blog.csdn.net/qq_35644234/article/details/60875818 Floyd算法的介绍 算法的特点 弗洛伊德算法是解决任意两点间的最短路径的一 ...

  3. python学习笔记(7): 面向对象

    class Foo: #类中的函数 def bar(self): #功能阐述 print('Bar') pass def hello(self,name): print('i am %s' %name ...

  4. pg_controldata - 显示一个 PostgreSQL 集群的控制信息

    SYNOPSIS pg_controldata [ datadir] DESCRIPTION 描述 pg_controldata 打印那些在 initdb 过程中初始化的信息,比如表版本和服务器的区域 ...

  5. python函数传参和返回值注意事项

    函数传参 空参数 定义函数时括号里面没有形参,调用时不用传参. def func(): print('null para.') # 调用 func() 位置传参 规定形参的数量,调用时必须传递相同数量 ...

  6. Shell脚本的fork炸弹

    #!bin/bash#功能:快速消耗计算机资源,致使计算机死机#作者:liusingbon#定义函数名为.(点), 函数中递归调用自己并放入后台执行function . { .|.& };.

  7. 03scikit-learn非监督学习

    In [1]: from sklearn.decomposition import PCA from sklearn.datasets import load_iris pca = PCA(n_com ...

  8. 学习旧岛小程序 (4)封装api 请求

    1.配置基本的 请求路径 和 key config.js const config = { baseUrl: 'http://bl.7yue.pro/v1/', appkey: "" ...

  9. django之创建子应用

    一:子应用 Django的视图编写是放在子应用中的.类似于flask中的视图. 二:创建子应用 例如:在刚才的dj_study项目中,创建一个名字为user的子应用(目录):注意是第一级的dj_stu ...

  10. MyBatis 中的#和$的区别

    #相当于对数据 加上 双引号,$相当于直接显示数据 #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是111,那么解析成sql时的 ...