Spring3使用CORS解决跨域请求的配置

Spring3使用CORS解决跨域请求的配置

1.以下是过滤器的代码:

/**
 * CORS过滤器
 * 
 * @author hkb <br>
 */
public class CorsFilter implements Filter {

    /*
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    /*
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
     * javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        httpServletResponse.setHeader("Access-Control-Max-Age", "3600");
        httpServletResponse.setHeader("Access-Control-Allow-Headers",
                "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
        httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");// 允许cookie
        chain.doFilter(request, response);
    }

    /*
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy() {

    }

}

以下是web.xml中的配置

<!-- CORS过滤器start -->
    <filter>
        <filter-name>corsFilter</filter-name>
        <filter-class>org.food.filter.CorsFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>corsFilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>
<!-- CORS过滤器end -->

 

 

转载:https://blog.csdn.net/hkhhkb/article/details/78806569

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Spring Security解决CORS跨域问题,可以按照以下步骤进行配置: 1. 添加CORS配置类:创建一个类,继承自`WebSecurityConfigurerAdapter`,并重写`configure(HttpSecurity http)`方法。 ```java @Configuration public class CorsConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors(); } @Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); configuration.addAllowedOrigin("*"); // 允许所有来源 configuration.addAllowedMethod("*"); // 允许所有请求方法 configuration.addAllowedHeader("*"); // 允许所有请求头 UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); return source; } } ``` 2. 启用CORS配置:在Spring Boot应用的入口类上添加`@EnableWebSecurity`注解。 ```java @EnableWebSecurity public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 通过以上配置Spring Security会自动处理跨域请求,并允许所有来源、所有请求方法和所有请求头。你可以根据需要调整配置,例如指定允许的来源、方法和头部信息。 请注意,如果你的应用程序使用了其他Spring Security配置,你需要将CORS配置类的优先级调整到较低的位置,以确保CORS配置被正确应用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值