How do I add a type to GWT's Serialization Policy whitelist?

后端 未结 9 587
离开以前
离开以前 2020-11-30 01:30

GWT\'s serializer has limited java.io.Serializable support, but for security reasons there is a whitelist of types it supports. The documentation I\'ve found,

相关标签:
9条回答
  • 2020-11-30 01:36

    To anyone who will have the same question and doesn't find previous answers satisfactory...

    I'm using GWT with GWTController, since I'm using Spring, which I modified as described in this message. The message explains how to modify GrailsRemoteServiceServlet, but GWTController calls RPC.decodeRequest() and RPC.encodeResponseForSuccess() in the same way.

    This is the final version of GWTController I'm using:

    /**
     * Used to instantiate GWT server in Spring context.
     *
     * Original version from <a href="http://docs.google.com/Doc?docid=dw2zgx2_25492p5qxfq&hl=en">this tutorial</a>.
     * 
     * ...fixed to work as explained <a href="http://blog.js-development.com/2009/09/gwt-meets-spring.html">in this tutorial</a>.
     * 
     * ...and then fixed to use StandardSerializationPolicy as explained in
     * <a href="http://markmail.org/message/k5j2vni6yzcokjsw">this message</a> to allow
     * using Serializable instead of IsSerializable in model.
     */
    public class GWTController extends RemoteServiceServlet implements Controller, ServletContextAware {
    
     // Instance fields
    
     private RemoteService remoteService;
    
     private Class<? extends RemoteService> remoteServiceClass;
    
     private ServletContext servletContext;
    
     // Public methods
    
     /**
      * Call GWT's RemoteService doPost() method and return null.
      * 
      * @param request
      *            The current HTTP request
      * @param response
      *            The current HTTP response
      * @return A ModelAndView to render, or null if handled directly
      * @throws Exception
      *             In case of errors
      */
     public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
      doPost(request, response);
      return null; // response handled by GWT RPC over XmlHttpRequest
     }
    
     /**
      * Process the RPC request encoded into the payload string and return a string that encodes either the method return
      * or an exception thrown by it.
      * 
      * @param payload
      *            The RPC payload
      */
     public String processCall(String payload) throws SerializationException {
      try {
       RPCRequest rpcRequest = RPC.decodeRequest(payload, this.remoteServiceClass, this);
    
       // delegate work to the spring injected service
       return RPC.invokeAndEncodeResponse(this.remoteService, rpcRequest.getMethod(), rpcRequest.getParameters(), rpcRequest.getSerializationPolicy());
      } catch (IncompatibleRemoteServiceException e) {
       return RPC.encodeResponseForFailure(null, e);
      }
     }
    
     /**
      * Setter for Spring injection of the GWT RemoteService object.
      * 
      * @param RemoteService
      *            The GWT RemoteService implementation that will be delegated to by the {@code GWTController}.
      */
     public void setRemoteService(RemoteService remoteService) {
      this.remoteService = remoteService;
      this.remoteServiceClass = this.remoteService.getClass();
     }
    
     @Override
     public ServletContext getServletContext() {
      return servletContext;
     }
    
     public void setServletContext(ServletContext servletContext) {
      this.servletContext = servletContext;
     }
    }
    
    0 讨论(0)
  • 2020-11-30 01:44

    to ensure the desired result delete all war/<app>/gwt/*.gwt.rpc

    0 讨论(0)
  • 2020-11-30 01:52

    The whitelist is generated by the gwt compiler and contains all the entries that are designated by the IsSerializable marker interface.

    To add a type to the list you just need to make sure that the class implements the IsSerializable interface.

    -- Andrej

    This is probably the easiest solution. The only thing to remember with this is that all the classes that you want to serialize should have "public, no-argument" constructor, and (depending upon requirements) setter methods for the member fields.

    0 讨论(0)
  • 2020-11-30 01:54

    The whitelist is generated by the GWT compiler and contains all the entries that are designated by the IsSerializable marker interface.

    To add a type to the list you just need to make sure that the class implements the IsSerializable interface.

    Additionally for serialization to work correctly the class must have a default no arg constructor (constructor can be private if needed). Also if the class is an inner it must be marked as static.

    0 讨论(0)
  • 2020-11-30 01:55

    IMHO the simpliest way to access whitelist programmatically is to create a class similar to this:

    public class SerializableWhitelist implements IsSerializable {
        String[] dummy1;
        SomeOtherThingsIWishToSerialize dummy2;
    }
    

    Then include it in the .client package and reference from the RPC service (so it gets analyzed by the compiler).

    I couldn't find a better way to enable tranfer of unparameterized maps, which is obviously what you sometimes need in order to create more generic services...

    0 讨论(0)
  • 2020-11-30 01:58

    I had this problem but ended up tracing the problem back to a line of code in my Serializable object:

    Logger.getLogger(this.getClass().getCanonicalName()).log(Level.INFO, "Foo");
    

    There were no other complaints before the exception gets caught in:

     @Override
      protected void serialize(Object instance, String typeSignature)
          throws SerializationException {
        assert (instance != null);
    
        Class<?> clazz = getClassForSerialization(instance);
    
        try {
          serializationPolicy.validateSerialize(clazz);
        } catch (SerializationException e) {
          throw new SerializationException(e.getMessage() + ": instance = " + instance);
        }
        serializeImpl(instance, clazz);
      }
    

    And the business end of the stack trace is:

    com.google.gwt.user.client.rpc.SerializationException: Type 'net.your.class' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.: instance = net.your.class@9c7edce
        at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:619)
    
    0 讨论(0)
提交回复
热议问题