Filter 过滤器详情(拦截所有请求进行过滤)

     **Filter 过滤器详情(拦截所有请求进行过滤)**



   @Component
    public class TokenFilter implements Filter {
        private static Logger logger = LoggerFactory.getLogger(TokenFilter.class);
    
        @Autowired
        private Assembly assembly;
    
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            String accessToken = httpServletRequest.getHeader("accessToken");
            String logout = httpServletRequest.getHeader("logout");
            if (StringUtils.isNotBlank(accessToken)&&StringUtils.isBlank(logout)) {
                Map parameters = assembly.getBeanId(accessToken);
                logger.info("获取到用户的BeanId信息" + JSON.toJSONString(parameters));
                httpServletRequest.setAttribute("beanId", parameters.get("beanId"));
                httpServletRequest.setAttribute("accessToken",parameters.get("token"));
            }
            chain.doFilter(request, response);
        }
    
        @Override
        public void destroy() {
    
        }
    }


package com..api.common;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.beantechs.tsp.boot.common.entity.Response;
import com.beantechs.tsp.boot.common.exception.NEException;
import com.beantechs.tsp.front.api.client.MessageCenterClient;
import com.beantechs.tsp.front.api.client.MessageClient;
import com.beantechs.tsp.front.api.client.UserClient;
import com.beantechs.tsp.front.api.client.VehicleCoreClient;
import com.beantechs.tsp.front.api.config.ConfigurationClass;
import com.beantechs.tsp.front.api.handler.CRUDHandler;
import com.beantechs.tsp.front.api.handler.RedisHandler;
import com.beantechs.tsp.front.api.utils.FrontUtils;
import com.beantechs.tsp.front.api.utils.JWTUtil;
import com.beantechs.tsp.front.api.vo.request.LoginNotification;
import com.beantechs.tsp.front.api.vo.request.userreq.MessageDTO;
import com.beantechs.tsp.front.api.vo.response.vehicleresp.AccountVehicleResp;
import com.beantechs.tsp.front.common.error.AppServiceError;
import io.fusionauth.jwt.domain.JWT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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


@Service
public class Assembly {

    private Logger logger= LoggerFactory.getLogger(Assembly.class);


    @Autowired
    private JWTUtil jwtutil;

    @Autowired
    private ConfigurationClass configurationClass;

    @Autowired
    private RedisHandler redisHandler;

    @Autowired
    private KafkaCallBackProducer kafkaCallBackProducer;

    private final static String TSTATUS="NOTOPEN";

    public void checkResultCode(String code,String resultCode){
        if (!resultCode.equals(code)){
            AppServiceError appServiceError = AppServiceError.getAppServiceError(code);
            throw new NEException(appServiceError);
        }
    }

    //解析登录token
    public Map getBeanId(String Token) {
        if (org.apache.commons.lang3.StringUtils.isBlank(Token))
            throw new NEException(AppServiceError.PARAMETER_EMPTY);

        //通过token解析UserId信息
        JWT jwt = jwtutil.parseJWT(Token);
        logger.info("jwt:"+JSON.toJSONString(jwt));
        String beanId = (String) jwt.getAllClaims().get("beanId");
        //根据手机号拿到用户登录时设置的token信息
        Map tokens = null;
        tokens = redisHandler.getHashValues(beanId);

        if (tokens == null || tokens.isEmpty())
            throw new NEException(AppServiceError.LOGIN_TOKEN_FAILURE);
        //判断token是否已经改变和失效
        String refreshToken = tokens.get("refreshToken");
        String accessToken = tokens.get("accessToken");
        if (!accessToken.equals(Token))
            throw new NEException(AppServiceError.REPEAT_LOGIN);
        boolean flag = false;
        try {
            jwt = jwtutil.decryptToken(accessToken);
        } catch (Exception e) {
            logger.error("accessToken已经失效");
            flag = true;
        }
        if (flag) {
            try {
                jwt = jwtutil.decryptToken(refreshToken);
            } catch (Exception e) {
                logger.error("refreshToken已经失效");
                redisHandler.deleteKey(beanId);
                throw new NEException(AppServiceError.LOGIN_TOKEN_FAILURE);
            }
  
        }

        tokens.put("beanId", beanId);
        tokens.put("token", accessToken);
        return tokens;

    }

    }

}

你可能感兴趣的:(Filter)