[spring security] spring security 4 基础Demo
依赖包:
<properties> <junit.version>4.11</junit.version>
<spring.version>4.1.6.RELEASE</spring.version> <spring-security.version>4.0.3.RELEASE</spring-security.version> <mysql.version>5.1.6</mysql.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency> <!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency> <!-- spring security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring-security.version}</version>
</dependency> <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring-security.version}</version>
</dependency> <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring-security.version}</version>
</dependency> <!-- mysql Driver --> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency> <dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> </dependencies>
1.web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 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>
<!-- Spring MVC -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
2.spring-security.xml:
<import resource="classpath:xwolf-datasource.xml"/>
<!--静态资源过滤-->
<security:http pattern="/**/*.css" security="none"></security:http>
<security:http pattern="/**/*.js" security="none"></security:http>
<security:http pattern="/**/*.jpg" security="none"></security:http>
<security:http pattern="/**/*.gif" security="none"></security:http>
<security:http pattern="/**/*.png" security="none"></security:http> <!-- 1.基础xml方式实现 -->
<security:http auto-config="true">
<security:intercept-url pattern="/admin.htm" access="hasRole('ROLE_USER')" />
</security:http> <security:authentication-manager> <security:authentication-provider>
<!-- 1.基础用户 <security:user-service>
<security:user name="admin" password="123456" authorities="ROLE_USER"/>
</security:user-service> --> <!-- 2.数据库查询用户-->
<security:jdbc-user-service data-source-ref="dataSource" users-by-username-query="select name,pwd,status as enabled from user where name=? "
authorities-by-username-query="select u.name ,r.auth as authority from user u join user_role ur on u.uid=ur.uid join role r on r.rid=ur.rid
where u.name=?"/>
</security:authentication-provider>
</security:authentication-manager>
jdbc中用到的用户和权限认证表信息。
其中用到的表结构:
两个页面分别对应hello.htm和admin.htm:
每一个页面一句话:
hello.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<h3>登录成功!Hello World!</h3>
</body>
</html>
admin.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>admin</title>
</head>
<body>
<h2>admin!</h2>
</body>
</html>
[spring security] spring security 4 基础Demo的更多相关文章
- 快速搭建基于Spring Boot + Spring Security 环境
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.Spring Security 权限管理框架介绍 简介: Spring Security 提供了基于 ...
- Spring boot +Spring Security + Thymeleaf 认证失败返回错误信息
[Please make sure to select the branch corresponding to the version of Thymeleaf you are using] Stat ...
- 215.Spring Boot+Spring Security:初体验
[视频&交流平台] SpringBoot视频:http://t.cn/R3QepWG Spring Cloud视频:http://t.cn/R3QeRZc SpringBoot Shiro视频 ...
- [权限管理系统(四)]-spring boot +spring security短信认证+redis整合
[权限管理系统]spring boot +spring security短信认证+redis整合 现在主流的登录方式主要有 3 种:账号密码登录.短信验证码登录和第三方授权登录,前面一节Sprin ...
- 基于Spring Boot+Spring Security+JWT+Vue前后端分离的开源项目
一.前言 最近整合Spring Boot+Spring Security+JWT+Vue 完成了一套前后端分离的基础项目,这里把它开源出来分享给有需要的小伙伴们 功能很简单,单点登录,前后端动态权限配 ...
- 255.Spring Boot+Spring Security:使用md5加密
说明 (1)JDK版本:1.8 (2)Spring Boot 2.0.6 (3)Spring Security 5.0.9 (4)Spring Data JPA 2.0.11.RELEASE (5)h ...
- 256.Spring Boot+Spring Security: MD5是加密算法吗?
说明 (1)JDK版本:1.8 (2)Spring Boot 2.0.6 (3)Spring Security 5.0.9 (4)Spring Data JPA 2.0.11.RELEASE (5)h ...
- Spring Boot+Spring Security:获取用户信息和session并发控制
说明 (1)JDK版本:1.8(2)Spring Boot 2.0.6(3)Spring Security 5.0.9(4)Spring Data JPA 2.0.11.RELEASE(5)hiber ...
- Spring Boot + Spring Cloud 实现权限管理系统 (Spring Security 版本 )
技术背景 到目前为止,我们使用的权限认证框架是 Shiro,虽然 Shiro 也足够好用并且简单,但对于 Spring 官方主推的安全框架 Spring Security,用户群也是甚大的,所以我们这 ...
随机推荐
- 【题解】【链表】【Leetcode】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- JAVA 打印指定月份日历
package learnExercise; import java.util.Scanner; public class PrintCalender { /** * @param args */ p ...
- winrar在右键菜单上加上:打包自动加上日期时间标签【图文教程】 - imsoft.cnblogs
说明: 注册表HKEY_CURRENT_USER\Software\WinRAR\Profiles\0找到GenerateArcName修改0为1,修改GenerateMask为你想要的日期式(默认 ...
- C#堆栈讲解
1:栈就是堆栈,因为堆和堆栈这样说太拗口了,搞得像绕口令,所以有些时候就把堆栈简称为栈.堆和栈,你看这又多舒服.但无论什么时候,堆栈都不等于堆和栈,必须说,堆和栈或者堆和堆栈. 2:值类型变量和引用类 ...
- jsoup的elements类
jsoup的Elements类 一.简介 该类是位于select包下,直接继承自Object,所有实现的接口有Cloneable, Iterable<Element>, Collectio ...
- my Highcharts
1. a=a || {}; 意义:如果a具有真值(不是undefined,null,NAN,false,0中的任意一种),则这个a可以被使用,否则将a定义为一个空的object对象{} 2. a | ...
- API中FileReader 接口事件
FileReader 接口包含了一套完整的事件模型,用于捕获读取文件时的状态onabort 中断onerror 出错onloadstart 开始onprogress ...
- 《Java程序设计》第二次实验报告
20145220 <Java程序设计>第二次实验报告 课程:Java程序设计 指导教师:娄嘉鹏 实验日期:2016.04.12 实验名称:Java面向对象程序设计 实验内容 初步掌握单元测 ...
- poj3159 最短路(差分约束)
题意:现在需要分糖果,有n个人,现在有些人觉得某个人的糖果数不能比自己多多少个,然后问n最多能在让所有人都满意的情况下比1多多少个. 这道题其实就是差分约束题目,根据题中给出的 a 认为 b 不能比 ...
- 这个代码怎么改??Help快速排序 quicksort
#include<stdio.h>int a[101],n;void quicksort(int left,int right){ int i,j,t,temp; if(l ...