JAVA 服务器端配置跨域请求配置类

方法一:添加cors全局配置

com/wwk/seckill/config/CorsConfig.java

package com.wwk.seckill.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 全局跨域配置
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        //创建跨域配置,增加CORS配置信息。
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //跨域请求默认不包含cookie,设置为true,可以包含cookie。
        corsConfiguration.setAllowCredentials(true);
        //支持哪些来源的请求跨越,支持
        corsConfiguration.addAllowedOrigin("*");
        //支持哪些头信息
        corsConfiguration.addAllowedHeader("*");
        //支持哪些方法跨越
        corsConfiguration.addAllowedMethod("*");
        //增加映射路径
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // /**表示所有请求都支持跨域
        source.registerCorsConfiguration("/**", corsConfiguration);

        //返回新的CorsFilter
        return new CorsFilter(source);
    }

}


方法二:添加cors配置类

WebMvcConfig

package com.wwk.wwkliving.commodity.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * WebMvcConfig:配置类继承WebMvcConfigurationSupport
 * 重写addCorsMappings 配置实现跨域
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST","GET","PUT","OPTIONS","DELETE")
                .maxAge(3600)
                .allowCredentials(true);
    }
}

具体来说,它允许所有来源(allowedOrigins("*"))的POST、GET、PUT、OPTIONS和DELETE方法(allowedMethods("POST","GET","PUT","OPTIONS","DELETE")),并允许凭据(allowCredentials(true))的访问,设置了最大缓存时间为3600秒(maxAge(3600))。

方法三:使用Filter方法实现跨域

  1. Filter类配置跨域拦截
package com.wwk.wwkliving.commodity.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.dsig.spec.XPathType;
import java.io.IOException;

//对映射路径配置,所有
@WebFilter(urlPatterns = "*")
public class CorsFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Credentials","true");
        response.setHeader("Access-Control-Allow-Methods","*");
        response.setHeader("Access-Control-Allow-Headers","Content-Type,Authorization");
        response.setHeader("Access-Control-Expose-Headers","*");
        filterChain.doFilter(request,response);




    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}

  1. 在启动类添加@ServletComponentScan注解,用来扫描Filter过滤器
package com.wwk.wwkliving.commodity;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
/**
 * @ServletComponentScan 是 Spring 框架中的一个注解,
 * 它可以自动扫描 @WebServlet、@WebFilter 和 @WebListener 注解,
 * 并将它们注册为 Servlet、Filter 和 Listener。
 */
@ServletComponentScan
public class CommodityApplication {
    public static void main(String[] args) {
        SpringApplication.run(CommodityApplication.class,args);
    }
}

``

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值