UserCenterHanlerInterceptor.java 3.7 KB
package com.tianbo.analysis.intercept;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tianbo.analysis.feign.UserCenterAPI;
import com.tianbo.analysis.model.ResultJson;
import com.tianbo.analysis.thread.SessionUserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

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


@Slf4j
@Component
public class UserCenterHanlerInterceptor implements HandlerInterceptor {

    private static  UserCenterHanlerInterceptor _THIS;

    @Autowired
    private Environment environment;

    @Autowired
    private UserCenterAPI userCenterAPI;


    @PostConstruct
    public void init(){
        _THIS = this;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String serviceName = _THIS.environment.getProperty("spring.application.name");
        String permPath = "/" + serviceName + request.getRequestURI();
        log.info("path = {}, serviceName= {}",request.getRequestURI(),serviceName);
        AntPathMatcher pathMatcher = new AntPathMatcher();
        boolean match = pathMatcher.match(permPath, request.getRequestURI());

        String authHeader = request.getHeader("Authorization");
        if (authHeader != null && authHeader.startsWith("Bearer ")) {

            ResultJson resultJson = _THIS.userCenterAPI.getDataPerm(permPath, authHeader);
            if (resultJson.getData()!=null){

            }
            JSONObject user = (JSONObject) JSON.toJSON(resultJson.getData());
            log.info(resultJson.toString());
        }

        /**
         * 根据访问接口地址和token信息获取用户权限信息
         * 参数为request 中的接口地址和的 auth 的token信息
         * 样例实体类如下:
         */

        String userDataPermJsonDemoStr = "{\n" +
                "\t\"username\":\"nmms\",\n" +
                "\t\"userId\": \"80\",\n" +
                "\t\"userDataPerm\":{\n" +
                "\t\t\"rowCondition\": \"usr\",\n" +
                "\t\t\"rowDataPermConditions\":[\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"colName\": \"USERNAME\"\n" +
                "\t\t\t},\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"colName\": \"BILLNO\"\n" +
                "\t\t\t}\n" +
                "\t\t],\n" +
                "\t}\t\n" +
                "}";
        JSONObject userInfo = JSONObject.parseObject(userDataPermJsonDemoStr);


        JSONObject userDataPerm = userInfo.getJSONObject("userDataPerm");
        String rowCondition = userDataPerm.getString("rowCondition");
        JSONArray rowDataPermConditions = userDataPerm.getJSONArray("rowDataPermConditions");

        JSONObject userDataPermModel = new JSONObject();
        userDataPermModel.put("colName","USERNAME");

        //获取token中的user的值
        userDataPermModel.put("colValue","nmms");
//        JSONObject userDataPermModel1 = new JSONObject();
//        userDataPermModel1.put("colName","BILLNO");
//        userDataPermModel1.put("colValue","78464771722");

        JSONArray userDataPermArray = new JSONArray();
        userDataPermArray.add(userDataPermModel);
//        userDataPermArray.add(userDataPermModel1);
        SessionUserContext.setSessionUser(userDataPermArray);
        log.info("进入HTTPServletRequest 拦截器");
        return true;
    }
}