当前位置: 首页 > 知识库问答 >
问题:

不能在Spring启动数据Rest中启用CORS

申屠英韶
2023-03-14

我需要在spring boot data rest api中启用全局CORS,以防止在从浏览器调用api时出现以下错误:http://localhost:8090/posts?user-id=1。请求的资源上没有“访问-控制-允许-来源”标头。因此,不允许访问源“http://localhost”。“

我可以在a浏览器中键入url并接收该资源的正确get响应,但我不能从网页中的ajax调用中发出相同的调用。

@SpringBootApplication
@EnableAutoConfiguration
@EnableJpaRepositories
@EnableWebMvc
public class Application  {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);

}

@Bean
public WebMvcConfigurer corsConfigurer() {

    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**");
        }
    };
}

@Bean
public CommandLineRunner demo(UserRepository userRepository, PostRepository postRepository,
                              CommentRepository commentRepository, EventRepository eventRepository, VenueRepository venueRepository) {
    return (args) -> {

        User user = new User("fsdsdfsd","sdsdsds","121212");
        userRepository.save(user);

        Venue venue = new Venue("dsaesrdfddgd","aerttyhyyty","yyyyyyyyyyyyyy");
        venueRepository.save(venue);

        Event event = new Event("dsaesrdfddgd","aerttyhyyty","yyyyyyyyyyyyyy",venue,user);
        eventRepository.save(event);

        Post post = new Post("some posts are funny. Some are not.",user, event);
        postRepository.save(post);

        Comment comment = new Comment("commmentntnrnejfnerfdgdfgdfdt", user, post);
        commentRepository.save(comment);
    };
}
@RepositoryRestResource
public interface PostRepository extends PagingAndSortingRepository<Post, Long> {


Page<Post> readBydeletedIsFalseOrderByCreated(Pageable pageRequest);

@CrossOrigin
Post readByIdAndDeletedIsFalse(Long postId);
buildscript {
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE")
}
}

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'project'
    version = '0.1.0'
}

war {
    baseName = 'project'
    version = '0.1.0'
}


repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-rest")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("com.h2database:h2")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

共有1个答案

宋弘壮
2023-03-14

因此,在评论中讨论了我发现的内容后,您可以在addCorsMappings方法中尝试此方法

registry.addMapping("/**").allowedOrigins("http://localhost:8090")
                          .allowedMethods("PUT", "DELETE", "GET", "POST");
 类似资料:
  • 我想做一些有趣的事情,我希望能够动态地构建SQL查询过滤器使用Spring Boot 1.5.9和Spring Data Rest,而不需要编写控制器。我觉得我可能走在正确的道路上,但我有点卡住了。 这个想法是通过使用HandlerInterceptorAdapter截取HTTP请求GET方法,并将请求查询参数存储到PagingAndSortingRepository可以使用的对象中。计划是重写S

  • 我正在学习在springboot中使用solr,我在一本书中尝试了该程序,spring-boot-starter-parent版本是2.1.2。在书中发布: 而我的sping-boot-starter-父版本是2.6.4: 我在pom中添加了solr依赖项。xml格式如下: 但是pom.xml显示错误:版本不能为空,所以我去https://mvnrepository.com/,发现最新的sping

  • 我正在尝试按照一个简单的示例应用程序教程来创建一个Spring启动应用程序并将其连接到mysql数据库。使用智能诊断。https://www.youtube.com/watch?v=YVl6M5ztOu8 项目结构: 当我尝试运行此示例应用程序时,我收到以下错误消息: **运行该程序的步骤: 在称为“restfulapi”的 mysql 工作台中创建连接 参数是本地主机端口 3306 的 root

  • 有人知道如何将Spring Boot指标与datadog集成吗? Datadog是一种云级别的IT监控服务。 它允许用户使用大量图表和图形轻松地可视化他们的数据。 我有一个Spring启动应用程序,它使用dropwizard指标来填充有关我用注释的所有方法的大量信息。 另一方面,我正在heroku中部署我的应用程序,因此我无法安装Datadog代理。 我想知道是否有一种方法可以自动将spring

  • 我在我的应用程序中使用spring data rest。 我收到以下错误,当我在存储库中添加此方法时,应用程序无法启动:- 方法:- 错误:- 如何解决这个问题? 更新时间:- 所有依赖项的列表:-

  • 我在intellij上启动spring boot应用程序时遇到问题,它失败了,并显示以下消息:与目标VM断开连接,地址:'127.0.0.1:49784',传输:'socket' 过程结束,退出代码为255。以前有人遇到过这种情况吗?