简单跨域
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CrosFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse response=(HttpServletResponse) servletResponse;
response.setHeader("Access-Control-Allow-Origin","*");
response.setHeader("Access-Control-Allow-Methods","GET,POST,PUT,DELETE");
response.setHeader("Access-Control-Allow-Headers","Content-Type,usertoken");
response.setHeader("Access-Control-Allow-Credentials","true");
filterChain.doFilter(servletRequest,response);
}
}
import com.fasterxml.jackson.core.filter.TokenFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean createFRB(){
FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
filterRegistrationBean.setFilter(new CrosFilter());
ArrayList<String> list=new ArrayList<>();
list.add("/*");
filterRegistrationBean.setUrlPatterns(list);
return filterRegistrationBean;
}
}