我正在嘗試爲自己的gwt編寫我的自定義調度程序。RestyGWT-自定義調度程序不會調用已註冊的過濾器
我的調度員RestyDispatcher
將包含兩個過濾器:
BasicAuthHeaderDispatcherFilter
(這將增加每個請求安全令牌),ForbiddenDispatcherFilter
(它將包含回調ForbiddenDispatcherCallback
) - 他的工作是重定向到登錄頁面如果用戶沒有登錄。
我有問題,我的過濾器是由gwt/restygwt註冊的,不幸的是他們不工作。
這裏是代碼:
杜松子酒客戶端模塊
public class ClientModule extends AbstractPresenterModule {
@Override
protected void configure() {
install(new DefaultModule.Builder().defaultPlace(Routing.PAGE.url).errorPlace(Routing.PAGE.url).unauthorizedPlace(Routing.LOGIN.url).tokenFormatter(RouteTokenFormatter.class).build());
install(new AppModule());
bind(CurrentUser.class).in(Singleton.class);
bind(UserLoginGatekeeper.class).in(Singleton.class);
// Load and inject CSS resources
bind(ResourceLoader.class).asEagerSingleton();
bind(RestyGwtConfig.class).asEagerSingleton();
}
}
RestyGwtConfig
public class RestyGwtConfig {
public RestyGwtConfig() {
final Dispatcher dispatcher = new RestyDispatcher();
GWT.log("--> RestyGwtConfig -> setDispatcher");
Defaults.setDispatcher(dispatcher);
UserCredentials.INSTANCE.setUserName("ronan");
UserCredentials.INSTANCE.setPassword("password");
}
static {
GWT.log("--> RestyGwtConfig -> setServiceRoot");
Defaults.setServiceRoot(new Resource(GWT.getModuleBaseURL()).resolve("../../cms/services").getUri());
}
}
RestyDispatcher
public class RestyDispatcher extends DefaultFilterawareDispatcher {
public RestyDispatcher() {
addFilter(new ForbiddenDispatcherFilter());
GWT.log("--> RestyDispatcher -> RestyDispatcher -> addFilter -> ForbiddenDispatcherFilter");
addFilter(new BasicAuthHeaderDispatcherFilter());
GWT.log("--> RestyDispatcher -> RestyDispatcher -> addFilter -> BasicAuthHeaderDispatcherFilter");
}
@Override
public Request send(Method method, RequestBuilder builder) throws RequestException {
GWT.log("SimpleDispatcher.Request.send.start");
Request send = builder.send();
GWT.log("SimpleDispatcher.Request.send.done");
return send;
}
}
ForbiddenDispatcherFilter
public class ForbiddenDispatcherFilter implements DispatcherFilter {
@Override
public boolean filter(Method method, RequestBuilder builder) {
GWT.log("--> ForbiddenDispatcherFilter -> filter -> setCallback");
builder.setCallback(new ForbiddenDispatcherCallback(method));
return true;
}
}
ForbiddenDispatcherCallback
public class ForbiddenDispatcherCallback implements RequestCallback {
protected RequestCallback requestCallback;
public ForbiddenDispatcherCallback(Method method) {
GWT.log("_________> ForbiddenDispatcherCallback " + method.toString());
this.requestCallback = method.builder.getCallback();
}
@Override
public void onResponseReceived(Request request, Response response) {
GWT.log("_________> ForbiddenDispatcherCallback -> onResponseReceived");
GWT.log(response.getStatusText() + response.getStatusCode());
if (response.getStatusCode() == Response.SC_FORBIDDEN || response.getStatusCode() == Response.SC_UNAUTHORIZED) {
// make a hard redirect to login page
Window.Location.assign("#/login");
} else {
requestCallback.onResponseReceived(request, response);
}
}
@Override
public void onError(Request request, Throwable exception) {
GWT.log("_________> ForbiddenDispatcherCallback -> onError");
requestCallback.onError(request, exception);
}
}
BasicAuthHeaderDispatcherFilter
final class BasicAuthHeaderDispatcherFilter implements DispatcherFilter {
public static final String AUTHORIZATION_HEADER = "Authorization";
@Override
public boolean filter(Method method, RequestBuilder builder) {
String basicAuthHeaderValue = createBasicAuthHeader(UserCredentials.INSTANCE.getUserName(), UserCredentials.INSTANCE.getPassword());
builder.setHeader(AUTHORIZATION_HEADER, basicAuthHeaderValue);
return true;
}
private String createBasicAuthHeader(String userName, String password) {
String credentials = userName + ":" + password;
String encodedCredentials = credentials;
return AUTHORIZATION_HEADER + ": Basic " + encodedCredentials;
}
}
當我運行應用程序,我在Web瀏覽器控制檯中看到此日誌:
--> RestyGwtConfig -> setServiceRoot
SuperDevModeLogger.java:71--> RestyDispatcher -> RestyDispatcher -> addFilter -> ForbiddenDispatcherFilter
SuperDevModeLogger.java:71--> RestyDispatcher -> RestyDispatcher -> addFilter -> BasicAuthHeaderDispatcherFilter
SuperDevModeLogger.java:71--> RestyGwtConfig -> setDispatcher
所以,我認爲我的調度員,蒙山做2個過濾器是否正確註冊。不幸的是我沒有看到從ForbiddenDispatcherFilter
的日誌說,它設置我的回調ForbiddenDispatcherCallback
。我也沒有看到任何提到的回調日誌。
當我執行任何resty服務,我得到的日誌條目:
SimpleDispatcher.Request.send.start SuperDevModeLogger.java:71
SimpleDispatcher.Request.send.done XMLHttpRequest.java:305
POST http://localhost:8080/cms/services/authenticated/testService/setInfo 401 (Unauthorized)
因爲我沒有在文檔中發現的不多,爲了實現這我基於這些例子:
但沒有這個地方足夠多的規定。請幫忙。