Spring Security教程(12)---- 使用数据库来管理方法

这个稍微有一点复杂,我是通过AOP来实现的,前半部分跟上一章类似,主要在配置上有点不同

读取方法与权限对应列表DAO

	public List> getMethodResourceMapping(){
		String sql = "SELECT S3.RESOURCE_PATH,S2.AUTHORITY_MARK FROM SYS_AUTHORITIES_RESOURCES S1 "+
				"JOIN SYS_AUTHORITIES S2 ON S1.AUTHORITY_ID = S2.AUTHORITY_ID "+
				"JOIN SYS_RESOURCES S3 ON S1.RESOURCE_ID = S3.RESOURCE_ID AND S3.RESOURCE_TYPE='METHOD' ORDER BY S3.PRIORITY DESC";
		
		List> list = new ArrayList>();
		
		Query query = this.entityManager.createNativeQuery(sql);
		List result = query.getResultList();
		Iterator it = result.iterator();
		
		while(it.hasNext()){
			Object[] o = it.next();
			Map map = new HashMap();
			map.put("resourcePath", (String)o[0]);
			map.put("authorityMark", (String)o[1]);
			list.add(map);
		}
		
		return list;
	}
这里只针对方法名拦截,对于同一个类中的同名方法统统作为一个方法拦截。如果需要更细粒度的拦截,即拦截到参数,请在完成这个内容之后自行进行深入的研究。

MethodKey用来做主键用的

public class MethodKey {
	private String className;
	private String methodName;
	
	public MethodKey(){};
	
	public MethodKey(String fullName){
		this.className = StringUtils.stripFilenameExtension(fullName);
		this.methodName = StringUtils.getFilenameExtension(fullName);
	};
	
	public MethodKey(Method method) {
		super();
		this.className = method.getDeclaringClass().getName();
		this.methodName = method.getName();
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getMethodName() {
		return methodName;
	}
	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
	
	public String getFullMethodName(){
		return this.className + "." + this.methodName;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof MethodKey))return false;
		
		MethodKey target = (MethodKey)obj;
		
		if(this.className.equals(target.getClassName()) && 
				this.methodName.equals(target.getMethodName()))return true;
		
		return false;
	}
	
}
getDeclaringClass获取到的是接口,这里我们只拦截接口。

MethodSecurityMetadataSource

public class MethodSecurityMetadataSource extends
		AbstractMethodSecurityMetadataSource implements InitializingBean{
	
	//protected Log logger = LogFactory.getLog(getClass());
	
	private final static List NULL_CONFIG_ATTRIBUTE = Collections.emptyList();
	
	private final static String RES_KEY = "resourcePath";
	private final static String AUTH_KEY = "authorityMark";
	
	private Map> requestMap;
	
	@Autowired
	private SysResourceRepository sysResourceRepository;
	
	/**
	 * 根据方法获取到访问方法所需要的权限
	 * @param method 访问的方法
	 * @param targetClass 方法所属的类
	 */
	@Override
	public Collection getAttributes(Method method,
			Class targetClass) {
		MethodKey key = new MethodKey(method);
		Collection attrs = NULL_CONFIG_ATTRIBUTE;
		
		for (Map.Entry> entry : requestMap.entrySet()) {
            if (entry.getKey().equals(key)) {
            	attrs =  entry.getValue();
            	break;
            }
        }
		logger.info("METHOD资源:"+key.getFullMethodName()+ " -> " +attrs);
		
		return attrs;
	}

	/**
	 * 获取到所有方法对应的权限集合
	 */
	@Override
	public Collection getAllConfigAttributes() {
		Set allAttributes = new HashSet();

        for (Map.Entry> entry : requestMap.entrySet()) {
            allAttributes.addAll(entry.getValue());
        }

        return allAttributes;
	}

	/**
	 * 初始化方法权限对应集合,绑定方法权限集合
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		this.requestMap = this.bindRequestMap();
	}
	
	/**
	 * 从数据库中获取方法及权限对应信息
	 * @return
	 */
	private Map loadMehod(){
		Map resMap = new LinkedHashMap();
		List> list = this.sysResourceRepository.getMethodResourceMapping();
		
		for(Map map : list){
			String resourcePath = map.get(RES_KEY);
			String authorityMark = map.get(AUTH_KEY);
			
			if(resMap.containsKey(resourcePath)){
				String mark = resMap.get(resourcePath);
				resMap.put(resourcePath, mark+","+authorityMark);
			}else{
				resMap.put(resourcePath, authorityMark);
			}
		}
		
		return resMap;
	}
	
	/**
	 * 封装从数据库中获取的方法权限集合
	 * @return
	 */
	public Map> bindRequestMap(){
		Map> resMap = 
				new LinkedHashMap>();
		
		Map map = this.loadMehod();
		for(Map.Entry entry : map.entrySet()){
			MethodKey key = new MethodKey(entry.getKey());
			Collection atts = 
					SecurityConfig.createListFromCommaDelimitedString(entry.getValue());
			
			resMap.put(key, atts);
		}
		
		return resMap;
	}

}
与资源的SecurityMetadataSource类似,只不过拦截方法的SecurityMetadataSource需要继承自AbstractMethodSecurityMetadataSource或实现MethodSecurityMetadataSource

具体配置

	
		
		
		
	
	
	
	
	
		
		
	

首先创建pointcut,pointcut是项目中的Service层。然后在advisor中配置拦截器及切面的对应关系

方法的拦截是通过AOP来实现的,方法的拦截到此结束

有资源的拦截和方法的拦截基本上就能保证项目的权限能够灵活分配了。



你可能感兴趣的:(Spring Security教程(12)---- 使用数据库来管理方法)