在前面的例子中,我們的用戶都是存放在內存中,現在就來實現spring security基於數據庫的用戶認證功能。
配置數據源dataSource
首先,這裏使用的數據庫連接池爲druid,數據庫爲mysql,所以pom.xml需引入相應的jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
然後再applicationContext.xml中配置數據源連接
<!-- 數據源配置 使用druid連接池 -->
<beans:bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本屬性 url、user、password -->
<!-- -->
<beans:property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8" />
<beans:property name="username" value="root" />
<beans:property name="password" value="123abc" />
<!-- 配置初始化大小、最小、最大 -->
<beans:property name="initialSize" value="1" />
<beans:property name="minIdle" value="1" />
<beans:property name="maxActive" value="20" />
<!-- 配置獲取連接等待超時的時間 -->
<beans:property name="maxWait" value="60000" />
<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒 -->
<beans:property name="timeBetweenEvictionRunsMillis"
value="60000" />
<!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->
<beans:property name="minEvictableIdleTimeMillis"
value="300000" />
<beans:property name="validationQuery" value="SELECT 'x'" />
<beans:property name="testWhileIdle" value="true" />
<beans:property name="testOnBorrow" value="false" />
<beans:property name="testOnReturn" value="false" />
<!-- 打開PSCache,並且指定每個連接上PSCache的大小 -->
<beans:property name="poolPreparedStatements" value="false" />
<beans:property name="maxPoolPreparedStatementPerConnectionSize"
value="20" />
<!-- 配置監控統計攔截的filters -->
<beans:property name="filters" value="stat" />
</beans:bean>
配置authentication-manager屬性
authentication-manager標籤指向一個AunthenticationManager接口的實現類,用來處理認證請求,最簡配置如下:
<authentication-manager alias="authenticationManager">
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"/>
</authentication-provider>
</authentication-manager>
初始化數據庫
這裏使用spring security默認提供的數據庫腳本,在spring security 4.x版本中,腳本命令位於/org/springframework/security/core/userdetails/jdbc/users.ddl.具體內容略作修改如下:
注意:在執行sql腳本之前先創建數據源中指定的數據庫
create table users(username varchar(50) not null primary key,password varchar(500) 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 users VALUES('admin','admin',1);
insert users VALUES('guest','guest',1);
-- 賦予guest權限ROLE_USER
insert authorities VALUES('admin','ROLE_USER');
通過以上配置,就基本實現了簡單的基於數據庫的用戶認證.原來項目中通過內存保存用戶
<user-service>
<user authorities="ROLE_USER" name="guest" password="guest" />
</user-service>
只有guest用戶可以登錄系統。而本次數據庫新增了adminy用戶,並且賦予了ROLE_USER權限,因此原guest用戶不能登錄,只有admin用戶可以登錄。
代碼下載地址 https://github.com/SmallBadFish/spring_security_demo/archive/0.4.0-authentication.zip
問題彙總
dataSource not found的解決辦法
web.xml中去掉spring-security.xml的引用,在applicationContext.xml中引入spring-security.xml