---
title: "Acegi + Spring + Hibernate + Struts 2搭建基于角色的权限控制系统"
author: "returnofking"
date: "2008-10-18T17:35:48Z"
tags: []
slug: "acegi-spring-hibernate-struts-2"
draft: false
lastmod: "2008-10-18T17:35:48Z"
id: 254735
url: "https://ReturnOfKing.iteye.com/blog/254735"
---

安全永远是WEB应用系统必须面对的头等大事, 也是最头疼的事, 其实安全系统就只包括两个问题: 认证和授权.

    以前做些网站系统, 安全检测逻辑都在放在须要安全控制的代码前面, 这样做有很多不好的地方, 重复多次的编码就不用说了, 代码移植性, 重用性都得不到体现, 安全检测逻辑要永远和业务逻辑放在一起.

    那么, 能不能够在进入方法前就调用一些安全检测? 其实Spring AOP就是这个思想, 那么又如何实现安全检测呢? Spring Acegi Security 框架就是做这个事情.

     本文主要是讨论下在已有的SSH系统中, 如何使用Acegi作为安全框架实现基于角色的权限控制(Role Based Access Control RBAC) , 本文主要是以Java 5注解的形式来配置安全框架, 大大减化配置和操作.

    本文的主要参考资料: &lt;Spring 2.0 核心技术与最佳实践&gt; 第10章 (Spring Acegi 安全框架)

                &lt;精通Spring 2.X -- 企业应用开发详解&gt; 第17章 (使用Acegi 实施应用系统安全)

                acegi-security-1.0.6 官方文档

    说明: 本文介绍的是RBAC, 在官方文档的基础上有所扩展或改动, 以更适合WEB应用系统. 其实我觉得大多数的网站基于角色已经足够了, 一般都没必要基于权限.

     文章开始:

一. 下载所要的软件或JAR包:

    我的相关配置是: Java 5, Tomcat 5.5.26, Struts 2.0.11, Spring 2.5.1, Hibernate 3.2, Acegi 1.0.6

二. 建立相关的数据库:

     数据表: 用户信息表User: id, enable, user\_name, user\_pass, email\_box

                 角色信息表RoleInfo: id, role\_name, role\_title, descp

                 用户与角色关联表(用户与角色是多对多关系)UserRole: user\_id, user\_name, role\_id, role\_name

                并在这三个表中插入相关的数据, 我是定义了两种角色(role\_name): ROLE\_USER, ROLE\_ADMIN

                和三个用户, 一个用户角色为: ROLE\_USER, ROLE\_ADMIN

                 另一个用户角色为: ROLE\_USER

                第三个没有角色.

二. 修改配置文件:

    其实对Acegi框架的应用难点就在配置文件, 所以要特别注意了:

    在 src 建立Acegi的配置文件: acegi-security.xml 当然这个文件的名称是可以任意的.

    acegi-security.xml 说白了就是配置: 安全拦截器, 认证管理器, 决策管理器.

    其内容如下:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans xmlns=""&gt;  
&lt;!-- ========================= 认证管理器 ========================= --&gt;  
&lt;bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager"&gt;  
&lt;property name="providers"&gt;  
   &lt;list&gt;  
    &lt;ref bean="daoAuthenticationProvider" /&gt;  
    &lt;ref bean="rememberMeAuthenticationProvider" /&gt;  
   &lt;/list&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;!-- 基于DAO验证的AuthenticationProvider --&gt;  
&lt;bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"&gt;  
&lt;property name="userDetailsService" ref="userDetailsService" /&gt;  
&lt;/bean&gt;  
&lt;bean id="userDetailsService" class="org.ymcn.security.AcegiUserDeitailsService"&gt;  
&lt;property name="userDao" ref="userDao" /&gt;  
&lt;property name="userRoleDao" ref="userRoleDao" /&gt;  
&lt;/bean&gt;

&lt;bean id="rememberMeAuthenticationProvider" class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider"&gt;  
&lt;property name="key" value="" /&gt;  
&lt;/bean&gt;  
&lt;bean id="rememberMeServices" class="org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices"&gt;  
&lt;property name="userDetailsService" ref="userDetailsService" /&gt;  
&lt;property name="parameter" value="j\_remember\_me" /&gt;  
&lt;property name="key" value="[obullxl@163.com](mailto:obullxl@163.com)" /&gt;  
&lt;property name="tokenValiditySeconds" value="31536000" /&gt;  
&lt;/bean&gt;

&lt;!-- ========================= 决策管理器 ========================= --&gt;  
&lt;bean id="accessDecisionManager" class="org.acegisecurity.vote.AffirmativeBased"&gt;  
&lt;property name="decisionVoters"&gt;  
   &lt;list&gt;  
    &lt;ref bean="roleVoter" /&gt;  
   &lt;/list&gt;  
&lt;/property&gt;  
&lt;!-- 是否全部弃权就通过 --&gt;  
&lt;property name="allowIfAllAbstainDecisions" value="false" /&gt;  
&lt;/bean&gt;  
&lt;bean id="roleVoter" class="org.acegisecurity.vote.RoleVoter"&gt;  
&lt;property name="rolePrefix" value="ROLE\_" /&gt;  
&lt;/bean&gt;

&lt;!-- ========================= 过滤器链 ========================= --&gt;  
&lt;bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy"&gt;  
&lt;property name="filterInvocationDefinitionSource"&gt;  
   &lt;value&gt;  
    CONVERT\_URL\_TO\_LOWERCASE\_BEFORE\_COMPARISON  
    PATTERN\_TYPE\_APACHE\_ANT  
    /\*\*=httpSessionContextIntegrationFilter,logoutFilter,authenticationProcessingFilter,rememberMeFilter,exceptionFilter,securityInterceptor  
   &lt;/value&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;bean id="httpSessionContextIntegrationFilter" class="org.acegisecurity.context.HttpSessionContextIntegrationFilter" /&gt;  
&lt;bean id="logoutFilter" class="org.acegisecurity.ui.logout.LogoutFilter"&gt;  
&lt;!-- 登录退出后的URL --&gt;  
&lt;constructor-arg value="/" /&gt;  
&lt;constructor-arg&gt;  
   &lt;list&gt;  
    &lt;ref bean="rememberMeServices" /&gt;  
    &lt;bean class="org.acegisecurity.ui.logout.SecurityContextLogoutHandler" /&gt;  
   &lt;/list&gt;  
&lt;/constructor-arg&gt;  
&lt;!-- 登录退出的URL --&gt;  
&lt;property name="filterProcessesUrl" value="/j\_logout.j" /&gt;  
&lt;/bean&gt;  
&lt;bean id="authenticationProcessingFilter" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter"&gt;  
&lt;property name="authenticationManager" ref="authenticationManager" /&gt;  
&lt;!-- 登录失败后的URL --&gt;  
&lt;property name="authenticationFailureUrl" value="/login.jsp?msg=%E6%97%A0%E6%95%88%E7%9A%84%E7%94%A8%E6%88%B7%E5%90%8D%E6%88%96%E5%8F%A3%E4%BB%A4" /&gt;  
&lt;!-- 登录成功后的URL --&gt;  
&lt;property name="defaultTargetUrl" value="/user/cmd.jsp" /&gt;  
&lt;!-- 登录的URL --&gt;  
&lt;property name="filterProcessesUrl" value="/j\_login.j" /&gt;  
&lt;property name="rememberMeServices" ref="rememberMeServices" /&gt;  
&lt;/bean&gt;  
&lt;bean id="rememberMeFilter" class="org.acegisecurity.ui.rememberme.RememberMeProcessingFilter"&gt;  
&lt;property name="authenticationManager" ref="authenticationManager" /&gt;  
&lt;property name="rememberMeServices" ref="rememberMeServices" /&gt;  
&lt;/bean&gt;  
&lt;bean id="exceptionFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter"&gt;  
&lt;!-- 出现AuthenticationException时的登录入口 --&gt;  
&lt;property name="authenticationEntryPoint"&gt;  
   &lt;bean class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint"&gt;  
   &lt;property name="loginFormUrl" value="/login.jsp" /&gt;  
    &lt;property name="forceHttps" value="false" /&gt;  
   &lt;/bean&gt;  
&lt;/property&gt;  
&lt;!-- 出现AccessDeniedException时的Handler --&gt;  
&lt;property name="accessDeniedHandler"&gt;  
   &lt;bean class="org.acegisecurity.ui.AccessDeniedHandlerImpl"&gt;  
   &lt;property name="errorPage" value="/denied.jsp" /&gt;  
   &lt;/bean&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;bean id="securityInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor"&gt;  
&lt;property name="authenticationManager" ref="authenticationManager" /&gt;  
&lt;property name="accessDecisionManager" ref="accessDecisionManager" /&gt;  
&lt;property name="objectDefinitionSource"&gt;  
   &lt;value&gt;  
    CONVERT\_URL\_TO\_LOWERCASE\_BEFORE\_COMPARISON  
    PATTERN\_TYPE\_APACHE\_ANT  
    /admin/\*\*=ROLE\_ADMIN  
    /user/\*\*=ROLE\_USER  
    /cart/previeworder\*=ROLE\_USER  
   &lt;/value&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;/beans&gt;

    在上面的配置文件中, 红色部分要特别注意, 其余的内容都差不多了.

&lt;bean id="userDetailsService" class="org.ymcn.security.AcegiUserDeitailsService"&gt;  
&lt;property name="userDao" ref="userDao" /&gt;  
&lt;property name="userRoleDao" ref="userRoleDao" /&gt;  
&lt;/bean&gt;

    在整个应用的安全控制中, 我们唯一要编写代码的类就是: org.ymcn.security.AcegiUserDeitailsService

    就连登录和登出的代码也不要了.

三. 修改 web.xml, 增加安全控制过滤链.

&lt;filter&gt;  
        &lt;filter-name&gt;acegiFilterChain&lt;/filter-name&gt;  
        &lt;filter-class&gt;org.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&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;acegiFilterChain&lt;/filter-name&gt;  
        &lt;url-pattern&gt;\*.j&lt;/url-pattern&gt;  
&lt;/filter-mapping&gt;

 

    注意: 这个过滤器一定要在MVC转发过滤器的前面!!!!

 

四. 在 applicationContext.xml 中增加 Acegi安全控制拦截器 和 Spring的自动代理功能实现AOP代理

&lt;!-- Acegi安全控制拦截器 --&gt;  
&lt;bean id="serviceSecurityInterceptor" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor"&gt;  
&lt;property name="validateConfigAttributes" value="true" /&gt;  
&lt;property name="authenticationManager" ref="authenticationManager" /&gt;  
&lt;property name="accessDecisionManager" ref="accessDecisionManager" /&gt;  
&lt;property name="objectDefinitionSource"&gt;  
   &lt;bean class="org.acegisecurity.intercept.method.MethodDefinitionAttributes"&gt;  
    &lt;property name="attributes"&gt;  
     &lt;bean class="org.acegisecurity.annotation.SecurityAnnotationAttributes" /&gt;  
    &lt;/property&gt;  
   &lt;/bean&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;!-- 利用Spring的自动代理功能实现AOP代理 --&gt;  
&lt;bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt;  
&lt;property name="interceptorNames"&gt;  
   &lt;list&gt;  
    &lt;value&gt;transactionInterceptor&lt;/value&gt;  
    &lt;value&gt;serviceSecurityInterceptor&lt;/value&gt;  
   &lt;/list&gt;  
&lt;/property&gt;  
&lt;property name="beanNames"&gt;  
   &lt;list&gt;  
    &lt;value&gt;userService&lt;/value&gt;  
    &lt;value&gt;mailService&lt;/value&gt;  
   &lt;/list&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

五. 编写在利用Acegi框架唯一要我们编写的类 AcegiUserDeitailsService.java

package org.ymcn.security;

import java.util.List;

import org.acegisecurity.GrantedAuthority;  
import org.acegisecurity.GrantedAuthorityImpl;  
import org.acegisecurity.userdetails.UserDetails;  
import org.acegisecurity.userdetails.UserDetailsService;  
import org.acegisecurity.userdetails.UsernameNotFoundException;  
import org.apache.commons.logging.Log;  
import org.apache.commons.logging.LogFactory;  
import org.springframework.dao.DataAccessException;  
import org.ymcn.dao.UserDao;  
import org.ymcn.dao.UserRoleDao;  
import org.ymcn.model.User;  
import org.ymcn.model.UserRole;

public class AcegiUserDeitailsService implements UserDetailsService {  
private final Log LOG = LogFactory.getLog(AcegiUserDeitailsService.class);  
/* 依赖注入 \*/  
private UserDao userDao;  
private UserRoleDao userRoleDao;

public void setUserDao(UserDao userDao) {  
   this.userDao = userDao;  
}  
public void setUserRoleDao(UserRoleDao userRoleDao) {  
   this.userRoleDao = userRoleDao;  
}

/* 用户所有的权限 \*/  
//private final List&lt;GrantedAuthority&gt; grantedAuthList = new ArrayList&lt;GrantedAuthority&gt;(6);  
private GrantedAuthority\[] grantedAuthArray;

public UserDetails loadUserByUsername(String userName)  
    throws UsernameNotFoundException, DataAccessException {  
   if(LOG.isDebugEnabled()) {  
    LOG.debug("Loading UserDetails of userName: " + userName);  
   }  
   /* 取得用户 \*/  
   User user = userDao.getUserByName(userName);  
   if(user == null) {  
    LOG.warn("UserDetails load failed: No such UserRole with userName: " + userName);  
            throw new UsernameNotFoundException("User name is not found.");  
   }  
   /* 取得所有用户权限 \*/  
   List&lt;UserRole&gt; userRoleList = userRoleDao.getUserRoleByUserName(userName);  
   if(userRoleList == null || userRoleList.size() == 0) {  
    LOG.warn("UserRole load failed: No such UserRole with userName: " + userName);  
            throw new UsernameNotFoundException("UserRole is not found.");  
   }  
   /* 取得用户的所有角色 \*/  
   int size = userRoleList.size();  
   grantedAuthArray = new GrantedAuthority\[size];  
   int j = 0;  
   for(int i = 0; i &lt; size; i++) {  
    UserRole userRole = userRoleList.get(i);  
    if(userRole != null) {  
     this.grantedAuthArray\[j++] = new GrantedAuthorityImpl(userRole.getRoleName().toUpperCase());  
    }  
   }  
   LOG.info("UserName: " + userName + " loaded successfully.");  
        return new org.acegisecurity.userdetails.User(userName, user.getUserPass(),  
           true, true, true, true, this.grantedAuthArray);  
}  
}

六. 在业务逻辑代码中利用Java 5注释实现安全控制

@Secured({"ROLE\_USER"})  
void sendSimpleMail(Long userId);

@Secured({"ROLE\_ADMIN"})  
void sendAttachmentMail() throws Exception;

    其实就是在需要安全控制的方法前加上: @Secured({"角色名"}), 非常的简单

七. 整个工作完成

    Acegi框架完全是一种可插拔式的, 完全可以在原有的系统中加个一个配置文件, 和在每个方法前加上: @Secured({"角色名"}) 就可完成.

     上面的 AcegiUserDeitailsService.java 中的有 UserDao, UserRoleDao, 我想一看就知道它们是干什么的了, 这完全取决于个人的实现, 与Acegi无关, 它仅仅只要返回一个 return new org.acegisecurity.userdetails.User(userName, user.getUserPass(),  
          true, true, true, true, this.grantedAuthArray) 就可以了.

    祝大家使用JAVA愉快!!!