SpringSecurity遇到的坑

1.想要测试接口,必须注释或者直接不写

//.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)

什么前后端分离没有session,放屁,误导我这么久,
但是springsecurity就是通过session来判断是否已经登陆了
加上死活通行不了,登录成功了也无效

2.这个必须加,否则测试不了

.and().csrf().disable();

配置类示范

   /**
     * 配置跨域
     *
     * @return CorsConfigurationSource
     * @author sheep669
     * @created at 2022/7/22 14:01
     */
    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        // 新建一个跨域配置
        CorsConfiguration configuration = new CorsConfiguration();
        //允许请求携带验证信息
        //configuration.setAllowCredentials(true);
        // 允许跨域访问的域名 所有
        configuration.addAllowedOrigin("*");
        // 允许访问的方法名 所有
        configuration.addAllowedMethod("*");
        // 允许服务端访问客户端的请求头 所有
        configuration.addAllowedHeader("*");
        // 跨域允许时间 3秒
        configuration.setMaxAge(Duration.ofMillis(3000));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 允许跨域访问的请求 所有请求 /**
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

一个使用

configuration.addAllowedOrigin("*");
configuration.addAllowedMethod("*");

多个使用

configuration.setAllowedOrigins(Arrays.asList("http://localhost:8081", "http://localhost:8082"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "DELETE", "PUT"));

以下不能不能一起使用 有他(configuration.setAllowCredentials(true))不能有*

//允许请求携带验证信息
configuration.setAllowCredentials(true);
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.addAllowedOrigin("*");
全部评论
多长点心吧
点赞
送花
回复
分享
发布于 2022-07-24 17:15

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务