设为首页 加入收藏

TOP

MethodInterceptor拦截器(一)
2015-07-20 17:45:37 来源: 作者: 【 】 浏览:4
Tags:MethodInterceptor 拦截

1.自定义一个annotation

package com.websystem.util;

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

/**
 * des:自定义使方法跳过拦截的注解
 * author: zbl
 * date: 2014年9月3日
 **/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public abstract @interface RequiredInterceptor{
	boolean required() default true;
}

2.在Controller里面的方法使用自定义的annotation,下面是一个登进登出的例子。

package com.websystem.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.websystem.model.ManagerModel;
import com.websystem.service.ManagerService;
import com.websystem.util.AESPlusHelper;
import com.websystem.util.Constant;
import com.websystem.util.RequiredInterceptor;

/**
 * des:
 * author: zbl
 * date: 2014年8月26日
 **/
@Controller
@SessionAttributes(Constant.Manager)
public class ManagerController {

	@Resource
	private ManagerService managerServiceImpl;

	@RequiredInterceptor(required = false)
	@RequestMapping(value = "manager/login.do",method = RequestMethod.GET)  
	public ModelAndView login(ManagerModel managerModel,ModelMap model){
		ManagerModel manager = managerServiceImpl.getManager(managerModel);
		
		if(manager!=null){
			manager.setPassword("");
			model.addAttribute(Constant.Manager, manager);
			return new ModelAndView(new RedirectView(Constant.MainURL));
		}else{
			return new ModelAndView(new RedirectView(Constant.LoginURL));
		}
	}
	
	@RequiredInterceptor(required = false)
	@RequestMapping(value = "manager/logout.do",method = RequestMethod.GET)
	@ResponseBody
	public Object logout(SessionStatus status){
		status.setComplete();
		
		Map
  
    map = new HashMap
   
    (); map.put(Constant.Success, true); return map; } }
   
  

3.定义MethodInterceptor,里面可以处理AOP逻辑代码。

package com.websystem.util;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

/**
 * des:
 * author: zbl
 * date: 2014年9月3日
 **/
@Component
public class SessionInterceptor implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		// TODO Auto-generated method stub
		RequiredInterceptor requiredInterceptor = AnnotationUtils.findAnnotation(invocation.getMethod(), RequiredInterceptor.class);
		if(requi
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU4064 Carcassonne(状态压缩DP) 下一篇链表反转(递归与非递归实现)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)
·Linux常用命令60条( (2025-12-25 00:50:40)
·nginx 监听一个端口 (2025-12-25 00:19:30)
·整个互联网就没有一 (2025-12-25 00:19:27)