Title

shiro springboot整合跨域问题

shiro springboot 整合跨域问题解决方法

package io.github.lyr2000.dissertation.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
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;

/**
 * @author LYR666
 * @description 跨域配置
 * @create 2021-11-05 11:46
 */
@Configuration
public class CorsConfig {
    //参考文章
    //https://urzz.xyz/2019/06/05/spring-shiro-cors-config/
    @Bean
    public FilterRegistrationBean corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        // 允许cookies跨域
        config.setAllowCredentials(true);
        // #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
        config.addAllowedOriginPattern("*");
        // #允许访问的头信息,*表示全部
        config.addAllowedHeader("*");
        // 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
        config.setMaxAge(18000L);
        // 允许提交请求的方法,*表示全部允许
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        source.registerCorsConfiguration("/**", config);

        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        // 设置监听器的优先级
        bean.setOrder(0);

        return bean;
    }

    // @Bean
    // public WebMvcConfigurer corsConfigurer(/*WebMvcConfigurer configurer*/) {
    //
    //     // configurer.addCorsMappings();
    //     return new WebMvcConfigurer() {
    //         @Override
    //         public void addCorsMappings(CorsRegistry registry) {
    //             registry.addMapping("/**")
    //                     .allowedOriginPatterns("*")
    //                     .allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
    //                     .allowedHeaders("*")
    //                     .allowCredentials(true)
    //                     .exposedHeaders(HttpHeaders.SET_COOKIE)
    //                     .maxAge(3600L);
    //         }
    //     };
    //     // CorsRegistry cor = new CorsRegistry();
    //     // cor.addMapping("/**").allowedOrigins("*");
    //     // configurer.addCorsMappings(cor);
    //     // return configurer;
    // }

    // @Bean
    // public org.springframework.web.filter.CorsFilter corsFilter() {
    //     final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    //     CorsConfiguration config = new CorsConfiguration();
    //     config.setAllowCredentials(true);
    //     config.setAllowedOriginPatterns(Collections.singletonList("*"));
    //     config.setAllowedHeaders(Arrays.asList("Origin", "Content-Type", "Accept", "responseType", "Authorization"));
    //     config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH"));
    //     source.registerCorsConfiguration("/**", config);
    //     return new org.springframework.web.filter.CorsFilter(source);
    //     // @Bean
    //     // public CorsRegistration corsRegistry() {
    //     //     return new CorsRegistry().addMapping("/**")
    //     //             .allowCredentials(false)
    //     //             .allowedOrigins("*");
    //     // }
    // }
    // @Bean
    // public CorsRegistration corsRegistry() {
    //     return new CorsRegistry().addMapping("/**")
    //             .allowCredentials(false)
    //             .allowedOrigins("*");
    // }
}


posted @ 2021-12-30 17:53  .geek  阅读(297)  评论(0编辑  收藏  举报