小编Mic*_*ter的帖子

Spring HATEOAS 递归表示模型处理器?

我有一个关于 Spring HATEOAS 的表示模型处理器的问题。我们正在尝试在将模型序列化给客户端之前对其进行处理。我们的用例是在运行时丰富对象imageUrl字段UserModel,因为我们必须根据来自配置 bean 的值构建 URL(AWS S3 存储桶 URL 因 DEV/PROD 设置而异)。

@Data
public class UserModel {
    // ...
    private String imageUrl;
}
Run Code Online (Sandbox Code Playgroud)

因此,我们创建了一个UserProcessor来实现这个:

public class UserProcessor implements RepresentationModelProcessor<EntityModel<UserModel>> {

    private final ConfigAccessor configAccessor;

    public UserProcessor(ConfigAccessor configAccessor) {
        this.configAccessor = configAccessor;
    }

    @Override
    public EntityModel<UserModel> process(EntityModel<UserModel> model) {
        if (model.getContent() != null)
            // do the enrichment and set "imageUrl" field
        }
        return model;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我们有这样的控制器方法,这非常有效:

@ResponseBody
@GetMapping("/me")
public EntityModel<UserModel> getCurrentUser(@AuthenticationPrincipal Principal principal) …
Run Code Online (Sandbox Code Playgroud)

spring spring-hateoas spring-boot

6
推荐指数
1
解决办法
440
查看次数

由于 Chrome 中缺少预检请求而阻止 HTTP 重定向

我们目前正在构建一个由 RESTful API 支持的 Angular 应用程序。该应用程序尝试使用以下请求 URL 获取用户的信息:http://example.com:1337/api/users/1。该资源使用 HTTP 301(永久移动)进行响应,并设置资源已移动到的位置标头,例如Location=http://another-hostname.com:8088/new/users/1。Angular 的 HTTP 客户端或者更确切地说是浏览器正在自动处理此响应并重定向到这个新位置。

遗憾的是,在 Chrome 中我们收到以下错误消息:

XMLHttpRequest cannot load http://example.com:1337/api/users/1. Redirect from 'http://example.com:1337/api/users/1' to 'http://another-hostname.com:8088/new/users/1' has been blocked by CORS policy: Request requires preflight, which is disallowed to follow cross-origin redirect.
Run Code Online (Sandbox Code Playgroud)

Chrome 在调用新资源的 GET 之前会跳过预检请求 (CORS)。我们用 Firefox 对此进行了测试,效果非常好。Firefox 发出新的预检请求,之后的 GET 请求将被成功处理。

所以,我想知道这里正确的行为是什么。有人有过 30 倍回复的类似经历吗?

是否可以禁用自动浏览器(或 Angular HTTP 客户端)重定向处理?这样我们就可以用全新的this.http.get(...).

感谢您的任何建议,

迈克尔

redirect google-chrome angular

4
推荐指数
1
解决办法
5967
查看次数