package com.jiayue.ssi.filter;
/**
 * @author xsl
 * @since 2023/05/17
 */

import com.jiayue.ssi.servlet.XssEscapeHttpServletRequestWrapper;
import com.jiayue.ssi.util.ResponseInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//拦截请求
@RequiredArgsConstructor
@Order(7)
@Slf4j
public class XssEscapeFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) {
        try {
            //先进行转义在把请求返回
            XssEscapeHttpServletRequestWrapper wrapper = new XssEscapeHttpServletRequestWrapper(request);
            filterChain.doFilter(wrapper, response);
        } catch (Exception e) {
            ResponseInfo.doResponse(response, "访问失败,联系管理员！", 401);
            return;
        }
    }
}

