SpringMVC给控制器添加自定义注解控制访问权限

1、创建自定义组件
package com.qingbo.ginkgo.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MenuO {
	/** 菜单名称 */
	String name();
	/** 菜单Code */
	String code();
	/** 父菜单Code */
	String parent() default "--";
	/** 等级  */
	int leven() default 1;
	/** 事件类型link,affiliated  */
	String clickType();
	/** 菜单类型 */
	String type();
	/** 菜单权限类型 */
	int power() default 0;
	/** 排序 */
	int order() default 999;
	/** 图标 */
	String iconCls();
	/** 是否在列表显示0显示,1隐藏 */
	int isShow() default 0;
	/** 菜单的唯一标示**/
	String iconName();
	/** 备注 */
	String remark() default "";
}
2.在Controller中使用该自定义注解
@RequestMapping("facade")
@MenuO(name="代理商管理",code="agent-facade",parent="information-manager",type = MenuType.TYPE_WEB,leven=1,clickType=MenuType.LEVEN_LINK,iconName="agent",remark="平台", iconCls = "icon-codepen",order=12)
public String facade(SystemUser systemUser, Model model, String vague, HttpServletRequest request){return ""}

 

 

3.创建拦截器获得所有注解,然后创建和修改注解数据

 

/**
 * 
 */
package com.qingbo.onem2.web;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qingbo.ginkgo.annotation.MenuO;
import com.qingbo.ginkgo.base.service.SystemAddressService;
import com.qingbo.ginkgo.common.util.Config;
import com.qingbo.ginkgo.common.util.SimpleLogFormater;
import com.qingbo.ginkgo.helper.XmlUtil;
import com.qingbo.onem2.dto.AddressStatic;
import com.qingbo.onem2.dto.BankBinStatic;
import com.qingbo.onem2.web.biz.BankBinBizService;
import com.qingbo.onem2.web.biz.SystemFunctionBizService;

/**
 * 资源菜单,启动初始化
 * 功能:自动读取<@Controller>层的<@Menu>注解,进行菜单的配置
 * @author 贺亮
 *
 */
public class MenuInitProcessor implements BeanPostProcessor{
	private static Log log = LogFactory.getLog(MenuInitProcessor.class);
	
	@Autowired private SystemFunctionBizService systemFunctionBizService;
	@Autowired private SystemAddressService systemAddressService;
	@Autowired private BankBinBizService bankBinBizService;
	
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)throws BeansException {
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)throws BeansException {
		//如果注册的bean不属于Controller,直接跳出
		if (!bean.getClass().isAnnotationPresent(Controller.class)) {
		      return bean;
		}
		//得到父地址映射
		String urlMapping = null;
		if(bean.getClass().isAnnotationPresent(RequestMapping.class)){
			RequestMapping classMapping= bean.getClass().getAnnotation(RequestMapping.class);
			urlMapping = classMapping.value()[0];
		}
		//得到Controller下的所有方法,判断是否有注册信息
		Method[] methods = bean.getClass().getDeclaredMethods();
		for(Method method:methods){
			if(method.isAnnotationPresent(MenuO.class)){
				//开始注册信息
				MenuO menu = method.getAnnotation(MenuO.class);
				//log.info(SimpleLogFormater.formatResult("----->Menu<----- register local menu : "+menu.name()+":"+urlMapping));
				//得到子地址映射
				if(method.isAnnotationPresent(RequestMapping.class)){
					RequestMapping methodMapping= method.getAnnotation(RequestMapping.class);
					if(urlMapping!=null){
						this.systemFunctionBizService.insertLocalMenu(menu,urlMapping+"/"+methodMapping.value()[0]);
					}else{
						this.systemFunctionBizService.insertLocalMenu(menu,methodMapping.value()[0]);
					}
				}
			}
		}
		//初始化静态字典
		new AddressStatic(systemAddressService);
		new BankBinStatic(bankBinBizService);
		return bean;
	}
}


4.配置拦截器

 

 

 

 

 

具体代码解释后期整理!

有疑问请下方留言!

你可能感兴趣的:(问题或解决方案收集)