菜鸟-手把手教你把Acegi应用到实际项目中(10)-保护业务方法

      前面已经讲过关于保护Web资源的方式,其中包括直接在XML文件中配置和自定义实现FilterInvocationDefinitionSource接口两种方式。在实际企业应用中,保护Web资源显得非常重要,它是保障Web应用安全性的关键部分。有了它,我们的Web应用就显得更加安全了。的确,部分Web应用有了它已经足够了。但许多时候却有这样的场景,某企业的系统允许用户A查看数据,但不允许他修改或删除数据;而用户B不但可以查看数据,而且可以修改和删除数据。此时,前面所说的保护Web资源的方式就无法满足这个需求了。既而我们会想到,关于查看、修改和删除等操作,都是通过操作相应业务方法来实现的。那么,我们可不可以实现对这些业务方法的保护呢?答案是肯定的,Acegi为我们提供了这一实现机制。
      对于业务方法的保护,其实跟保护Web资源的方式非常相似。只要我们弄清楚了保护Web资源的工作原理和各种实现方式,再来学习保护业务方法相关的知识,那么将会很快上手。
      在继续阅读本节内容之前,朋友们应该先阅读“菜鸟-教你把Acegi应用到实际项目(9)-实现FilterInvocationDefinition”一节(http://zhanjia.iteye.com/blog/261123)。因为此篇的第二部分内容与前一篇的内容非常相似,故在此我只列出不同部分,不做详细解释。

一、在Acegi配置文件中配置实现保护业务方法
1、下面先看看关于保护Web资源和保护业务方法的部分配置:
*保护Web资源的配置

 

<bean id="filterInvocationInterceptor"
	class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
	<property name="authenticationManager" ref="authenticationManager" />
	<property name="accessDecisionManager"
		ref="httpRequestAccessDecisionManager" />
	<property name="objectDefinitionSource">
		<value>
			<![CDATA[
				CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
			    PATTERN_TYPE_APACHE_ANT
			    /**/*.jpg=AUTH_ANONYMOUS,AUTH_USER
			    /**/*.gif=AUTH_ANONYMOUS,AUTH_USER
			    /**/*.png=AUTH_ANONYMOUS,AUTH_USER
			    /login.jsp*=AUTH_ANONYMOUS,AUTH_USER
  				/**=AUTH_USER
			]]>
		</value>
	</property>
</bean>

 

*保护业务方法配置

 

<bean id="contactManagerSecurity"
	class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
	<property name="authenticationManager" ref="authenticationManager" />
	<property name="accessDecisionManager"
		ref="httpRequestAccessDecisionManager" />
	<property name="objectDefinitionSource">
		<value>
			sample.service.IContactManager.create=AUTH_FUNC_ContactManager.create
			sample.service.IContactManager.delete=AUTH_FUNC_ContactManager.delete
			sample.service.IContactManager.getAll=AUTH_FUNC_ContactManager.getAll
			sample.service.IContactManager.getById=AUTH_FUNC_ContactManager.getById
			sample.service.IContactManager.update=AUTH_FUNC_ContactManager.update
		</value>
	</property>
</bean>

 

      从上面配置方式可以看到,两种配置方式基本差不多,只有两个地方存在差别。一个是实现类不同,前者是FilterSecurityInterceptor,后者是MethodSecurityInterceptor。另外一个是objectDefinitionSource中的配置不同。
      FilterSecurityInterceptor和MethodSecurityInterceptor都继承自AbstractSecurityInterceptor,而且都拥有objectDefinitionSource属性。尽管他们都拥有相同的objectDefinitionSource属性,但前者属于FilterInvocationDefinitionSource类型,而后者属于MethodDefinitionSource类型。然而,这两个Source又都继承自ObjectDefinitionSource。正因为如此,所以保护Web资源和保护业务方法的原理是一样的,只要懂得运用其中一个,那么另一个也就会了。

      “=”号左边的内容代表了方法名全称,即以类的全限定名和目标方法名一同构成。“=”号右边的内容代表了左边方法所对应的角色集合,角色集合由逗号隔开的多个角色名构成。

 

2、业务接口和实现类

public interface IContactManager{
  public List getAll();
  public Contact getById(Integer id);
  public void create(Contact contact);
  public void update(Contact contact);
  public void delete(Contact contact);
}

public class ContactManager implements IContactManager {
	……
}

 

3、加入保护业务方法的拦截器 

 

<bean id="transactionInterceptor"
	class="org.springframework.transaction.interceptor.TransactionInterceptor">
	<property name="transactionManager">
		<ref bean="transactionManager" />
	</property>
	<property name="transactionAttributeSource">
		<value>
			sample.service.impl.ContactManager.*=PROPAGATION_REQUIRED
		</value>
	</property>
</bean>

<!--dao start -->
<bean id="contactDao" class="sample.dao.impl.ContactDao">
	<property name="dataSource">
		<ref bean="dataSource" />
	</property>
</bean>

<!--service start -->
<bean id="contactManagerTarget"
	class="sample.service.impl.ContactManager">
	<property name="contactDao">
		<ref bean="contactDao" />
	</property>
</bean>

<bean id="contactManager"
	class="org.springframework.aop.framework.ProxyFactoryBean">
	<property name="proxyInterfaces">
		<value>sample.service.IContactManager</value>
	</property>
	<property name="interceptorNames">
		<list>
			<idref local="transactionInterceptor" />
			<!-- 加入保护业务方法的拦截器 -->
			<idref bean="contactManagerSecurity"/>
			<idref local="contactManagerTarget" />
		</list>
	</property>
</bean>

 

 

 二、自定义实现MethodDefinitionSource接口保护业务方法
      这部分内容建立在前一篇的基础之上,请参考:“菜鸟-教你把Acegi应用到实际项目(9)-实现FilterInvocationDefinition”一节(http://zhanjia.iteye.com/blog/261123)

 

1、修改RdbmsEntryHolder类
前篇保护Web资源时RdbmsEntryHolder类如下:

public class RdbmsEntryHolder implements Serializable {
    // 保护的URL模式   
    private String url;   
    // 要求的角色集合   
    private ConfigAttributeDefinition cad;
    ......
}

 

      由于我们现在所要保护的是业务方法,故我们将url变量易名为method,这样会更加明确。method变量存放类似于“sample.service.IContactManager.create”、“sample.service.IContactManager.update*”的方法名全称。

 

2、修改RdbmsSecuredUrlDefinition类
      将RdbmsSecuredUrlDefinition改名为RdbmsSecuredMethodDefinition,黑体部分为修改后的代码。

public class RdbmsSecuredMethodDefinition extends MappingSqlQuery{

	protected static final Log log = LogFactory.getLog(RdbmsSecuredMethodDefinition.class);
	
    public RdbmsSecuredMethodDefinition(DataSource ds) {
        super(ds, Constants.ACEGI_RDBMS_SECURED_SQL);
        compile();
    }

    /**
     * convert each row of the ResultSet into an object of the result type.
     */
    protected Object mapRow(ResultSet rs, int rownum)
        throws SQLException {    	
    	RdbmsEntryHolder rsh = new RdbmsEntryHolder();
    	// 设置业务方法
    	rsh.setMethod(rs.getString(Constants.ACEGI_RDBMS_SECURED_METHOD).trim());
    	
        ConfigAttributeDefinition cad = new ConfigAttributeDefinition();
        
        String rolesStr = rs.getString(Constants.ACEGI_RDBMS_SECURED_ROLES).trim();
        // commaDelimitedListToStringArray:Convert a CSV list into an array of Strings
        // 以逗号为分割符, 分割字符串
        String[] tokens = 
        		StringUtils.commaDelimitedListToStringArray(rolesStr); // 角色名数组
        // 构造角色集合
        for(int i = 0; i < tokens.length;++i)
        	cad.addConfigAttribute(new SecurityConfig(tokens[i]));
        
        //设置角色集合
        rsh.setCad(cad);
    	
        return rsh;
    }

}

 

 其中,Constants常量类如下:

public interface Constants {

	// Acegi相关常量--------------------------------------------
	
	// 业务方法与对应角色查询语句
	public static final String ACEGI_RDBMS_SECURED_SQL = "SELECT authority,protected_res FROM authorities WHERE auth_type='FUNCTION' AND authority LIKE 'AUTH_FUNC_ContactManager%'";

	// 方法字段名称
	public static final String ACEGI_RDBMS_SECURED_METHOD = "protected_res";
	
	// 角色字符串字段名称
	public static final String ACEGI_RDBMS_SECURED_ROLES = "authority";
	
}

 3、自定义实现MethodDefinitionSource接口
      修改RdbmsFilterInvocationDefinitionSource类,改名为RdbmsMethodDefinitionSource,并修改相应方法,黑体部分为修改后的代码。

 

变量:

 修改前:private RdbmsSecuredUrlDefinition rdbmsInvocationDefinition;
 修改后:private RdbmsSecuredMethodDefinition rdbmsSecuredMethodDefinition;

 

以下两个函数,黑体为修改或增加部分:

protected void initDao() throws Exception {
	this.rdbmsSecuredMethodDefinition = 
		new RdbmsSecuredMethodDefinition(this.getDataSource()); // 传入数据源, 此数据源由Spring配置文件注入
	……
}

public ConfigAttributeDefinition getAttributes(Object object) throws IllegalArgumentException {
	if ((object == null) || !this.supports(object.getClass())) {
		throw new IllegalArgumentException("抱歉,目标对象不是MethodInvocation类型");
	}
	
	Method method = ((MethodInvocation) object).getMethod();
	
	List list = this.getRdbmsEntryHolderList();
	if (list == null || list.size() == 0)
		return null;
	
	// 获取方法全称, 如java.util.Set.isEmpty
	String methodString = method.getDeclaringClass().getName() + "." + method.getName();
	
	String mappedName;
	Iterator it = list.iterator();
	// 循环判断当前访问的方法是否设置了角色访问机制, 有则返回ConfigAttributeDefinition(角色集合), 否则返回null
	while (it.hasNext()) {
		RdbmsEntryHolder entryHolder = (RdbmsEntryHolder) it.next();
		mappedName = entryHolder.getMethod();
		boolean matched = pathMatcher.match(entryHolder.getMethod(), methodString);
		//boolean matched = methodString.equals(mappedName) || isMatch(methodString, mappedName);
		if (logger.isDebugEnabled()) {
			logger.debug("匹配到如下Method: '" + methodString + ";模式为 "
					+ entryHolder.getMethod() + ";是否被匹配:" + matched);
		}

		// 如果在用户所有被授权的URL中能找到匹配的, 则返回该ConfigAttributeDefinition(角色集合)
		if (matched) {
			return entryHolder.getCad();
		}
	}
	
	return null;
}

 

 4、通过Spring DI注入RdbmsMethodDefinitionSource

 

<bean id="contactManagerSecurity" 
	class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
	<property name="authenticationManager" ref="authenticationManager" />
	<property name="accessDecisionManager" ref="httpRequestAccessDecisionManager" />
	<property name="objectDefinitionSource" ref="rdbmsMethodDefinitionSource" />
</bean>

<bean id="rdbmsMethodDefinitionSource" class="sample.service.impl.RdbmsMethodDefinitionSource">
	<property name="dataSource" ref="dataSource" />
	<property name="webresdbCache" ref="webresCacheBackend" />
</bean>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>

<bean id="webresCacheBackend" 
	class="org.springframework.cache.ehcache.EhCacheFactoryBean">
	<property name="cacheManager">
		<ref local="cacheManager"/>
	</property>
	<property name="cacheName">
		<value>webresdbCache</value>
	</property>
</bean>

 

      至此,我们此节所讲的内容已结束,大家可以下载源代码以便调试。另外,代码中还提供了另一个版本的实现类RdbmsMethodDefinitionSourceVersion2,它继承了AbstractMethodDefinitionSource,在一定程序上减少了代码量,朋友们可以自行研究。

 

 

三、其他说明
1、数据库
在项目Acegi9的WebRoot/db目录下存放有相关数据库脚本,本节所采用的数据库版本是MySQL 5.0。


2、环境说明
开发环境:

MyEclipse 5.0GA
Eclipse3.2.1
JDK1.5.0_10
tomcat5.5.23
acegi-security-1.0.7
Spring2.0

 


Jar包:
acegi-security-1.0.7.jar
commons-codec.jar
jstl.jar(1.1)
spring.jar(2.0.8)
standard.jar
commons-logging.jar(1.0)
ehcache-1.3.0.jar
c3p0-0.9.0.jar
log4j-1.2.13.jar
mysql-connector-java-3.1.10-bin.jar

 

 

      真不好意思,上面所说的黑体部分,在代码里面变成了<STRONG></STRONG>。也就是说,在该标签内的内容即为黑体部分

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
东南亚位于我国倡导推进的“一带一路”海陆交汇地带,作为当今全球发展最为迅速的地区之一,近年来区域内生产总值实现了显著且稳定的增长。根据东盟主要经济体公布的最新数据,印度尼西亚2023年国内生产总值(GDP)增长5.05%;越南2023年经济增长5.05%;马来西亚2023年经济增速为3.7%;泰国2023年经济增长1.9%;新加坡2023年经济增长1.1%;柬埔寨2023年经济增速预计为5.6%。 东盟国家在“一带一路”沿线国家的总体GDP经济规模、贸易总额与国外直接投资均为最大,因此有着举足轻重的地位和作用。当前,东盟与国已互相成为双方最大的交易伙伴。-东盟贸易总额已从2013年的443亿元增长至 2023年合计超逾6.4万亿元,占国外贸总值的15.4%。在过去20余年,东盟国家不断在全球多变的格局里面临挑战并寻求机遇。2023东盟国家主要经济体受到国内消费、国外投资、货币政策、旅游业复苏、和大宗商品出口价企稳等方面的提振,经济显现出稳步增长态势和强韧性的潜能。 本调研报告旨在深度挖掘东南亚市场的增长潜力与发展机会,分析东南亚市场竞争态势、销售模式、客户偏好、整体市场营商环境,为国内企业出海开展业务提供客观参考意见。 本文核心内容: 市场空间:全球行业市场空间、东南亚市场发展空间。 竞争态势:全球份额,东南亚市场企业份额。 销售模式:东南亚市场销售模式、本地代理商 客户情况:东南亚本地客户及偏好分析 营商环境:东南亚营商环境分析 本文纳入的企业包括国外及印尼本土企业,以及相关上下游企业等,部分名单 QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。邮箱:market@qyresearch.com

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值