Gb1400AuthInterceptor.java 1.55 KB
package com.viontech.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * @author vion
 **/
public class Gb1400AuthInterceptor implements HandlerInterceptor {

    private Logger log = LoggerFactory.getLogger(Gb1400AuthInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //拦截国标1400相关接口
        String servletPath = request.getServletPath();
        if(!servletPath.contains("/VIID/System/Register")){
            return true;
        }
        String authorization = request.getHeader("Authorization");
        if(StringUtils.isEmpty(authorization)){
            response.addHeader("WWW-Authenticate","Digest realm=\"viid\",qop=\"auth\",nonce=\"123456\",opaque=\"654321\"");
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}