以Bean结尾,表示它是一个Bean,不同于普通Bean的是:它是实现了FactoryBean
例如自己实现一个FactoryBean,功能:用来代理一个对象,对该对象的所有方法做一个拦截,在调用前后都输出一行LOG,模仿ProxyFactoryBean的功能。
shiro中就利用facoryBean的getObject()返回Ini.Section对象注入动态权限
/**
* my factory bean
* 代理一个类,拦截该类的所有方法,在方法的调用前后进行日志的输出
* @author daniel.zhao
*
*/
public class MyFactoryBean implements FactoryBean
private static final Logger logger = LoggerFactory.getLogger(MyFactoryBean.class);
private String interfaceName;
private Object target;
private Object proxyObj;
@Override
public void destroy() throws Exception {
logger.debug("destroy......");
}
@Override
public void afterPropertiesSet() throws Exception {
proxyObj = Proxy.newProxyInstance(
this.getClass().getClassLoader(),
new Class[] { Class.forName(interfaceName) },
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
logger.debug("invoke method......" + method.getName());
logger.debug("invoke method before......" + System.currentTimeMillis());
Object result = method.invoke(target, args);
logger.debug("invoke method after......" + System.currentTimeMillis());
return result;
}
});
logger.debug("afterPropertiesSet......");
}
@Override
public Object getObject() throws Exception {
logger.debug("getObject......");
return proxyObj;
}
@Override
public Class> getObjectType() {
return proxyObj == null ? Object.class : proxyObj.getClass();
}
@Override
public boolean isSingleton() {
return true;
}
public String getInterfaceName() {
return interfaceName;
}
public void setInterfaceName(String interfaceName) {
this.interfaceName = interfaceName;
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
public Object getProxyObj() {
return proxyObj;
}
public void setProxyObj(Object proxyObj) {
this.proxyObj = proxyObj;
}
}
XML-Bean配置如下
12 3 4
@RunWith(JUnit4ClassRunner.class)
@ContextConfiguration(classes = { MyFactoryBeanConfig.class })
public class MyFactoryBeanTest {
@Autowired
private ApplicationContext context;
/**
* 测试验证FactoryBean原理,代理一个servcie在调用其方法的前后,打印日志亦可作其他处理
* 从ApplicationContext中获取自定义的FactoryBean
* context.getBean(String beanName) ---> 最终获取到的Object是FactoryBean.getObejct(),
* 使用Proxy.newInstance生成service的代理类
*/
@Test
public void testFactoryBean() {
HelloWorldService helloWorldService = (HelloWorldService) context.getBean("fbHelloWorldService");
helloWorldService.getBeanName();
helloWorldService.sayHello();
}
}
shiro示例:
/flex/rbac/preLogin.action=authc
/j_acegi_security_check=authc
/flex/uifrm/index.jsp=authc
/flex/rbac/getLoginIndex.action=authc
/logout = logout
/user/logout.action = logout
/flex/rbac/preLogin.action=authc
/j_acegi_security_check=authc
/flex/uifrm/index.jsp=authc
/flex/rbac/getLoginIndex.action=authc
/logout = logout
/user/logout.action = logout
package com.houbank.incoming.web.shrio;
import com.alibaba.dubbo.config.annotation.Reference;
import com.houbank.basic.util.response.BaseResponse;
import com.houbank.incoming.api.FinancialSalesFuncFacade;
import com.houbank.incoming.api.FinancialSalesRoleFacade;
import com.houbank.incoming.model.condition.FinancialSalesFuncCondition;
import com.houbank.incoming.model.condition.FinancialSalesRoleCondition;
import com.houbank.incoming.model.condition.FinancialSalesUserRoleCondition;
import com.houbank.incoming.model.domain.FinancialSalesFunc;
import com.houbank.incoming.model.domain.FinancialSalesFuncRole;
import com.houbank.incoming.model.domain.FinancialSalesRole;
import com.houbank.incoming.web.controller.FinancialSalesUserController;
import org.apache.shiro.config.Ini;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.*;
public class ChainDefinitionSectionMetaSource implements FactoryBean
private String filterChainDefinitions;
@Autowired(required=false)
private RedisTemplate redisTemplate;
@Reference
private FinancialSalesFuncFacade financialSalesFuncFacade;
@Reference
private FinancialSalesRoleFacade financialSalesRoleFacade;
/**
* 默认premission字符串
*/
public static final String PREMISSION_STRING="perms[\"{0}\"]";
public Ini.Section getObject() throws BeansException {
//获取所有Resource
Map
try {
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return getSectionService(filterChainDefinitions) ;
}
public Ini.Section getSectionService(String filterChainDefinitions) {
List
List
FinancialSalesUserRoleCondition financialSalesRoleCondition =new FinancialSalesUserRoleCondition();
FinancialSalesFuncCondition financialSalesFuncCondition =new FinancialSalesFuncCondition();
list = financialSalesRoleFacade.selectAll();
List
BaseResponse> listfuncs= financialSalesFuncFacade.selectAll(financialSalesFuncCondition);
List
Set
Set
for (Iterator
Map resource = (Map)it1.next();////角色菜单信息
itmls.add(resource.get("funcId")+"");
}
Ini ini = new Ini();
ini.load(filterChainDefinitions);
Ini.Section section = ini.getSection(Ini.DEFAULT_SECTION_NAME);
Map
Set
Set
if(list!=null&&list.size()>0){
//双重循环讲角色和对应的菜单对应
for(int i = 0; i < listfunc.size(); i++){
FinancialSalesFunc r = (FinancialSalesFunc)listfunc.get(i);
roleIds=new HashSet
mlroleIds=new HashSet
for (Iterator
Map resource = it.next();
if(r.getId().equals(resource.get("funcId"))){
//如果不为空值添加到section中
roleIds.add(resource.get("roleId")+"");
}
}
if(!"".equals(r.getUrl()) && null!=r.getUrl()){
mapr.put(r.getUrl()+"", roleIds);
}
}
}
StringBuffer au=null;
String st="";
//匹配到的转化为要求格式
for(Map.Entry
au = new StringBuffer();
au.append("authc,role[");
for(String a :en.getValue()){
au.append("\""+a+"\",");
}
String strau=au.substring(0,au.lastIndexOf(","));
if(strau.contains(",role[")){
strau+="]";
}
st+=en.getKey()+">>>>"+strau+"\n";
section.put(en.getKey(),strau);
}
System.out.println(st);
return section;
}
/**
* 通过filterChainDefinitions对默认的url过滤定义
*
* @param filterChainDefinitions 默认的url过滤定义
*/
public void setFilterChainDefinitions(String filterChainDefinitions) {
this.filterChainDefinitions = filterChainDefinitions;
}
public Class> getObjectType() {
return this.getClass();
}
public boolean isSingleton() {
return false;
}
public FinancialSalesFuncFacade getFinancialSalesFuncFacade() {
return financialSalesFuncFacade;
}
public void setFinancialSalesFuncFacade(FinancialSalesFuncFacade financialSalesFuncFacade) {
this.financialSalesFuncFacade = financialSalesFuncFacade;
}
public FinancialSalesRoleFacade getFinancialSalesRoleFacade() {
return financialSalesRoleFacade;
}
public void setFinancialSalesRoleFacade(FinancialSalesRoleFacade financialSalesRoleFacade) {
this.financialSalesRoleFacade = financialSalesRoleFacade;
}
}