브라우저에서 내 API를 호출 할 때마다 http : // localhost : 8090 / posts? user-id = 1 오류가 발생하지 않도록 스프링 부트 데이터 나머지 API에서 전역 CORS를 활성화해야합니다 . 요청 된 리소스에 'Access-Control-Allow-Origin'헤더가 없습니다. 따라서 출처 ' http : // localhost '는 액세스가 허용되지 않습니다. '
브라우저에 URL을 입력하고 해당 리소스에 대한 올바른 응답을받을 수 있지만 웹 페이지의 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'
}
그래서 내가 찾은 의견에 대한 토론 후 addCorsMappings 메소드 에서 이것을 시도 할 수 있습니다.
registry.addMapping("/**").allowedOrigins("http://localhost:8090")
.allowedMethods("PUT", "DELETE", "GET", "POST");
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다