2016-02-11 67 views
3

我正在嘗試爲自己的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)

因爲我沒有在文檔中發現的不多,爲了實現這我基於這些例子:

但沒有這個地方足夠多的規定。請幫忙。

回答

1

我在RestyDispatcher類中手動覆蓋send方法,所以我的實現必須手動執行super.send(method, builder)。或者我不應該重寫send方法。

所以RestyDispatcher它應該是這樣的:

public class RestyDispatcher extends DefaultFilterawareDispatcher { 

    public RestyDispatcher() { 
     addFilter(new ForbiddenDispatcherFilter()); 
     addFilter(new BasicAuthHeaderDispatcherFilter()); 
    } 

    @Override 
    public Request send(Method method, RequestBuilder builder) throws RequestException { 
     return super.send(method, builder); 
    } 

} 

public class RestyDispatcher extends DefaultFilterawareDispatcher { 

    public RestyDispatcher() { 
    addFilter(new ForbiddenDispatcherFilter()); 
    addFilter(new BasicAuthHeaderDispatcherFilter()); 
    } 
} 
0

我的解決方案是手動執行filter方法。 我不知道這是不錯的解決方案。我認爲這是一個錯誤,原因filter方法應該由RestyGWT調用,因爲它是在interface DispatcherFilter聲明和DefaultFilterawareDispatcher類實現如下:

package org.fusesource.restygwt.client.dispatcher; 
public class DefaultFilterawareDispatcher implements FilterawareDispatcher { 

    @Override 
    public Request send(Method method, RequestBuilder builder) throws RequestException { 
     for (DispatcherFilter f : dispatcherFilters) { 
      if (!f.filter(method, builder)) { 
       // filter returned false, no continue 
       if (GWT.isClient() && LogConfiguration.loggingIsEnabled()) { 
        Logger.getLogger(DefaultFilterawareDispatcher.class.getName()) 
          .fine(f.getClass() + " told me not to continue filtering for: " 
            + builder.getHTTPMethod() + " " + builder.getUrl()); 
       } 
       return null; 
      } 
     } 
     return builder.send(); 
    } 
} 

這裏是我的代碼實行:

public class ClientModule extends AbstractPresenterModule { 
    @Override 
    protected void configure() { 
     bind(RestyGwtConfig.class).asEagerSingleton(); 
     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(IsAdminGatekeeper.class).in(Singleton.class); 
     bind(UserLoginGatekeeper.class).in(Singleton.class); 
    } 
} 

public class RestyGwtConfig { 
    static { 
     Defaults.setDispatcher(new RestyDispatcher()); 
     Defaults.setServiceRoot(new Resource(GWT.getModuleBaseURL()).resolve("../../cms/services").getUri()); 
    } 
} 

public class RestyDispatcher extends DefaultFilterawareDispatcher { 

    public RestyDispatcher() { 
     addFilter(new ForbiddenDispatcherFilter()); 
     addFilter(new BasicAuthHeaderDispatcherFilter()); 
    } 
    @Override 
    public Request send(Method method, RequestBuilder builder) throws RequestException { 
     for (DispatcherFilter e : this.dispatcherFilters) { 
      e.filter(method, builder); 
     } 
    return builder.send(); 
    } 
} 

如果我錯了,請糾正我。

相關問題