---
title: "Spring—Acegi系统安全—身份证管理—基于数据库存储用户信息认证"
author: "demojava"
date: "2010-07-30T11:41:02Z"
tags: []
categories: ["企业架构"]
slug: "spring-acegi"
draft: false
lastmod: "2010-07-30T12:52:56Z"
id: 725193
url: "https://demojava.iteye.com/blog/725193"
---

**//数据库脚本：mysql.sql**

DROP DATABASE IF EXISTS sampledb;

CREATE DATABASE sampledb DEFAULT CHARACTER SET utf8;

USE sampledb;

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;

 

**在web.xml中配置信息**

**&lt;?xml version="1.0" encoding="UTF-8"?&gt;**

**&lt;web-app version="2.4" xmlns="[http://java.sun.com/xml/ns/j2ee](http://java.sun.com/xml/ns/j2ee)"**

    **xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"**

    **xsi:schemaLocation="[http://java.sun.com/xml/ns/j2ee](http://java.sun.com/xml/ns/j2ee)**

    **[http://java.sun.com/xml/ns/j2ee/web-app\_2\_4.xsd](http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd)"&gt;**

    **&lt;context-param&gt;  &lt;!-- 指定配置文件的相对位置 --&gt;**

         **&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;**

         **&lt;param-value&gt;**

            **classpath:applicationContext.xml,**

            **classpath:applicationContext-acegi-plugin.xml**

         **&lt;/param-value&gt;**

    **&lt;/context-param&gt;**

   

    **&lt;filter&gt; &lt;!--   创建Acegi 安全过滤器的的代理对象 --&gt;**

         **&lt;filter-name&gt;AcegiFilterChainProxy&lt;/filter-name&gt;**

         **&lt;filter-class&gt;**

             **org.acegisecurity.util.FilterToBeanProxy** 

         **&lt;/filter-class&gt;**

         **&lt;!--  org.acegisecurity.util.FilterChainProxy  这个对象将在spring的配置文件中 --&gt;**

         **&lt;init-param&gt;**

             **&lt;param-name&gt;targetClass&lt;/param-name&gt;**

             **&lt;param-value&gt;**

                  **org.acegisecurity.util.FilterChainProxy**

             **&lt;/param-value&gt;**

         **&lt;/init-param&gt;**

    **&lt;/filter&gt;**

    **&lt;filter-mapping&gt;**

         **&lt;filter-name&gt;AcegiFilterChainProxy&lt;/filter-name&gt;**

         **&lt;url-pattern&gt;/\*&lt;/url-pattern&gt; &lt;!-- 这个过滤器将拦截所有请求 --&gt;**

    **&lt;/filter-mapping&gt;**

   

    **&lt;listener&gt;  &lt;!-- Spring的上下文的监听器 --&gt;**

         **&lt;listener-class&gt;**

             **org.springframework.web.context.ContextLoaderListener**

         **&lt;/listener-class&gt;**

    **&lt;/listener&gt;**

**&lt;listener&gt;**

         **&lt;listener-class&gt;**

             **org.acegisecurity.ui.session.HttpSessionEventPublisher**

         **&lt;/listener-class&gt;**

    **&lt;/listener&gt;**

 

    **&lt;!--session 超时定义--&gt;**

    **&lt;session-config&gt;**

         **&lt;session-timeout&gt;60&lt;/session-timeout&gt;**

    **&lt;/session-config&gt;**

 

    **&lt;!--默认首页定义--&gt;**

    **&lt;welcome-file-list&gt;**

         **&lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;**

         **&lt;welcome-file&gt;index.html&lt;/welcome-file&gt;**

    **&lt;/welcome-file-list&gt;**

**&lt;/web-app&gt;**

 

 

**下面我们来看看spring的配置文件**

 

&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&gt;

&lt;beans&gt;

    &lt;!--

        /\*\*=channelProcessingFilter,httpSessionContextIntegrationFilter,logoutFilter,authenticationProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter,concurrentSessionFilter,filterInvocationInterceptor

    --&gt;

   

    &lt;!-- 创建代理对象 --&gt;

    &lt;bean id="filterChainProxy"

         class="org.acegisecurity.util.FilterChainProxy"&gt;

         &lt;property name="filterInvocationDefinitionSource"&gt;

             &lt;value&gt;

                  CONVERT\_URL\_TO\_UPPERCASE\_BEFORE\_COMPARISON

                  PATTERN\_TYPE\_APACHE\_ANT               /\*\*=channelProcessingFilter,httpSessionContextIntegrationFilter,authenticationProcessingFilter,logoutFilter,rememberMeProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter,concurrentSessionFilter,filterSecurityInterceptor

             &lt;/value&gt;

         &lt;/property&gt;

    &lt;/bean&gt;

    &lt;!--CONVERT\_URL\_TO\_UPPERCASE\_BEFORE\_COMPARISON表示的是判读的是URL 而 PATTERN\_TYPE\_APACHE\_ANT 表示是的使用的是ANT的路径风格进行配置匹配的URL 如果不写Acgi将使用正则表达式来解析这个URL 这个地方不要使用会出或换行 --&gt;

   

    &lt;!-- authenticationProcessingFilter 使用认证的处理器过滤匹配的URL  --&gt;

    &lt;!--

         每次request前 HttpSessionContextIntegrationFilter从Session中获取Authentication对象，在request完后

         又把Authentication对象保存到Session中供下次request使用,此filter必须其他Acegi filter前使用

    --&gt;

    &lt;bean id="httpSessionContextIntegrationFilter"

         class="org.acegisecurity.context.HttpSessionContextIntegrationFilter" /&gt;

    &lt;!--

         和servlet spec差不多,处理登陆请求

         authenticationFailureUrl定义登陆失败时转向的页面

         defaultTargetUrl定义登陆成功时转向的页面

         filterProcessesUrl定义登陆请求的页面

         rememberMeServices用于在验证成功后添加cookie信息

    --&gt;

    &lt;bean id="authenticationProcessingFilter"

         class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter"&gt;

         &lt;property name="authenticationManager"   ref="authenticationManager" /&gt;

         &lt;property name="authenticationFailureUrl"   value="/index.jsp?login\_error=1" /&gt;&lt;!-- 认证失败后的URL --&gt;

         &lt;property name="defaultTargetUrl" value="/main.jsp" /&gt;&lt;!-- 跳转后的URL --&gt;

         &lt;property name="filterProcessesUrl"  value="/j\_acegi\_security\_check" /&gt;&lt;!-- 要过滤的URL  --&gt;

         &lt;property name="rememberMeServices" ref="rememberMeServices" /&gt;&lt;!-- 登录时将用户名或密码保存到 cookie 中 --&gt;

    &lt;/bean&gt;

   

    &lt;!-- 认证的处理过滤器 --&gt;

    &lt;bean id="authenticationManager"

class="org.acegisecurity.providers.ProviderManager"&gt;

         &lt;property name="providers"&gt;

             &lt;list&gt;

                  &lt;ref local="daoAuthenticationProvider" /&gt;

                  &lt;bean class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider"&gt;

                       &lt;property name="key" value="baobaotao" /&gt;

                  &lt;/bean&gt;

                  &lt;ref local="anonymousAuthenticationProvider" /&gt;

             &lt;/list&gt;

         &lt;/property&gt;

         &lt;property name="sessionController" ref="concurrentSessionController"/&gt;

    &lt;/bean&gt;

&lt;!-- 基于数据库存储的用户信息获取 --&gt;

    &lt;bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"&gt;

         &lt;property name="userDetailsService" ref="userDetailsService" /&gt;

&lt;!--#1 --&gt;

 

    &lt;/bean&gt;

&lt;!--  基于数据库的中使用列表的对象 org.acegisecurity.userdetails.jdbc.JdbcDaoImpl 这个处理类是固定的 --&gt;

  &lt;bean id="userDetailsService"

       class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl"&gt;

       &lt;property name="dataSource" ref="dataSource" /&gt;

       &lt;property name="usersByUsernameQuery"&gt;

           &lt;value&gt;

                SELECT username,password,1 FROM t\_user WHERE status='1'

                AND username = ?

           &lt;/value&gt;

           &lt;!-- 根据用户名查询用户的SQL语句 --&gt;

       &lt;/property&gt;

       &lt;property name="authoritiesByUsernameQuery"&gt;

           &lt;value&gt;

                SELECT u.username,p.priv\_name FROM t\_user u,t\_user\_priv

                p WHERE u.user\_id =p.user\_id AND u.username = ?

           &lt;/value&gt;

       &lt;!-- 根据用户名查询用户权限的sql语句 --&gt;

       &lt;/property&gt;

  &lt;/bean&gt;

//jdbcDaoImpl通过usersByUsernameQuery和authoritiesByUsernameQuery属性的定义查询用户信息和用户的权限的sql语句，实际上jdbcDaoImpl为以上两个属性提供了默认