spring 流入
spring 注入:
1,配置文件注入
(一)bean
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="authenticationRealm" />
<!-- <property name="cacheManager" ref="shiroCacheManager" /> -->
</bean>
<bean id="authenticationRealm" class="com.esteel.common.AuthenticationRealm">
<property name="authorizationCacheName" value="authorization" />//这部分也是字符串注入
</bean>
(二)字符串注入
<bean id="myShiroFilterFactory" class="com.esteel.common.MyShiroFilterFactory">
<property name="filterChainDefinitions">
<value>
/admin/ = anon
/index/ = anon
/index = anon
/login = anon
/logout = logout
/getRandomValidateCode = anon
/verifyCode = anon
/admin/** = anon
/main**=authc
/ui/info**=authc
/ui/listUser**=authc,perms[admin:manage]
/dwzIndex**=authc,perms[admin:manage]
</value>
</property>
</bean>
public class MyShiroFilterFactory {
private static Logger logger = LoggerFactory
.getLogger(MyShiroFilterFactory.class);
private String filterChainDefinitions;
/**
* 通过filterChainDefinitions对默认的url过滤定义
*
* @param filterChainDefinitions 默认的url过滤定义
*/
public void setFilterChainDefinitions(String filterChainDefinitions) {
this.filterChainDefinitions = filterChainDefinitions;
}
bean注入的一种高级用法,用了泛型即注入的对象及其类型是spring自动调用实现他的被注入对象的getObject得到的类型对象
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/ui/login.jsp" />
<property name="successUrl" value="/ui/dwzIndex.jsp" />
<property name="unauthorizedUrl" value="/ui/accessDenied.jsp" />
//这里注入的对象不是ChainDefinitionSectionMetaSour而是他实现的getObject方法返回的对象Ini.Section,他实际上就是一个map,不知知道这点会导致注入失败,Section实现map接口
所以直接注给map即可
<property name="filterChainDefinitionMap" ref="chainDefinitionSectionMetaSource" />
<property name="filters">
<map>
<entry key="authc" value-ref="authenticationFilter" />
<entry key="role" value-ref="roleAuthorizationFilter" />
</map>
</property>
</bean>
<bean id="chainDefinitionSectionMetaSource" class="com.esteel.common.ChainDefinitionSectionMetaSource">
public class ShiroFilterFactoryBean
/* */ implements FactoryBean, BeanPostProcessor
/* */ {
/* */
/* */ private Map<String, String> filterChainDefinitionMap;
public Map<String, String> getFilterChainDefinitionMap()
/* */ {
/* 295 */ return this.filterChainDefinitionMap;
/* */ }
public void setFilterChainDefinitionMap(Map<String, String> filterChainDefinitionMap)
/* */ {
/* 308 */ this.filterChainDefinitionMap = filterChainDefinitionMap;
/* */ }
}
package com.esteel.common;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.shiro.config.Ini;
import org.apache.shiro.config.Ini.Section;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import com.esteel.system.bean.OpmMenuitem;
import com.esteel.system.bean.OpmMenuitemlimit;
import com.esteel.system.beanVo.OpmRolelimitVo;
import com.esteel.system.service.OpmMenuitemService;
import com.esteel.system.service.OpmMenuitemlimitService;
import com.esteel.system.service.OpmRolelimitService;
public class ChainDefinitionSectionMetaSource implements FactoryBean<Ini.Section>{
// @Autowired
// private ResourceDao resourceDao;
@Autowired
private OpmRolelimitService opmRolelimitService;
@Autowired
private OpmMenuitemlimitService opmMenuitemlimitService;
@Autowired
private OpmMenuitemService opmMenuitemService;
private String filterChainDefinitions;
/**
* 默认premission字符串
*/
public static final String PREMISSION_STRING="perms[\"{0}\"]";
public Section getObject() throws BeansException {
//获取所有Resource
List<OpmRolelimitVo> list =new ArrayList<OpmRolelimitVo>();
List<OpmRolelimitVo> listAll =new ArrayList<OpmRolelimitVo>();
list= opmRolelimitService.getRoleLimtContro();
OpmMenuitem item = new OpmMenuitem();
List<OpmMenuitem> items =opmMenuitemService.getMenuItems(item);
OpmMenuitemlimit opmMenuitemlimit = new OpmMenuitemlimit();
List<OpmMenuitemlimit> opmMenuitemlimits =opmMenuitemlimitService.getOpmMenuitemlimit(opmMenuitemlimit);
List<OpmMenuitem> itemsb =new ArrayList<OpmMenuitem>();
List<OpmMenuitemlimit> opmMenuitemlimitsb =new ArrayList<OpmMenuitemlimit>();
Set<String> itms= new HashSet<String>();
Set<String> itmls= new HashSet<String>();
for (Iterator<OpmRolelimitVo> it1 = list.iterator(); it1.hasNext();) {
OpmRolelimitVo resource = it1.next();
if(resource.getMenuitemid()!=null){
//如果不为空值添加到section中
if(!"".equals(resource.getMenuitemid()) &&!"".equals(resource.getMenuitemid())) {
itms.add(resource.getMenuitemid());
}
}
if(resource.getMenuitemlimitid()!=null&&(resource.getMenuitemlimitid()!=null)&&!"".equals(resource.getMenuitemlimitid())){
itmls.add(resource.getMenuitemlimitid());
}
}
for(String im :itms){
OpmMenuitem o = new OpmMenuitem();
o.setId(im);
itemsb.add(o);
}
for(String im :itmls){
OpmMenuitemlimit o = new OpmMenuitemlimit();
o.setId(im);
opmMenuitemlimitsb.add(o);
}
items.removeAll(itemsb);
opmMenuitemlimits.removeAll(opmMenuitemlimitsb);
Ini ini = new Ini();
//加载默认的url
ini.load(filterChainDefinitions);
Ini.Section section = ini.getSection(Ini.DEFAULT_SECTION_NAME);
//循环Resource的url,逐个添加到section中。section就是filterChainDefinitionMap,
//里面的键就是链接URL,值就是存在什么条件才能访问该链接
Map<String,Set<String>> mapr = new HashMap<String,Set<String>>();
Set<String> roleIds=null;
Set<String> mlroleIds=null;
if(list!=null&&list.size()>0){
for(OpmRolelimitVo r :list){
roleIds=new HashSet<String>();
mlroleIds=new HashSet<String>();
for (Iterator<OpmRolelimitVo> it = list.iterator(); it.hasNext();) {
OpmRolelimitVo resource = it.next();
if(r.getMenuitemid().equals(resource.getMenuitemid())){
//如果不为空值添加到section中
if(!"".equals(resource.getMenuitemid()) &&!"".equals(resource.getMenuitemid())) {
roleIds.add(resource.getRoleid());
}
}
if(r.getMenuitemlimitid()!=null&&resource.getMenuitemlimitid()!=null&&(r.getMenuitemlimitid().equals(resource.getMenuitemlimitid()))){
mlroleIds.add(resource.getRoleid());
}
}
//这里可以直接转set用section
mapr.put(r.getMuri(), roleIds);
mapr.put(r.getMluri(), mlroleIds);
}
}
StringBuffer au=null;
String st="";
for(Map.Entry<String, Set<String>> en:mapr.entrySet()){
au = new StringBuffer();
au.append("authc,role[");
for(String a :en.getValue()){
au.append("\""+a+"\",");
}
String strau=au.substring(0,au.lastIndexOf(","));
strau+="]";
st+=en.getKey()+">>>>"+strau+"\n";
section.put(en.getKey()+"**",strau);
}
// section.put("dwzIndex", MessageFormat.format(PREMISSION_STRING, "authc,perms[admin:manage]"));格式错了
//
// section.put(resource.getMenuitemid(), MessageFormat.format(PREMISSION_STRING, resource.getMenuitemid()));
// section.put("/system/tbBasBed/list**","authc,perms[admin:manage]");
System.out.println(st);
for(OpmMenuitem i: items){
section.put(i.getUri()+"**","authc,role[tempr]");
}
for(OpmMenuitemlimit i: opmMenuitemlimits){
section.put(i.getUri()+"**","authc,role[tempr]");
}
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;
}
}
2注解注入:
public class AuthenticationRealm extends AuthorizingRealm {
@Autowired
private OpmUserService opmUserService;
@Autowired
private OpmUserRoleService opmUserRoleService;
@Autowired
private OpmRolelimitService opmRolelimitService;}
3,构造方法注入:
注意:注入接口的时候注解将实现类赋值给接口