How to redirect to login page after session expire in GWT RPC call

后端 未结 8 1269
死守一世寂寞
死守一世寂寞 2020-12-13 07:44

I am using GWT and RPC in my app. after session expires when I do a RPC call, because of my login-filter the request redirect to login.jsp, but my problem is client doen\'t

8条回答
  •  囚心锁ツ
    2020-12-13 08:30

    I agree with pathed that you should do redirecting in your AsyncCallbacks. However, you don't need to explicitly use your custom MyAsyncCallback callbacks instead of standard GWT AsyncCallback. This is important for example when you already have a lot of code that uses standard callbacks.

    When you invoke GWT.create(MyService.class) GWT generates proxy for your MyServiceAsync service interface. This proxy is responsible for communicating with the server and invoking your callbacks when it gets data from the server. Proxies are generated using GWT code generators mechanism and by default GWT uses ServiceInterfaceProxyGenerator class to generate these proxies.

    You can extend this default generator (ServiceInterfaceProxyGenerator class) to automatically use your custom MyAsyncCallbacks in all callbacks invocations. We recently did exactly that in a project. Below there is source code which we used.

    Code for MyAsyncCallback, it is identical to the one presented by pathed:

    package my.package.client;
    
    import com.google.gwt.user.client.rpc.AsyncCallback;
    
    public class MyAsyncCallback implements AsyncCallback {
    
        private final AsyncCallback asyncCallback;
    
        public MyAsyncCallback(AsyncCallback asyncCallback) {
            this.asyncCallback = asyncCallback;
        }
    
        @Override
        public void onFailure(Throwable caught) {
            if (caught instanceof SessionTimeoutException) {
                // redirect
                return;
            }
    
            asyncCallback.onFailure(caught);
        }
    
        @Override
        public void onSuccess(T result) {
            asyncCallback.onSuccess(result);
        }
    
    }
    

    Code for GWT code generator (MyRpcRemoteProxyGenerator):

    package my.package.server;
    
    import com.google.gwt.core.ext.typeinfo.JClassType;
    import com.google.gwt.user.rebind.rpc.ProxyCreator;
    import com.google.gwt.user.rebind.rpc.ServiceInterfaceProxyGenerator;
    
    public class MyRpcRemoteProxyGenerator extends ServiceInterfaceProxyGenerator {
    
        @Override
        protected ProxyCreator createProxyCreator(JClassType remoteService) {
            return new MyProxyCreator(remoteService);
        }
    }
    

    And generator helper class (MyProxyCreator):

    package my.package.server;
    
    import java.util.Map;
    
    import com.google.gwt.core.ext.typeinfo.JClassType;
    import com.google.gwt.core.ext.typeinfo.JMethod;
    import com.google.gwt.user.rebind.SourceWriter;
    import com.google.gwt.user.rebind.rpc.ProxyCreator;
    import com.google.gwt.user.rebind.rpc.SerializableTypeOracle;
    
    
    public class MyProxyCreator extends ProxyCreator {
    
        private final String methodStrTemplate = "@Override\n"
                + "protected  com.google.gwt.http.client.Request doInvoke(ResponseReader responseReader, "
                + "String methodName, int invocationCount, String requestData, "
                + "com.google.gwt.user.client.rpc.AsyncCallback callback) {\n"
                + "${method-body}" + "}\n";
    
        public MyProxyCreator(JClassType serviceIntf) {
            super(serviceIntf);
        }
    
        @Override
        protected void generateProxyMethods(SourceWriter w,
                SerializableTypeOracle serializableTypeOracle,
                Map syncMethToAsyncMethMap) {
            // generate standard proxy methods
            super.generateProxyMethods(w, serializableTypeOracle,
                    syncMethToAsyncMethMap);
    
            // generate additional method
            overrideDoInvokeMethod(w);
        }
    
        private void overrideDoInvokeMethod(SourceWriter w) {
            StringBuilder methodBody = new StringBuilder();
            methodBody
                    .append("final com.google.gwt.user.client.rpc.AsyncCallback newAsyncCallback = new my.package.client.MyAsyncCallback(callback);\n");
            methodBody
                    .append("return super.doInvoke(responseReader, methodName, invocationCount, requestData, newAsyncCallback);\n");
    
            String methodStr = methodStrTemplate.replace("${method-body}",
                    methodBody);
            w.print(methodStr);
        }
    
    }
    

    Finally you need to register the new code generator to be used for generating proxies for async services. This is done by adding this to your GWT configuration file (gwt.xml file):

    
        
    
    

    At the beginning it may seem to be a very complicated solution :) but it has its strengths:

    • You can still use standard GWT AsyncCallbacks
    • You can enforce redirecting when session times out globally for your application
    • You can easily tun it on and off (by adding or removing generate-with in your GWT config files)

提交回复
热议问题