com.xxd.web.interceptors.CompanyAuthInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.xxd.web.interceptors.CompanyAuthInterceptor.java

Source

package com.xxd.web.interceptors;

import com.alibaba.fastjson.JSONObject;
import com.xxd.common.util.CookieUtil;
import com.xxd.common.util.HttpHelper;
import com.xxd.common.util.JsonUtil;
import com.xxd.constant.Constant;
import com.xxd.enums.UserTypeEnum;
import com.xxd.ha.hystrix.command.usercenter.EnterpriseAuthProgressCommand;
import com.xxd.ha.hystrix.vo.usercenter.AccountUserInfoVo;
import com.xxd.ha.hystrix.vo.usercenter.EnterpriseProgressVo;
import com.xxd.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

public class CompanyAuthInterceptor implements HandlerInterceptor {

    @Autowired
    UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object o) throws Exception {
        String token = CookieUtil.getCookieValue(req, Constant.TOKEN);
        String ua = HttpHelper.getUserAgent(req);
        // ?URI ??
        String accountUri = "/usercenter/company/account.html";
        String authUri = "/usercenter/company/authentication.html";
        String loginUri = "/usercenter/company/login.html";
        String registerUri = "/usercenter/company/register.html";
        String licenseUri = "/usercenter/company/license.html";

        // ??
        boolean isLoginPage = StringUtils.equals(req.getRequestURI(), loginUri)
                || StringUtils.equals(req.getRequestURI(), registerUri);
        if (StringUtils.isEmpty(token) || !userService.isLogin(token, ua)) {
            if (isLoginPage) {
                // ?
                return true;
            }
            // 
            res.sendRedirect(loginUri);
            return false;
        }
        JSONObject userInfoJson = userService.getUserInfo(token, ua);
        AccountUserInfoVo accountUserInfoVo = JsonUtil.toObject(userInfoJson, AccountUserInfoVo.class);
        if (accountUserInfoVo == null) {
            // token ?userInfo
            // res.sendRedirect(loginUri);
            return false;
        }
        String userType = accountUserInfoVo.getUsertype();
        if (StringUtils.equals(UserTypeEnum.NEW_ENTERPRISE.getType(), userType)) {
            // ?
            EnterpriseProgressVo eap = new EnterpriseAuthProgressCommand(token, ua).execute();
            // ??
            boolean isAuthPage = StringUtils.equals(req.getRequestURI(), authUri);
            if (eap.getStatus() && isLoginPage) {
                // ?  
                res.sendRedirect(accountUri);
                return false;
            }
            if (!eap.getStatus() && !isAuthPage && !StringUtils.equals(req.getRequestURI(), licenseUri)) {
                // ??? ?license ?
                res.sendRedirect(authUri);
                return false;
            }
            // 
            return true;
        }
        res.sendRedirect("/usercenter/accountInfo.html");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
            ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
            Object o, Exception e) throws Exception {

    }
}