我在spring boot 3.1spring security 6.1之间完成了后端工作

我已经使用spring security和各种控制器配置了登录.当我从Postman调用我的后端时,我没有任何问题,但当我通过ajax时,我在浏览器控制台中收到CORS错误.我try 将配置设置为CORS,但它不适用于我,默认设置spring也不起作用.有什么主意吗?我把我的班级图片放在网上,以防它们对我有帮助.

类CorsConfig

package com.pms.pmsBack.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }
}

类SpringSecurityConfig

@Bean
    SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
         http
                 .csrf(csrf -> csrf.disable())
                 .sessionManagement(management -> management.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                 .addFilter(new JwtAuthenticationFilter(authenticationConfiguration.getAuthenticationManager()))
                 .addFilter(new JwtValidationFilter(authenticationConfiguration.getAuthenticationManager()))
                 .authorizeHttpRequests((authorizeHttpRequests) -> authorizeHttpRequests.requestMatchers(HttpMethod.POST, "/login")
                         .permitAll()
                         .anyRequest()
                         .authenticated()
                 );

         return http.build();
    }

The CORS error: enter image description here

enter image description here If this is not enough, I will be attentive to the answers to add more information. Thank you so much

Axios示例:

const respuesta = await fetch('http://localhost:8080/login', data)
                .then(function(response) {
                  if (response.ok) {
                    return response.json();
                  }
                  throw new Error('Error in Request');
                })
                .then(function(data) {
                  console.log(data);
                })
                .catch(function(error) {
                  console.log(error);
                });

推荐答案

CorsConfig.java分中,try 添加@EnableWebMvc个注释. 该注释实际上将使其处理所有注册表操作.

Java相关问答推荐

收听RDX中用户数据的变化

如何为具有多对多关系的实体的给定SQL查询构建JPA规范?

如何配置ActiveMQ Artemis以使用AMQP 1.0和其他协议与Java

Junit with Mockito for java

嵌入式ActiveMQ Artemis Web控制台加载错误

Spring Boot Maven包

获取字符串中带空格的数字和Java中的字符

为什么Java Annotation接口覆盖对象类中的方法

try 使用类来包含JSON响应

当b是一个字节并且在Java中值为-1时,为什么b>;>;>;1总是等于-1?

Spring Framework6.1中引入的新RestClient是否有适合于测试的变体,就像RestTemplate和TestRestTemplate一样?

嘲笑黄瓜中的对象

为什么mvn编译生命周期阶段不只是编译已更改的java文件?

Java类型推断:为什么要编译它?

如何利用OpenTelemeter将初始值(零)输出到普罗米修斯

如何使用Hibernate v6.2构建NamingStrategy,以表名作为所有列的前缀?

由于版本不匹配,从Java 8迁移到Java 17和Spring 6 JUnit4失败

如何在单元测试中获得我的装饰Mapstruct映射器的实例?

Springboot应用程序无法识别任何@RestController或@Service,我认为@Repository也无法识别

如何从指定某些字段的父对象创建子对象