Springboot 中配置使用拦截器

内容


1. SpringMVC 拦截器使用流程

在这里插入图片描述

2. CheckLoginInterceptor 拦截器

package com.yy.springboot.interceptor;

import com.alibaba.fastjson.JSON;
import com.yy.springboot.annotation.RequireLogin;
import com.yy.springboot.domain.UserInfo;
import com.yy.springboot.service.IUserInfoService;
import com.yy.springboot.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

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

@Component
public class CheckLoginInterceptor implements HandlerInterceptor {
    @Autowired
    private IUserInfoService userInfoService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		/**
		 * 放行跨域请求,没有跨域可以不写下面的第一个 if
		 */
        // 1:静态资源处理对象:ResourceHanler?
        // 2:跨域的预请求处理对象:PreFlightHandler
        // 3:动态请求处理对象:HandlerMethod
        // 跨域处理,不是动态请求(域请求)就放行,动态请求进不去
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
		
		// 根据 Controller 控制器方法头顶是否贴了RequireLogin 自定义注解判断是否需要登录;
		// 1:贴了自定义注解,需要登录后操作;2:没贴自定义注解,可以直接访问
        HandlerMethod hm = (HandlerMethod) handler;
        RequireLogin annotation = hm.getMethodAnnotation(RequireLogin.class);

        String token = request.getHeader("token");
        UserInfo user = userInfoService.getCurrentUser(token);

        if (annotation != null) {
            if (user == null) {
                response.setContentType("application/json;charset=utf-8");
                response.getWriter().write(JSON.toJSONString(JsonResult.noLogin()));
                return false;
            }
        }
        return true;
    }
}

3. SpringBoot 配置类

package com.yy.springboot.config;

import com.yy.springboot.interceptor.CheckLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private CheckLoginInterceptor checkLoginInterceptor;

    /**
     * 配置拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(checkLoginInterceptor)
            	// 拦截所有方法
                .addPathPatterns("/**")
            	// 放行指定方法
                .excludePathPatterns("/users/checkPhone")
                .excludePathPatterns("/users/sendVerifyCode")
                .excludePathPatterns("/users/regist")
                .excludePathPatterns("/users/login");
    }
}

总结

上面就是拦截器的使用了,代码仅供参考,欢迎讨论交流。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值