依赖包:

<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的更多相关文章

  1. 快速搭建基于Spring Boot + Spring Security 环境

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1.Spring Security 权限管理框架介绍 简介: Spring Security 提供了基于 ...

  2. Spring boot +Spring Security + Thymeleaf 认证失败返回错误信息

    [Please make sure to select the branch corresponding to the version of Thymeleaf you are using] Stat ...

  3. 215.Spring Boot+Spring Security:初体验

    [视频&交流平台] SpringBoot视频:http://t.cn/R3QepWG Spring Cloud视频:http://t.cn/R3QeRZc SpringBoot Shiro视频 ...

  4. [权限管理系统(四)]-spring boot +spring security短信认证+redis整合

    [权限管理系统]spring boot +spring security短信认证+redis整合   现在主流的登录方式主要有 3 种:账号密码登录.短信验证码登录和第三方授权登录,前面一节Sprin ...

  5. 基于Spring Boot+Spring Security+JWT+Vue前后端分离的开源项目

    一.前言 最近整合Spring Boot+Spring Security+JWT+Vue 完成了一套前后端分离的基础项目,这里把它开源出来分享给有需要的小伙伴们 功能很简单,单点登录,前后端动态权限配 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Spring Boot + Spring Cloud 实现权限管理系统 (Spring Security 版本 )

    技术背景 到目前为止,我们使用的权限认证框架是 Shiro,虽然 Shiro 也足够好用并且简单,但对于 Spring 官方主推的安全框架 Spring Security,用户群也是甚大的,所以我们这 ...

随机推荐

  1. ci总结

    1.创建模型,在模型中加载$this->load->database();2.在模型中写入想实现的功能方法3.在控制器中加载辅助函数和模型,$this->load->model ...

  2. hadoop shell 详解

    概述  所有的hadoop命令均由bin/hadoop脚本引发.不指定参数运行hadoop脚本会打印所有命令的描述.  用法: hadoop [--config confdir] [COMMAND] ...

  3. ABySS 拼接工具

    ABySS, that stands for Assembly By Short Sequences, is a de novo, parallel, paired-end sequence asse ...

  4. Python 新手常犯错误(第二部分)

    转发自:http://blog.jobbole.com/43826/ 在之前几个月里,我教一些不了解Python的孩子来慢慢熟悉这门语言.渐渐地,我发现了一些几乎所有Python初学者都会犯的错误,所 ...

  5. Wamp安装使用+Git for Windows

    相信很多朋友都曾在windows上做过web开发,我们常用的Web应用程序平台是:Apache+Mysql+Perl/PHP/Python,在windows下集成称为WAMP.web开发新手有时候由于 ...

  6. 在Linux上使用web2py_uwsgi_nginx搭建web服务器

    本文介绍在Linux使用Python+Nginx+web2py+uWSGI搭建一个web服务器的过程. Python 2.7.11 解压安装包 tar -zxvf Python-2.7.11.tgz ...

  7. xmind第一天笔记

  8. lable自动适配大小

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  9. c 函数及指针学习 4

    1数组和指针声明的差别 声明数组:为数组分配内存,为数组名分配内存(指针常量 4个字节) 指针:为指针分配内存(指针变量 4个字节) 1 2 3 4 5 6 7 8 9 10 #include < ...

  10. meta是什么意思?

    META标签,是HTML语言head区的一个辅助性标签.在几乎所有的page里,我们都可以看 到类似下面这段html代码: -------------------------------------- ...