0
点赞
收藏
分享

微信扫一扫

springBoot自定义拦截器

编写FuelH5InterceptorConfig配置类

package com.fuel.framework.config;

import com.fuel.framework.interceptor.FuelH5Interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 通用配置
 * 
 * @author hhxx
 */
@Configuration
public class FuelH5InterceptorConfig implements WebMvcConfigurer
{
    @Autowired
    private FuelH5Interceptor fuelH5Interceptor;

    /**
     * 自定义拦截规则
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(fuelH5Interceptor)
                .addPathPatterns("/api/wx/**")    //拦截所有请求 通过判断token是否合法来决定是否访问
                .addPathPatterns("/api/personal/**")
                .addPathPatterns("/api/scan/**")
                .excludePathPatterns("/api/wx/getToken")
                .excludePathPatterns("/api/scan/login")
                .excludePathPatterns("/api/scan/wxCallBackInfo");
    }

}

编写FuelH5Interceptor处理类

package com.fuel.framework.interceptor;

import com.alibaba.fastjson2.JSON;
import com.fuel.common.config.JWTConfig;
import com.fuel.common.constant.Constants;
import com.fuel.common.constant.HttpStatus;
import com.fuel.common.core.domain.AjaxResult;
import com.fuel.common.utils.ServletUtils;
import com.fuel.common.utils.StringUtils;
import com.fuel.common.utils.spring.SpringUtils;
import com.fuel.framework.utils.jwt.JwtUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class FuelH5Interceptor implements HandlerInterceptor {

    // 令牌自定义标识
    @Value("${token.header}")
    private String header;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        //获取token
        String requestURI = request.getRequestURI();
        String token = request.getHeader(header);
        if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX))
        {
            token = token.replace(Constants.TOKEN_PREFIX, "");
        }
        JWTConfig jwtConfig = SpringUtils.getBean(JWTConfig.class);
        if (StringUtils.isNotBlank(token)) {
            try {
                JwtUtils.verify(jwtConfig.getUserName(), jwtConfig.getPassWord(), token);
            }  catch (Exception e) {
                ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(HttpStatus.UNTOKEN, "token过期")));
                return false;
            }
        }else {
            ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(HttpStatus.UNTOKEN, "请求未携带token")));
            return false;
//            throw new ServiceException("请求未携带token", HttpStatus.UNTOKEN);
        }

        return true;
    }
}

举报

相关推荐

0 条评论