版权声明:【本文为博主原创文章或收集整理,未经博主允许不得转载】 https://blog.csdn.net/zsq520520/article/details/77880741
在Spring Security实战(一),我把用户信息和权限信息放到了xml文件中,这是为了演示如何使用最小的配置就可以使用Spring Security,而实际开发中,用户信息和权限信息通常是被保存在数据库中的,为此Spring Security也提供了通过数据库获得用户权限信息的方式。本教程将讲解使用数据库管理用户权限。
一 引入相关的jar包
这个用的是mysql数据库和druid开源的jdbc连接池,在项目的pom.xml中引入jar包:
二 定义数据源
第一、新建datasource.properties配置文件:
mysql.database.driverName=com.mysql.jdbc.Driver
mysql.database.url=jdbc:mysql://127.0.0.1:3316/ssecurity?useUnicode=true&characterEncoding=utf8&useSSL=true&allowMultiQueries=true
mysql.database.user=root
mysql.database.password=root
initialSize=50
minIdle=50
maxActive=8500
maxWait=60000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 1 FROM DUAL
#最大分配的对象数
default.max.total=1000
#最大能够保持idel状态的对象数
default.max.idle=200
#当池内没有返回对象时,最大等待时间 1s
default.max.wait=1000
#当调用borrow Object方法时,是否进行有效性检查
default.testonborrow=true
#当调用return Object方法时,是否进行有效性检查
default.testonreturn=true
第二、新建一个applicationContext-dataSource.xml配置数据源,配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="mysqlDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 基本属性 url、user、password -->
<property name="driverClassName" value="${mysql.database.driverName}" />
<property name="url" value="${mysql.database.url}" />
<property name="username" value="${mysql.database.user}" />
<property name="password" value="${mysql.database.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${initialSize}" />
<property name="minIdle" value="${minIdle}"/>
<property name="maxActive" value="${maxActive}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${maxWait}"/>
<property name="validationQuery" value="${validationQuery}" />
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}"/>
<!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
<property name="filters" value="stat"/>
</bean>
</beans>
因为本实战主要将spring security,数据源相关的配置请自行搜索。
三新建applicationContext.xml文件配置,配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 加载配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1"></property>
<property name="ignoreUnresolvablePlaceholders" value="true"></property>
<property name="locations">
<list>
<value>classpath*:datasource.properties</value>
</list>
</property>
</bean>
<!-- 导入其他的模板文件 -->
<import resource="classpath*:applicationContext-security.xml" />
</beans>
四 修改applicationContext-security.xml配置文件
为了从数据库中获取用户权限信息,我们所需要的仅仅是修改配置文件中的authentication-provider部分。修改后如下:
<!-- 默认数据库对用户进行存储 Spring Security默认情况下需要两张表,用户表和权限表。-->
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="mysqlDataSource" />
</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"
xmlns:sec="http://www.springframework.org/schema/security"
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 pattern="/**/*.css" security="none"></http>
<http pattern="/**/*.jpg" security="none"></http>
<http pattern="/**/*.jpeg" security="none"></http>
<http pattern="/**/*.gif" security="none"></http>
<http pattern="/**/*.png" security="none"></http>
<http pattern="/js/*.js" security="none"></http>
<http pattern="/login.jsp" security="none"></http>
<http pattern="/getCode" security="none" /><!-- 不过滤验证码 -->
<http pattern="/test/**" security="none"></http><!-- 不过滤测试内容 -->
<http auto-config="true">
<!-- 表示访问app.jsp时,需要ROLE_SERVICE权限 -->
<intercept-url pattern="/adminPage.jsp" access="hasRole('ROLE_ADMIN')"></intercept-url>
<!--表示访问任何资源都需要ROLE_ADMIN权限。-->
<intercept-url pattern="/**" access="hasRole('ROLE_USER')"></intercept-url>
</http>
<!-- 导入数据源 -->
<beans:import resource="applicationContext-dataSource.xml"></beans:import>
<!-- 默认数据库对用户进行存储 Spring Security默认情况下需要两张表,用户表和权限表。-->
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="mysqlDataSource" />
</authentication-provider>
</authentication-manager>
</beans:beans>
sql数据库中新建表和插入数据
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权限。这和我们上一章中的配置相同,因此本章实例的效果也和上一节完全相同,这里就不再赘述了。
结果请参考实战一的结果。