如何解决Spring:@CrossOrigin 不适用于 Spring Data Rest 应用程序中的自定义控制器
我使用 spring 数据 rest 构建了一个 rest api 服务,需要为一个端点添加一个自定义控制器。 CORS 要求所有 SDR 端点都正常,但该自定义控制器失败并显示 403 错误。
这是我所做的:
@CrossOrigin
@Tag(name = "tag-list")
@RepositoryRestController
@RequestMapping("/tagList")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TagListController implements RepresentationModelProcessor<RepositoryLinksResource> {
@PersistenceContext
private final @NonNull EntityManager entityManager;
@Operation(summary = "Retrieve a list of tags used in trainings")
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<EntityModel<TagList>> get() {
@SuppressWarnings("unchecked")
List<String> tags = entityManager.createNativeQuery("SELECT DISTINCT(tag) FROM tag").getResultList();
EntityModel<TagList> model = EntityModel.of(new TagList(tags.stream().collect(Collectors.toSet())));
model.add(linkTo(methodOn(TagListController.class).get()).withSelfRel());
return ResponseEntity.ok(model);
}
@Override
public RepositoryLinksResource process(RepositoryLinksResource model) {
model.add(linkTo(TagListController.class).withRel("taglist"));
return model;
}
@Getter
@Setter
@AllArgsConstructor
public static class TagList {
private Set<String> values;
}
}
这就是我添加的全部内容。看起来 @CrossOrigin
被忽略了。我怎样才能做到这一点?
编辑
我注意到,当我在 Minikube 上本地部署时,与 GKE 上的生产部署相比,我得到的请求日志不同:
在 GKE 上
2021-03-28 06:59:43.897 DEBUG 1 --- [io-8080-exec-10] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/","classpath:/resources
/","classpath:/static/","classpath:/public/","/"]
2021-03-28 06:59:43.897 DEBUG 1 --- [io-8080-exec-10] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2021-03-28 06:59:43.897 DEBUG 1 --- [io-8080-exec-10] w.c.HttpSessionSecurityContextRepository : Did not store anonymous SecurityContext
2021-03-28 06:59:43.897 DEBUG 1 --- [io-8080-exec-10] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2021-03-28 06:59:43.897 DEBUG 1 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet : Completed 403 FORBIDDEN
在 Minikube 上
2021-03-28 07:03:41.650 DEBUG 1 --- [io-8080-exec-10] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to training.edit.catalog.rest.controller.TagListController#get()
2021-03-28 07:03:41.650 DEBUG 1 --- [io-8080-exec-10] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2021-03-28 07:03:41.651 DEBUG 1 --- [io-8080-exec-10] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2021-03-28 07:03:41.651 DEBUG 1 --- [io-8080-exec-10] o.s.web.servlet.DispatcherServlet : Completed 200 OK
请求怎么可能没有映射到 TagListController#get()
?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。