Spring Security教程(二):通过数据库获得用户权限信息
上一篇博客中,Spring Security教程(一):初识Spring Security,我把用户信息和权限信息放到了xml文件中,这是为了演示如何使用最小的配置就可以使用Spring Security,而实际开发中,用户信息和权限信息通常是被保存在数据库中的,为此Spring Security也提供了通过数据库获得用户权限信息的方式。本教程将讲解使用数据库管理用户权限。
一、引入相关的jar包
这个例子用的是mysql数据库和c3p0开源的jdbc连接池,在项目的pom.xml中引入jar包。
- <!-- Mysql -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.21</version>
- </dependency>
- <dependency>
- <groupId>c3p0</groupId>
- <artifactId>c3p0</artifactId>
- <version>0.9.1.2</version>
- </dependency>
二、定义数据源
在applicationContext.xml中定义c3p0的数据源,配置如下:
- <!-- 数据源 -->
- <beans:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <!-- 此为c3p0在spring中直接配置datasource c3p0是一个开源的JDBC连接池 -->
- <beans:property name="driverClass" value="com.mysql.jdbc.Driver" />
- <beans:property name="jdbcUrl"
- value="jdbc:mysql://localhost:3306/springsecuritydemo?useUnicode=true&characterEncoding=UTF-8" />
- <beans:property name="user" value="root" />
- <beans:property name="password" value="" />
- <beans:property name="maxPoolSize" value="50"></beans:property>
- <beans:property name="minPoolSize" value="10"></beans:property>
- <beans:property name="initialPoolSize" value="10"></beans:property>
- <beans:property name="maxIdleTime" value="25000"></beans:property>
- <beans:property name="acquireIncrement" value="1"></beans:property>
- <beans:property name="acquireRetryAttempts" value="30"></beans:property>
- <beans:property name="acquireRetryDelay" value="1000"></beans:property>
- <beans:property name="testConnectionOnCheckin" value="true"></beans:property>
- <beans:property name="idleConnectionTestPeriod" value="18000"></beans:property>
- <beans:property name="checkoutTimeout" value="5000"></beans:property>
- <beans:property name="automaticTestTable" value="t_c3p0"></beans:property>
- </beans:bean>
因为本教程主要将spring security,数据源相关的配置就不在这里赘述了,请自行搜索。
三、修改配置文件
- <authentication-manager>
- <authentication-provider>
- <jdbc-user-service data-source-ref="dataSource"/>
- </authentication-provider>
- </authentication-manager>
配置文件到这部就算修改完毕了,最终配置文件如下:
- <?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"
- 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 auto-config='true'>
- <intercept-url pattern="/adminPage.jsp" access="ROLE_ADMIN" />
- <intercept-url pattern="/**" access="ROLE_USER" />
- </http>
- <!-- 数据源 -->
- <beans:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <!-- 此为c3p0在spring中直接配置datasource c3p0是一个开源的JDBC连接池 -->
- <beans:property name="driverClass" value="com.mysql.jdbc.Driver" />
- <beans:property name="jdbcUrl"
- value="jdbc:mysql://localhost:3306/springsecuritydemo?useUnicode=true&characterEncoding=UTF-8" />
- <beans:property name="user" value="root" />
- <beans:property name="password" value="" />
- <beans:property name="maxPoolSize" value="50"></beans:property>
- <beans:property name="minPoolSize" value="10"></beans:property>
- <beans:property name="initialPoolSize" value="10"></beans:property>
- <beans:property name="maxIdleTime" value="25000"></beans:property>
- <beans:property name="acquireIncrement" value="1"></beans:property>
- <beans:property name="acquireRetryAttempts" value="30"></beans:property>
- <beans:property name="acquireRetryDelay" value="1000"></beans:property>
- <beans:property name="testConnectionOnCheckin" value="true"></beans:property>
- <beans:property name="idleConnectionTestPeriod" value="18000"></beans:property>
- <beans:property name="checkoutTimeout" value="5000"></beans:property>
- <beans:property name="automaticTestTable" value="t_c3p0"></beans:property>
- </beans:bean>
- <!-- 默认数据库对用户进行存储 -->
- <authentication-manager>
- <authentication-provider>
- <jdbc-user-service data-source-ref="dataSource"/>
- </authentication-provider>
- </authentication-manager>
- </beans:beans>
四、在mysql数据库中新建表和插入数据
Spring Security默认情况下需要两张表,用户表和权限表。以下是mysql中的建表语句:
- create table users(
- username varchar(50) not null primary key,
- password varchar(50) not null,
- enabled boolean not null
- );
- create table authorities (
- username varchar(50) not null,
- authority varchar(50) not null,
- constraint fk_authorities_users foreign key(username) references users(username)
- );
- create unique index ix_auth_username on authorities (username,authority);
插入数据语句:
- insert into users(username,password,enabled) values('admin','admin',true);
- insert into users(username,password,enabled) values('user','user',true);
- insert into authorities(username,authority) values('admin','ROLE_ADMIN');
- insert into authorities(username,authority) values('admin','ROLE_USER');
- insert into authorities(username,authority) values('user','ROLE_USER');
上述sql中,我们创建了两个用户admin和user,其中admin拥有ROLE_ADMIN和ROLE_USER权限,而user只拥有ROLE_USER权限。这和我们上一章中的配置相同,因此本章实例的效果也和上一节完全相同,这里就不再赘述了。
Spring Security教程(二):通过数据库获得用户权限信息的更多相关文章
- Spring Security教程(二):自定义数据库查询
Spring Security教程(二):自定义数据库查询 Spring Security自带的默认数据库存储用户和权限的数据,但是Spring Security默认提供的表结构太过简单了,其实就 ...
- Spring Security教程(二)
上一篇博客中,Spring Security教程(一),我把用户信息和权限信息放到了xml文件中,这是为了演示如何使用最小的配置就可以使用Spring Security,而实际开发中,用户信息和权限信 ...
- Spring Security笔记:使用数据库进行用户认证(form login using database)
在前一节,学习了如何自定义登录页,但是用户名.密码仍然是配置在xml中的,这样显然太非主流,本节将学习如何把用户名/密码/角色存储在db中,通过db来实现用户认证 一.项目结构 与前面的示例相比,因为 ...
- Spring Security教程系列(一)基础篇-1
第 1 章 一个简单的HelloWorld 第 1 章 一个简单的HelloWorld Spring Security中可以使用Acegi-1.x时代的普通配置方式,也可以使用从2.0时代才出现的命名 ...
- Spring Security教程(三):自定义表结构
在上一篇博客中讲解了用Spring Security自带的默认数据库存储用户和权限的数据,但是Spring Security默认提供的表结构太过简单了,其实就算默认提供的表结构很复杂,也不一定能满足项 ...
- Spring Security教程(三)
在上一篇博客中讲解了用Spring Security自带的默认数据库存储用户和权限的数据,但是Spring Security默认提供的表结构太过简单了,其实就算默认提供的表结构很复杂,也不一定能满足项 ...
- 【Spring Security】二、数据库管理用户权限
一 引入相关的jar包 这个例子用的是mysql数据库和c3p0开源的jdbc连接池,在项目的pom.xml中引入jar包 <!-- Mysql --> <dependency> ...
- Spring Security教程(五):自定义过滤器从数据库从获取资源信息
在之前的几篇security教程中,资源和所对应的权限都是在xml中进行配置的,也就在http标签中配置intercept-url,试想要是配置的对象不多,那还好,但是平常实际开发中都往往是非常多的资 ...
- Spring Security教程(八):用户认证流程源码详解
本篇文章主要围绕下面几个问题来深入源码: 用户认证流程 认证结果如何在多个请求之间共享 获取认证用户信息 一.用户认证流程 上节中提到Spring Security核心就是一系列的过滤器链,当一个请求 ...
随机推荐
- CTP交易函数大全
管理接口 交易接口
- MySQL查看当前用户、存储引擎、日志
#查看MySQL的当前用户 mysql> SELECT USER(); +----------------+ | USER() | +----------------+ | root@local ...
- java 文件复制
java实现文件复制 CreateTime--2017年9月7日15:04:48 Author:Marydon 1.需求 根据原文件复制一份到指定位置 2.代码实现 需要导入: import ja ...
- 错误提示:通过 Web 服务器的身份验证的用户无权打开文件系统上的文件
//win7中iis配置好了可是网页打不开,为什么.? //错误提示:通过 Web 服务器的身份验证的用户无权打开文件系统上的文件 //解决办法1.右键单击你的网站根目录文件夹,如wwwroot文件夹 ...
- python之函数用法capitalize()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法capitalize() #capitalize() #说明:将字符串的第一个字母变成 ...
- Inno Setup入门(四)——为程序创建桌面快捷方式
Icons这一可选段定义所有创建在开始菜单和\或其它位置 (比如桌面) 的快捷方式.一个例子如下: [setup] ;全局设置,本段必须 AppName=Test AppVerName=TEST De ...
- alias别名使用
rhel系列的别名使用,方便操作! 功能说明:设置指令的别名.语 法:alias [别名] = [指令名称]参 数 :若不加任何参数,则列出目前所有的别名设置.举 例 :ermao@lo ...
- 阿里云ecs配置辅助网卡绑定公网ip地址
EIP直通车 前置条件:1.大家的实例是从经典迁移到VPC里面的,上古时期,经典实例大家购买实例的时候都是买了带宽的.而这种带宽一般情况下都是包年包月的,而且这种绑定在实例上的IP,我们把它叫做公网I ...
- std::bind 详解及参数解析
// Bind_std_function.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> ...
- IP首部格式[转载]
TCP 传输首部是 IP首部,所以把IP首部格式 拿过来研究下,看IP首部解码过程: 来源:51CTO博客,地址:http://lihuan.blog.51cto.com/4391550/7999 ...