/*
 * @Author: Kane
 * @Date: 2022-12-15 10:44:20
 * @LastEditors: Kane
 * @LastEditTime: 2023-01-29 10:40:39
 * @FilePath: \AdminSys\src\main\java\com\cpic\xim\web\filters\cros\CrosFilter.java
 * @Description: 过滤器，用于对CROS访问进行响应。允许任何来源的访问。
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.web.filters.cros;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CrosFilter implements Filter
{
    @Override
    public void doFilter( ServletRequest req, ServletResponse resp, FilterChain chain )
            throws ServletException, IOException
    {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String originHeader = request.getHeader( "Origin" );

        response.setHeader( "Access-Control-Allow-Origin", originHeader );
        response.setHeader( "Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE" );
        response.setHeader( "Access-Control-Max-Age", "0" );
        response.setHeader( "Access-Control-Allow-Headers",
                "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token" );
        response.setHeader( "Access-Control-Allow-Credentials", "true" );
        response.setHeader( "XDomainRequestAllowed", "1" );
        response.setHeader( "XDomainRequestAllowed", "1" );

        chain.doFilter( request, response );
    }
}
