shiro跨域CORS问题处理

前言

跨域问题的基本处理查看上一篇文章:https://blog.csdn.net/Mint6/article/details/104468530,这个可以解决大部分问题。

跨域问题后端有四种方法可以解决:https://blog.csdn.net/Mint6/article/details/104726325

如果是使用的shiro以上步骤都试过了,还有如下问题,请看本文章的解决办法。

问题

把主要提示拷贝出来

A cookie associated with a cross-site resource at http://127.0.0.1/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.

 跨域问题导致的,cookie带不过去,重定向失败。1.明确这个情况是跨域导致的问题。

分析经过

(1)跨域:其实已经使用这个文章(https://mp.csdn.net/console/editor/html/104726325)的方式2:重写WebMvcConfigurer解决了,在进行前后端联调的时候,接口已经可以调通了。

@Configuration
public class WebMvcConfg implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                //当**Credentials为true时,**Origin不能为星号,需为具体的ip地址【如果接口不带cookie,ip无需设成具体ip】
                .allowedOrigins("http://localhost:9527", "http://127.0.0.1:9527", "http://127.0.0.1:8082", "http://127.0.0.1:8083")
                //是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("*")
                //跨域允许时间
                .maxAge(3600);
    }
}

(2)但是在调节其中一个ajax请求的时候,后端权限使用的springboot+shiro实现的,发现原来的跨域代码重写WebMvcConfigurer不管用了。

(3)原因:原来的逻辑是用户请求->WebMvcConfigurer设置header->controller,

Spring Boot整合Shiro之后用户请求->shiro的监听器->shiro逻辑->WebMvcConfigurer设置header->controller,

默认所有请求会先经过shiro的监听器,在执行shiro代码逻辑的时候跨域出错了,所以上面的全局方法已经不管用了

解决办法

在shiro代码之前设置header,解决跨域问题就好了。所以按如下增加配置,并且可以重写WebMvcConfigure的方式去掉

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;

/**
 * @Auther: Administrator
 * @Date:  
 * @Description:
 */
@Configuration
public class CorsConfig {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setMaxAge(3600L);
        corsConfiguration.setAllowCredentials(true);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
}

最后:这个是针对shiro特殊情况的处理,如果是有跨域问题还没定位问题请先看跨域的基本处理办法:https://blog.csdn.net/Mint6/article/details/104468530

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值