springMVC 自定义拦截器

1:spring.xml配置拦截器

 <mvc:interceptors>
    	<!-- 登陆拦截 -->
    	<mvc:interceptor>
    		<mvc:mapping path="/**"/>
    		<bean class="com.common.interceptor.MyAuthorizationInterceptor">
    		</bean>
    	</mvc:interceptor>
</mvc:interceptors>

2:java代码的配置

1):Authorization注解的配置

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

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Authorization {
	
	/**
     * 是否检查登陆状态
     *     yes:检查
     *     no:不检查
     */
	String checkLogin() default "yes";
	
}
2):AuthorizationInterceptor

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public abstract class AuthorizationInterceptor implements HandlerInterceptor {

	protected final static Logger log = LoggerFactory.getLogger("tl.user.login");

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		HandlerMethod handlerMethod = (HandlerMethod) handler;
		Method method = handlerMethod.getMethod();
		// 判断是否有注解
		Authorization authorization = method.getAnnotation(Authorization.class);
		// 判读类是否有注解
		if (authorization == null) {
			authorization = method.getDeclaringClass().getAnnotation(Authorization.class);
		}
		if (authorization == null) {
			return true;
		}
		return true;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("2");

	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("3");

	}

	/**
	 * 页面重定向,如果ref为空,默认跳转到登陆页面。
	 */
	protected abstract void sendRedirect(HttpServletRequest request, HttpServletResponse response, Object handler,
			String ref) throws Exception;

}

3):在controller上引用注解

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.common.interceptor.Authorization;

@Controller
@RequestMapping("/")
public class HomeController {
	
	@RequestMapping("/home")
	public String getHome(Map<String, Object> context){
		context.put("home", "欢迎来到我的世界!!!");
		return "index";
	}
	
	@RequestMapping("/auth")
	@Authorization
	public String auth(Map<String, Object> context){
		context.put("home", "欢迎来到我的世界!!!1");
		return "index";
	}
	
}

3:常见问题 

1:HandlerMethod handlerMethod = (HandlerMethod) handler;如果报错 请检查 注解配置  将<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />

加上

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值