zoukankan      html  css  js  c++  java
  • springboot解决跨域问题

    在启动类里加入corsFilter

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.cors.CorsConfiguration;
    import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
    import org.springframework.web.filter.CorsFilter;
    
    
    @SpringBootApplication
    public class NuclearApplication {
        /**
         * 跨域过滤器
         *
         * @return
         */
        @Bean
        public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", buildConfig());
            return new CorsFilter(source);
        }
    
        private CorsConfiguration buildConfig() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            corsConfiguration.addAllowedOrigin("*");
            corsConfiguration.addAllowedHeader("*");
            corsConfiguration.addAllowedMethod("*");
            return corsConfiguration;
        }
    
    
        public static void main(String[] args) {
            SpringApplication.run(NuclearApplication.class, args);
        }
    
    }
    

    特殊情况可以使用第二种来做,加入一个filter

    import org.springframework.stereotype.Component;
    
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @Component
    public class AccessFilter implements Filter {
    	@Override
    	public void init(FilterConfig filterConfig) throws ServletException {
    	}
    		
    	@Override
    	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    		HttpServletResponse response = (HttpServletResponse) servletResponse;
    		HttpServletRequest request = (HttpServletRequest) servletRequest;
    		response.setContentType("application/json; charset=utf-8");
    		response.setCharacterEncoding("UTF-8");
    		response.setHeader("Access-Control-Max-Age", "3600");
    		response.setHeader("Access-Control-Allow-Methods", "POST, GET,PUT, OPTIONS, DELETE");
    		response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
    		response.setHeader("Access-Control-Allow-Credentials", "true");
    		response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, token");
    		filterChain.doFilter(servletRequest, servletResponse);
    	}
    	@Override
    	public void destroy() {
    	}
    }
  • 相关阅读:
    【Storm篇】--Storm从初始到分布式搭建
    【Storm篇】--Storm 容错机制
    git命令log与reflog的比较
    长时间不操作Navicat或Putty会断线?
    git使用ssh密钥
    公钥能用公钥解吗?
    什么是公钥,,什么是密钥啊 ?有什么区别吗?
    CSS滑动门
    Html设置文本换行与不按行操作
    设置鼠标光标与页面favicon
  • 原文地址:https://www.cnblogs.com/ttzzyy/p/9056498.html
Copyright © 2011-2022 走看看