zoukankan      html  css  js  c++  java
  • 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-provider部分。修改后如下:
    <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权限。这和我们上一章中的配置相同,因此本章实例的效果也和上一节完全相同,这里就不再赘述了。

  • 相关阅读:
    【反射】Java反射机制
    Composer教程之常用命令
    Composer教程之基础用法
    Composer教程之初识Composer
    Composer 的结构详解
    现代 PHP 新特性系列(七) —— 内置的 HTTP 服务器
    现代 PHP 新特性系列(一) —— 命名空间
    现代 PHP 新特性系列(二) —— 善用接口
    现代 PHP 新特性系列(三) —— Trait 概览
    现代 PHP 新特性系列(四) —— 生成器的创建和使用
  • 原文地址:https://www.cnblogs.com/shamo89/p/9982434.html
Copyright © 2011-2022 走看看