构建基于数据库权限信息存储
在Spirng Acegi 身份认证连载 的基础上进行配置文件的修改。
步骤1创建数据库
数据库的脚本如下:
DROP DATABASE IF EXISTS SpringSecurityDB;
CREATE DATABASE SpringSecurityDB DEFAULT CHARACTER SET utf8;
USE SpringSecurityDB;
CREATE TABLE T_USER (
USER_ID INTEGER NOT NULL AUTO_INCREMENT,
USERNAME VARCHAR(30) NOT NULL,
PASSWORD VARCHAR(30) DEFAULT NULL,
STATUS TINYINT(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`USER_ID`),
UNIQUE KEY `USERNAME` (`USERNAME`)
);
CREATE TABLE T_USER_PRIV (
USER_ID INTEGER NOT NULL DEFAULT '0',
PRIV_NAME VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (USER_ID, PRIV_NAME)
);
INSERT INTO T_USER (USER_ID, USERNAME, PASSWORD, STATUS) VALUES
(1,'tom','tom',1),
(2,'john','john',1);
INSERT INTO T_USER_PRIV (USER_ID, PRIV_NAME) VALUES
(1,'PRIV_1'),
(1,'PRIV_2'),
(1,'PRIV_COMMON'),
(2,'PRIV_1'),
(2,'PRIV_COMMON');
COMMIT;
步骤2 修改applicationContext-acegi-plugin.xml配置文件的修改
<bean id="daoAuthenticationProvider" class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl">
<property name="userDetailsService" ref="userDetailsService"/>
</bean>
<bean id="userDetailsService" class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl">
<property name="userMap">
<value>
john=john,PRIV_COMMON,PRIV_1
tom=tom,PRIV_COMMON,PRIV_1,PRIV_2
peter=peter,disabled,PRIV_COMMON,PRIV_1
</value>
</property>
</bean>
修改为
<bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="userDetailsService"/>
</bean>
<bean id="userDetailsService" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl">
<property name="dataSource" ref="dataSource"/>
<property name="usersByUsernameQuery">
<value>
<![CDATA[ SELECT u.USERNAME,u.PASSWORD,1 FROM T_USER u where u.STATUS = 1 AND u.USERNAME = ?]]>
</value>
</property>
<property name="authoritiesByUsernameQuery">
<value>
<![CDATA[SELECT u.USERNAME,p.PRIV_NAME FROM T_USER u,T_USER_PRIV p
WHERE u.USER_ID = p.USER_ID and u.USERNAME=?]]>
</value>
</property>
</bean>
步骤3 修改applicationContext.xml配置文件的修改
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="driverUrl">
<value>jdbc:mysql://127.0.0.1:3306/SpringSecurityDB?user=root&password=kingsoft
</value>
</property>
<property name="user" value="root" />
<property name="password" value="kingsoft" />
<property name="alias" value="Pool_dbname" />
<property name="prototypeCount" value="0" />
<property name="maximumConnectionCount" value="50" />
<property name="minimumConnectionCount" value="2" />
<property name="simultaneousBuildThrottle" value="50" />
<property name="houseKeepingTestSql" value="select CURRENT_DATE" />
</bean>
步骤4、项目lib中添加jar
proxool-0.9.1.jar、proxool-cglib.jar和mysql.jar