java ssm 自定义注解式请求头拦截器

1.创建@interface自定义注解

import java.lang.annotation.*;

@Target(ElementType.METHOD)//指定被修饰的Annotation可以放置的位置:方法上面
@Retention(RetentionPolicy.RUNTIME)//注解会在class字节码文件中存在,在运行时可以通过反射获取到
@Inherited//指定被修饰的Annotation将具有继承性
public @interface HandlerLJ {
    //public String url() default "";
    //可以随便写参数,在使用时调用
}

2.写一个实现HandlerInterceptor拦截器的类

import com.alibaba.fastjson.JSON;
import com.hx.slj.modules.zybGrzx.service.zybGrzxService;
import org.codehaus.jackson.map.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.security.auth.message.AuthException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import static com.sun.xml.internal.ws.api.message.Packet.State.ServerResponse;

public class HandlerLJInterceptor implements HandlerInterceptor {

    @Autowired
    private zybGrzxService zybGrzxService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean flag = false;
        //通过handler请求的方法再获取被请求的方法上的LoginInterceptor注解
        if (!(handler instanceof HandlerMethod)) { return true; } //没有注解就return true
        HandlerLJ handlerLJ = ((HandlerMethod)handler).getMethodAnnotation(HandlerLJ.class);
               //判断是否有HandlerLJ注解
       if (null == handlerLJ){
            flag = true;
        }else{
            //有注解说明我们需要进行登录验证的处理
            String YzToken = request.getHeader("token");
            //String userToken = zybGrzxService.get("").getToken();
            if("".equals(YzToken)){//|| userToken.equals(YzToken)
                PrintWriter pw = response.getWriter();
                //handlerLJ.url();
                flag = false;
                Map<String,Object> map = new HashMap<>();
                map.put("code","1");
                map.put("message","请先登录!");
                //System.err.println(handlerLJ.url());
               // response.sendRedirect(request.getContextPath()+ handlerLJ.url());
                //redirect(request,response,handlerLJ.url());
                pw.write(JSON.toJSONString(map));
                pw.flush();
                pw.close();
            }else{
                flag = true;
            }
            System.err.println(flag);
        }
        return flag;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}

3.在spring-mvc.xml中配置拦截器

	<mvc:interceptors>
		<!-- 多个拦截器,顺序执行 -->
		<mvc:interceptor>
			<mvc:mapping path="${adminPath}/**"/>
			<!--被排除拦截的资源-->
			<mvc:exclude-mapping path="${adminPath}/zybGrzx/grzxLogin"/>
			<mvc:exclude-mapping path="${adminPath}/zybGrzx/grzxSubmit"/>
			<bean class="com.hx.slj.modules.zybGrzx.config.HandlerLJInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>

4.类中调用

    @RequestMapping("/test")
    @HandlerLJ  //自定义的注解
    @ResponseBody
    public Map<String,Object> test(){
        Map<String,Object> map = new HashMap<>();
        map.put("data","成功!");
        return map;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值