上一篇博客中,Spring Security教程(一):初识Spring Security,我把用户信息和权限信息放到了xml文件中,这是为了演示如何使用最小的配置就可以使用Spring Security,而实际开发中,用户信息和权限信息通常是被保存在数据库中的,为此Spring Security也提供了通过数据库获得用户权限信息的方式。本教程将讲解使用数据库管理用户权限。

一、引入相关的jar包

这个例子用的是mysql数据库和c3p0开源的jdbc连接池,在项目的pom.xml中引入jar包。

  1. <!-- Mysql -->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. <version>5.1.21</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>c3p0</groupId>
  9. <artifactId>c3p0</artifactId>
  10. <version>0.9.1.2</version>
  11. </dependency>

二、定义数据源

在applicationContext.xml中定义c3p0的数据源,配置如下:

  1. <!-- 数据源 -->
  2. <beans:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  3. destroy-method="close">
  4. <!-- 此为c3p0在spring中直接配置datasource c3p0是一个开源的JDBC连接池 -->
  5. <beans:property name="driverClass" value="com.mysql.jdbc.Driver" />
  6.  
  7. <beans:property name="jdbcUrl"
  8. value="jdbc:mysql://localhost:3306/springsecuritydemo?useUnicode=true&characterEncoding=UTF-8" />
  9. <beans:property name="user" value="root" />
  10. <beans:property name="password" value="" />
  11. <beans:property name="maxPoolSize" value="50"></beans:property>
  12. <beans:property name="minPoolSize" value="10"></beans:property>
  13. <beans:property name="initialPoolSize" value="10"></beans:property>
  14. <beans:property name="maxIdleTime" value="25000"></beans:property>
  15. <beans:property name="acquireIncrement" value="1"></beans:property>
  16. <beans:property name="acquireRetryAttempts" value="30"></beans:property>
  17. <beans:property name="acquireRetryDelay" value="1000"></beans:property>
  18. <beans:property name="testConnectionOnCheckin" value="true"></beans:property>
  19. <beans:property name="idleConnectionTestPeriod" value="18000"></beans:property>
  20. <beans:property name="checkoutTimeout" value="5000"></beans:property>
  21. <beans:property name="automaticTestTable" value="t_c3p0"></beans:property>
  22. </beans:bean>

因为本教程主要将spring security,数据源相关的配置就不在这里赘述了,请自行搜索。

三、修改配置文件

为了从数据库中获取用户权限信息,我们所需要的仅仅是修改配置文件中的authentication-provider部分。修改后如下:
  1. <authentication-manager>
  2. <authentication-provider>
  3. <jdbc-user-service data-source-ref="dataSource"/>
  4. </authentication-provider>
  5. </authentication-manager>

配置文件到这部就算修改完毕了,最终配置文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans:beans xmlns="http://www.springframework.org/schema/security"
  3. xmlns:beans="http://www.springframework.org/schema/beans"
  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-3.0.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  9. http://www.springframework.org/schema/tx
  10. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  11. http://www.springframework.org/schema/security
  12. http://www.springframework.org/schema/security/spring-security.xsd">
  13. <http auto-config='true'>
  14. <intercept-url pattern="/adminPage.jsp" access="ROLE_ADMIN" />
  15. <intercept-url pattern="/**" access="ROLE_USER" />
  16. </http>
  17. <!-- 数据源 -->
  18. <beans:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  19. destroy-method="close">
  20. <!-- 此为c3p0在spring中直接配置datasource c3p0是一个开源的JDBC连接池 -->
  21. <beans:property name="driverClass" value="com.mysql.jdbc.Driver" />
  22.  
  23. <beans:property name="jdbcUrl"
  24. value="jdbc:mysql://localhost:3306/springsecuritydemo?useUnicode=true&characterEncoding=UTF-8" />
  25. <beans:property name="user" value="root" />
  26. <beans:property name="password" value="" />
  27. <beans:property name="maxPoolSize" value="50"></beans:property>
  28. <beans:property name="minPoolSize" value="10"></beans:property>
  29. <beans:property name="initialPoolSize" value="10"></beans:property>
  30. <beans:property name="maxIdleTime" value="25000"></beans:property>
  31. <beans:property name="acquireIncrement" value="1"></beans:property>
  32. <beans:property name="acquireRetryAttempts" value="30"></beans:property>
  33. <beans:property name="acquireRetryDelay" value="1000"></beans:property>
  34. <beans:property name="testConnectionOnCheckin" value="true"></beans:property>
  35. <beans:property name="idleConnectionTestPeriod" value="18000"></beans:property>
  36. <beans:property name="checkoutTimeout" value="5000"></beans:property>
  37. <beans:property name="automaticTestTable" value="t_c3p0"></beans:property>
  38. </beans:bean>
  39. <!-- 默认数据库对用户进行存储 -->
  40. <authentication-manager>
  41. <authentication-provider>
  42. <jdbc-user-service data-source-ref="dataSource"/>
  43. </authentication-provider>
  44. </authentication-manager>
  45. </beans:beans>

四、在mysql数据库中新建表和插入数据

Spring Security默认情况下需要两张表,用户表权限表。以下是mysql中的建表语句:

  1. create table users(
  2. username varchar(50) not null primary key,
  3. password varchar(50) not null,
  4. enabled boolean not null
  5. );
  6.  
  7. create table authorities (
  8. username varchar(50) not null,
  9. authority varchar(50) not null,
  10. constraint fk_authorities_users foreign key(username) references users(username)
  11. );
  12.  
  13. create unique index ix_auth_username on authorities (username,authority);

插入数据语句:

  1. insert into users(username,password,enabled) values('admin','admin',true);
  2. insert into users(username,password,enabled) values('user','user',true);
  3.  
  4. insert into authorities(username,authority) values('admin','ROLE_ADMIN');
  5. insert into authorities(username,authority) values('admin','ROLE_USER');
  6. insert into authorities(username,authority) values('user','ROLE_USER');

上述sql中,我们创建了两个用户admin和user,其中admin拥有ROLE_ADMIN和ROLE_USER权限,而user只拥有ROLE_USER权限。这和我们上一章中的配置相同,因此本章实例的效果也和上一节完全相同,这里就不再赘述了。

Spring Security教程(二):通过数据库获得用户权限信息的更多相关文章

  1. Spring Security教程(二):自定义数据库查询

    Spring Security教程(二):自定义数据库查询   Spring Security自带的默认数据库存储用户和权限的数据,但是Spring Security默认提供的表结构太过简单了,其实就 ...

  2. Spring Security教程(二)

    上一篇博客中,Spring Security教程(一),我把用户信息和权限信息放到了xml文件中,这是为了演示如何使用最小的配置就可以使用Spring Security,而实际开发中,用户信息和权限信 ...

  3. Spring Security笔记:使用数据库进行用户认证(form login using database)

    在前一节,学习了如何自定义登录页,但是用户名.密码仍然是配置在xml中的,这样显然太非主流,本节将学习如何把用户名/密码/角色存储在db中,通过db来实现用户认证 一.项目结构 与前面的示例相比,因为 ...

  4. Spring Security教程系列(一)基础篇-1

    第 1 章 一个简单的HelloWorld 第 1 章 一个简单的HelloWorld Spring Security中可以使用Acegi-1.x时代的普通配置方式,也可以使用从2.0时代才出现的命名 ...

  5. Spring Security教程(三):自定义表结构

    在上一篇博客中讲解了用Spring Security自带的默认数据库存储用户和权限的数据,但是Spring Security默认提供的表结构太过简单了,其实就算默认提供的表结构很复杂,也不一定能满足项 ...

  6. Spring Security教程(三)

    在上一篇博客中讲解了用Spring Security自带的默认数据库存储用户和权限的数据,但是Spring Security默认提供的表结构太过简单了,其实就算默认提供的表结构很复杂,也不一定能满足项 ...

  7. 【Spring Security】二、数据库管理用户权限

    一 引入相关的jar包 这个例子用的是mysql数据库和c3p0开源的jdbc连接池,在项目的pom.xml中引入jar包 <!-- Mysql --> <dependency> ...

  8. Spring Security教程(五):自定义过滤器从数据库从获取资源信息

    在之前的几篇security教程中,资源和所对应的权限都是在xml中进行配置的,也就在http标签中配置intercept-url,试想要是配置的对象不多,那还好,但是平常实际开发中都往往是非常多的资 ...

  9. Spring Security教程(八):用户认证流程源码详解

    本篇文章主要围绕下面几个问题来深入源码: 用户认证流程 认证结果如何在多个请求之间共享 获取认证用户信息 一.用户认证流程 上节中提到Spring Security核心就是一系列的过滤器链,当一个请求 ...

随机推荐

  1. CTP交易函数大全

    管理接口 交易接口

  2. MySQL查看当前用户、存储引擎、日志

    #查看MySQL的当前用户 mysql> SELECT USER(); +----------------+ | USER() | +----------------+ | root@local ...

  3. java 文件复制

      java实现文件复制 CreateTime--2017年9月7日15:04:48 Author:Marydon 1.需求 根据原文件复制一份到指定位置 2.代码实现 需要导入: import ja ...

  4. 错误提示:通过 Web 服务器的身份验证的用户无权打开文件系统上的文件

    //win7中iis配置好了可是网页打不开,为什么.? //错误提示:通过 Web 服务器的身份验证的用户无权打开文件系统上的文件 //解决办法1.右键单击你的网站根目录文件夹,如wwwroot文件夹 ...

  5. python之函数用法capitalize()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法capitalize() #capitalize() #说明:将字符串的第一个字母变成 ...

  6. Inno Setup入门(四)——为程序创建桌面快捷方式

    Icons这一可选段定义所有创建在开始菜单和\或其它位置 (比如桌面) 的快捷方式.一个例子如下: [setup] ;全局设置,本段必须 AppName=Test AppVerName=TEST De ...

  7. alias别名使用

    rhel系列的别名使用,方便操作! 功能说明:设置指令的别名.语 法:alias   [别名]  =  [指令名称]参 数 :若不加任何参数,则列出目前所有的别名设置.举    例 :ermao@lo ...

  8. 阿里云ecs配置辅助网卡绑定公网ip地址

    EIP直通车 前置条件:1.大家的实例是从经典迁移到VPC里面的,上古时期,经典实例大家购买实例的时候都是买了带宽的.而这种带宽一般情况下都是包年包月的,而且这种绑定在实例上的IP,我们把它叫做公网I ...

  9. std::bind 详解及参数解析

    // Bind_std_function.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> ...

  10. IP首部格式[转载]

    TCP 传输首部是 IP首部,所以把IP首部格式 拿过来研究下,看IP首部解码过程:   来源:51CTO博客,地址:http://lihuan.blog.51cto.com/4391550/7999 ...