springboot开启跨域security也开启跨域配置

springboot开启跨域spring security也开启跨域配置

浏览器 同源策略,导致跨域失败,添加了security框架后,因为安全框架有一系列的过滤器,即使springboot把跨域打开,security的过滤器也可以能拦截。

所以,如果要在服务器端设置,要改两处

 

方法一:

先开放springboot的跨域

 

@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
    /**
     * 跨域配置
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域的请求域名
                .allowedOriginPatterns("*")
                //是否允许cookie
                .allowCredentials(true)
                //允许请求的方式
                .allowedMethods("GET","POST","DELETE","PUT")
                //设置请求跨域的header属性
                .allowedHeaders("*")
                //允许跨域时间
                .maxAge(3600);
    }
}

 

再开启security的跨域

 

httpSecurity
                // CSRF禁用,因为不使用session
                .csrf().disable()
                // 禁用HTTP响应标头
                .headers().cacheControl().disable().and()
                // 认证失败处理类
                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                // 基于token,所以不需要session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                // 过滤请求
                .authorizeRequests()
                // 对于登录login 注册register 验证码captchaImage 允许匿名访问
                .antMatchers("/login", "/register", "/captchaImage").permitAll()
                // 静态资源,可匿名访问
                .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
                .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated()
                .and()
                .headers().frameOptions().disable();
        // 添加Logout filter
        httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
        // 添加JWT filter
        httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        //允许跨域
        httpSecurity.csrf();

 

 

 

 

 方法二: (待验证)

这种方式好像没有给springboot的配置跨域

@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
    /**
     * 跨域配置
     */
    @Bean
    public CorsFilter corsFilter()
    {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        // 设置访问源地址
        config.addAllowedOriginPattern("*");
        // 设置访问源请求头
        config.addAllowedHeader("*");
        // 设置访问源请求方法
        config.addAllowedMethod("*");
        // 有效期 1800秒
        config.setMaxAge(1800L);
        // 添加映射路径,拦截一切请求
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        // 返回新的CorsFilter
        return new CorsFilter(source);
    }


}

spring security配置

    /**
     * 跨域过滤器
     */
    @Autowired
    private CorsFilter corsFilter;

    // 添加CORS filter
    httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
    httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);

 

 

 

 

 

 

转 :https://www.bilibili.com/video/BV1mm4y1X7Hc

 

posted @ 2023-05-19 17:39  与f  阅读(541)  评论(0编辑  收藏  举报