<authentication-provider> <user-service> <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" /> <user name="user" password="user" authorities="ROLE_USER" /> </user-service> </authentication-provider>
将上述配置代码配置为html
<authentication-provider> <jdbc-user-service data-source-ref="dataSource"/> </authentication-provider>
如今只要再为jdbc-user-service提供一个dataSource就可让Spring Security使用数据库中的权限信息了。在此咱们使用spring建立一个演示用的dataSource实现,这个dataSource会链接到hsqldb数据库,从中获取用户权限信息。[1]spring
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <beans:property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <beans:property name="url" value="jdbc:hsqldb:res:/hsqldb/test"/> <beans:property name="username" value="sa"/> <beans:property name="password" value=""/> </beans:bean>
最终的配置文件以下所示:sql
<?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.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> <http auto-config='true'> <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN" /> <intercept-url pattern="/**" access="ROLE_USER" /> </http> <authentication-manager> <authentication-provider> <jdbc-user-service data-source-ref="dataSource"/> </authentication-provider> </authentication-manager> //链接数据库 <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <beans:property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <beans:property name="url" value="jdbc:hsqldb:res:/hsqldb/test"/> <beans:property name="username" value="sa"/> <beans:property name="password" value=""/> </beans:bean> </beans:beans>
Spring Security默认状况下须要两张表,用户表和权限表。如下是hsqldb中的建表语句:数据库
create table users(1 username varchar_ignorecase(50) not null primary key, password varchar_ignorecase(50) not null, enabled boolean not null ); create table authorities (2 username varchar_ignorecase(50) not null, authority varchar_ignorecase(50) not null, constraint fk_authorities_users foreign key(username) references users(username) ); create unique index ix_auth_username on authorities (username,authority);3
1.users:用户表。包含username用户登陆名,password登录密码,enabled用户是否被禁用三个字段。缓存
其中username用户登陆名为主键。jsp
2.authorities:权限表。包含username用户登陆名,authorities对应权限两个字段。ide
其中username字段与users用户表的主键使用外键关联。url
3.对authorities权限表的username和authority建立惟一索引,提升查询效率spa
Spring Security会在初始化时,从这两张表中得到用户信息和对应权限,将这些信息保存到缓存中。其中users表中的登陆名和密码用来控制用户的登陆,而权限表中的信息用来控制用户登录后是否有权限访问受保护的系统资源。code
咱们在示例中预先初始化了一部分数据:
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');
这个实现和以前将用户和用户权限写在配置文件中明显方便不少。尤为是用户数量过多时。不过这种方法是保持最基本的表结构,也是默认的表结构。最好在理解原理基础上自定义数据库。(方法后续讲到。)