我的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>
我从调试器(带有IDEA插件的Chrome)中发现,SecuredRequestBuilderFactory类没有映射,并且未包含凭据。
那么,任何想法如何使其发挥作用?
答案 0 :(得分:0)
<强> 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());
}
}