本篇文章用户和权限用数据库存储,而资源(url)和权限的对应采用硬编码配置在XML中实现的。
在本篇中需要把用户和权限信息存到数据库中,本例子采用mysql数据库,数据库表如下:
CREATE TABLE users( username VARCHAR(50) NOT NULL, password VARCHAR(50) NOT NULL, enabled BOOLEAN NOT NULL, PRIMARY KEY (username) )ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE authorities ( username VARCHAR(50) NOT null, authority VARCHAR(50) NOT null, CONSTRAINT fk_authorities_users FOREIGN KEY(username) REFERENCES users(username) )ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE UNIQUE INDEX ix_auth_username ON authorities (username,authority); INSERT INTO users(username,password,enabled) values('admin','21232f297a57a5a743894a0e4a801fc3',1); INSERT INTO users(username,password,enabled) values('user','ee11cbb19052e40b07aac0ca060c23ee',1); INSERT INTO authorities VALUES('admin','ROLE_ADMIN'); INSERT INTO authorities VALUES('user','ROLE_USER');
创建名字为sping_security数据库,同时为该工程配置数据库信息,本例子采用了连接池的方式C30,配置如下:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"> <description>dataSource配置</description> <!-- 指定数据库配置文件地址. --> <context:property-placeholder location="classpath*:datasource/jdbc.properties"/> <!-- 定义数据源Bean,使用C3P0连接池数据源实现 --> <bean name="spring_security_dataSource" id="spring_security_dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 指定连接数据库的驱动 --> <property name="driverClass" value="${jdbc.driverClassName}" /> <!-- 指定连接数据库的URL --> <property name="jdbcUrl" value="${jdbc.url}" /> <!-- 指定连接数据库的用户名 --> <property name="user" value="${jdbc.username}" /> <!-- 指定连接数据库的密码 --> <property name="password" value="${jdbc.password}" /> <!-- C3p0连接池的配置信息 --> <!-- 指定连接池中保留的最大连接数. Default:15 --> <property name="maxPoolSize" value="${cpool.maxPoolSize}" /> <!-- 指定连接池中保留的最小连接数 --> <property name="minPoolSize" value="${cpool.minPoolSize}" /> <!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3 --> <property name="initialPoolSize" value="${cpool.initialPoolSize}" /> <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0 --> <property name="maxIdleTime" value="${cpool.maxIdleTime}" /> <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3 --> <property name="acquireIncrement" value="${cpool.acquireIncrement}" /> <!--JDBC的标准,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0 --> <property name="maxStatements" value="${cpool.maxStatements}" /> <!-- 每60秒检查所有连接池中的空闲连接.Default:0 --> <property name="idleConnectionTestPeriod" value="${cpool.idleConnectionTestPeriod}" /> <!-- 定义在从数据库获取新连接失败后重复尝试的次数。 Default:30 --> <property name="acquireRetryAttempts" value="${cpool.acquireRetryAttempts}" /> <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default:false --> <property name="breakAfterAcquireFailure" value="${cpool.breakAfterAcquireFailure}" /> <!--跟性能消耗大请只在需要的时候是哟个它。如果设为true,那么在每个connection提交的时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable等提升连接测试的性能。Default:false --> <property name="testConnectionOnCheckout" value="${cpool.testConnectionOnCheckout}" /> <!--连接关闭时默认将所有未提交的操作回滚。Default: false --> <property name="autoCommitOnClose" value="${cpool.autoCommitOnClose}" /> <!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒。Default:0 --> <property name="checkoutTimeout" value="${cpool.checkoutTimeout}" /> <!--c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试使用。Default: null --> <property name="automaticTestTable" value="${cpool.automaticTestTable}" /> </bean> </beans>
jdbc properties
# 数据库配置信息 jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring_security?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true jdbc.username=root jdbc.password=root # 数据库连接池信息配置 # Connection pool size cpool.maxPoolSize=25 cpool.minPoolSize=10 cpool.initialPoolSize=15 # How long to keep unused connections around(in seconds) # Note: MySQL times out idle connections after 8 hours(28,800 seconds) # so ensure this value is below MySQL idle timeout cpool.maxIdleTime=7200 # Acquiring new connections is slow, so eagerly retrieve extra connections # when current pool size is reached cpool.acquireIncrement=5 cpool.maxStatements=0 cpool.idleConnectionTestPeriod=60 cpool.acquireRetryAttempts=30 cpool.breakAfterAcquireFailure=true cpool.testConnectionOnCheckout=false cpool.autoCommitOnClose=true # Time to wait for an open connection before timing out # (in milliseconds) cpool.checkoutTimeout=5000 cpool.automaticTestTable=test
针对权限问题,我们建立了如下的几个页面,用于验证不同权限问题
(1) admin.jsp页面,只允许admin访问。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>管理员页面</title> </head> <body> 欢迎管理员 </body> </html>
(2) 页面无权限时403页面,该页面用于显示无权限的页面accessDefined
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>无权访问</title> </head> <body> 你的访问被拒绝,无权访问该资源! </body> </html>
(3)在index页面显示用户,注意在页面显示用户信息的时候我们采用了spring security的特殊标签,如果想在java代码中获取用户的信息,采用如下的方式:
同时,我们添加了用户退出按钮,代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> <%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>首页</title> </head> <body> <h1>这里是首页,<sec:authentication property="name"/>!</h1> <% String[] str = session.getValueNames(); for(int i=0;i<str.length;i++){ out.println("key=="+str[i]); out.println("value=="+session.getAttribute(str[i])); } %> <br/> <a href="admin.jsp">进入admin.jsp</a> <!-- 页面退出的时候 应用spring security的固定url地址--> <br/> <a href="${pageContext.request.contextPath}/j_spring_security_logout" />Logout</a> </body> </html>
工程结构如下:
(3)部署,我们首先验证权限问题,当admin进入时页面直接显示admin的名字,同时我们访问”进入adim.jsp“,界面过程如下:
(4)更换user用户登录,访问admin页面时,界面过程如下:
(5) 验证下退出后的情况,当我们点击退出时,直接返回登录界面,点击浏览器后退按钮,返回到原先的界面,点击链接,此时又返回到登录界面,说明退出按钮的session被清空,起作用了。
关于权限的控制,spring security提供了标签,直接控制菜单的显示与否,标签用法如下:
<sec:authorize ifAllGranted="ROLE_ADMIN" >
<a href="admin.jsp">进入admin.jsp页面</a>
</sec:authorize>
sec:authorize 标签的属性:
A) ifAllGranted:只有当前用户拥有所有指定的权限时,才能显示标签体的内容 (相当于“与” 的关系)
B) ifAnyGranted:当前用户拥有指定的权限中的一个的时候,就能显示标签内部内容(相当于“或”的关系)
C) ifNotGranted:没有指定的权限的时候,显示标签体的内容 (相当于“非”的关系)
或者可如下方式写
<sec:authorize url="/admin.jsp" >
<a href="admin.jsp">进入admin.jsp页面</a>
</sec:authorize>