在前面的例子中,我们的用户都是存放在内存中,现在就来实现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