上一篇博客中,Spring Security教程(一),我把用戶信息和權(quán)限信息放到了xml文件中,這是為了演示如何使用最小的配置就可以使用Spring Security,而實(shí)際開發(fā)中,用戶信息和權(quán)限信息通常是被保存在數(shù)據(jù)庫中的,為此Spring Security也提供了通過數(shù)據(jù)庫獲得用戶權(quán)限信息的方式。本教程將講解使用數(shù)據(jù)庫管理用戶權(quán)限。
一 引入相關(guān)的jar包
這個(gè)例子用的是mysql數(shù)據(jù)庫和c3p0開源的jdbc連接池,在項(xiàng)目的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>
<!-- 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>
二 定義數(shù)據(jù)源
在applicationContext.xml中定義c3p0的數(shù)據(jù)源,配置如下:
<!-- 數(shù)據(jù)源 -->
<beans:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 此為c3p0在spring中直接配置datasource c3p0是一個(gè)開源的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>
因?yàn)楸窘坛讨饕獙pring security,數(shù)據(jù)源相關(guān)的配置就不在這里贅述了,請自行搜索。
三 修改配置文件
為了從數(shù)據(jù)庫中獲取用戶權(quán)限信息,我們所需要的僅僅是修改配置文件中的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>
<!-- 數(shù)據(jù)源 -->
<beans:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 此為c3p0在spring中直接配置datasource c3p0是一個(gè)開源的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>
<!-- 默認(rèn)數(shù)據(jù)庫對用戶進(jìn)行存儲 -->
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"/>
</authentication-provider>
</authentication-manager>
</beans:beans>
四 在mysql數(shù)據(jù)庫中新建表和插入數(shù)據(jù)
Spring Security默認(rèn)情況下需要兩張表,用戶表和權(quán)限表。以下是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);
-- 插入數(shù)據(jù)語句:
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中,我們創(chuàng)建了兩個(gè)用戶admin和user,其中admin擁有ROLE_ADMIN和ROLE_USER權(quán)限,而user只擁有ROLE_USER權(quán)限。這和我們上一章中的配置相同,因此本章實(shí)例的效果也和上一節(jié)完全相同,這里就不再贅述了。
結(jié)果請參考教程一的結(jié)果
微信公眾號關(guān)注:ByteZ,獲取更多學(xué)習(xí)資料
