GWTP Rest dispatch:用自己的实现替换 DefaultRequestBuilderFactory

GWTP Rest dispatch: replace DefaultRequestBuilderFactory with own implementation

我的 GWT 前端在端口 8888 上。REST 后端在 8080 上。默认情况下 cookie 不起作用。如果我为请求调用 RequestBuilder.setIncludeCredentials() ,似乎应该传递 cookie。 在这种情况下,我需要用我自己的实现替换 DefaultRequestBuilderFactory,因为我需要将 cookie 发送到服务器。

SecuredRequestBuilderFactory.java:

public class SecuredRequestBuilderFactory extends DefaultRequestBuilderFactory {
    public SecuredRequestBuilderFactory(HttpRequestBuilderFactory httpRequestBuilderFactory, BodyFactory bodyFactory, HeaderFactory headerFactory, UriFactory uriFactory, @RequestTimeout Integer requestTimeoutMs) {
        super(httpRequestBuilderFactory, bodyFactory, headerFactory, uriFactory, requestTimeoutMs);
    }

    @Override
    public <A extends RestAction<?>> RequestBuilder build(A action, String securityToken) throws ActionException {
        RequestBuilder builder = super.build(action, securityToken);
        builder.setIncludeCredentials(true);
        return builder;
    }
}

我已经添加到我的 gwt.xml:

<replace-with class="com.example.rest.SecuredRequestBuilderFactory">
    <any>
        <when-type-is class="com.gwtplatform.dispatch.rest.client.core.RequestBuilderFactory"/>
        <when-type-is class="com.gwtplatform.dispatch.rest.client.core.DefaultRequestBuilderFactory"/>
    </any>
</replace-with>

我从调试器(Chrome 使用 IDEA 插件)发现 class SecuredRequestBuilderFactory 没有映射,并且不包含凭据。

那么,有什么让它发挥作用的想法吗?

我找到了通过 REST 请求传递所有 cookie 的解决方案:

SecuredRequestBuilderFactory.java:

public class SecuredRequestBuilderFactory extends DefaultRequestBuilderFactory {

    @Inject
    public SecuredRequestBuilderFactory(HttpRequestBuilderFactory httpRequestBuilderFactory, BodyFactory bodyFactory, HeaderFactory headerFactory, UriFactory uriFactory, @RequestTimeout Integer requestTimeoutMs) {
        super(httpRequestBuilderFactory, bodyFactory, headerFactory, uriFactory, requestTimeoutMs);
    }

    @Override
    public <A extends RestAction<?>> RequestBuilder build(A action, String securityToken) throws ActionException {
        RequestBuilder builder = super.build(action, securityToken);
        builder.setIncludeCredentials(true);
        return builder;
    }
}

MyRestDispatchModule.java:

public class MyRestDispatchModule extends AbstractGinModule {

    static class MyCoreModuleBuilder extends CoreModuleBuilder {
        MyCoreModuleBuilder(BaseRestDispatchModuleBuilder<?> baseBuilder) {
            super(baseBuilder);
            requestBuilderFactory(SecuredRequestBuilderFactory.class);
        }
    }

    static class MyBuilder extends RestDispatchAsyncModule.Builder {
        @Override
        public CoreModuleBuilder core() {
            return new MyCoreModuleBuilder(this);
        }
    }

    @Override
    protected void configure() {
        install(new MyBuilder().build());
    }
}