How can I use JAXB from an unsigned applet (without signing it)?

后端 未结 3 1030
生来不讨喜
生来不讨喜 2021-01-22 12:50

I would like to marshall Java objects into XML and vice-versa from within an Unsigned Applet and I can\'t change any of the security permission/policy files, or sign the applica

相关标签:
3条回答
  • 2021-01-22 12:53

    I also tried to find a solution with JAXB without any success.

    I switched to Axis2 (1.5.4) with ADB binding, but it also tried accessing system properties which failed with security manager checks.

    In the end, I got a workable solution together by using AspectJ and using an aspect that rewrites System.getProperty() calls to return null when they fail. Since all the properties Axis2 needed were non-critical anyway this worked. I also needed to apply an aspect over org.apache.axiom.util.stax.dialect.StAXDialectDetector.getRootUrlForResource() to always return null since it tried to make a ClassLoader.getSystemClassLoader() call which also failed under a security manager. Again this seemed like a non-critical call. Got AspectJ to rewrite the Axis2 classes at build-time and it ran as an unsigned applet.

    It's a really messy solution, but at least it worked.

    I couldn't get the same AspectJ hack working with JAXB because JAXB requires direct access to the private fields of classes which doesn't fly under a security manager if JAXB is bundled with the applet (which we need to do if AspectJ is used to rewrite classes).

    0 讨论(0)
  • 2021-01-22 12:57

    I never did quite figure this one out. What I did instead was to grab a fairly simple JSON library called Flexjson. It also threw a similar security exception but the library was simple enough that I was able to switch-off the library code that causes the exception in an Applet with a boolean flag.

    0 讨论(0)
  • 2021-01-22 13:07

    This may solve your problem. I know it solved mine :)

    public void actionPerformed(ActionEvent e) {
        try {
        JAXBContext jc = AccessController.doPrivileged(new PrivilegedExceptionAction<JAXBContext>() {
    
            public JAXBContext run() throws JAXBException {
    
                // needs to run here otherwise throws AccessControlException
                return JAXBContext.newInstance(SimpleObject.class);
            }
        });         
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
            SimpleObject object = new SimpleObject();
            object.setSampleText("Hello");
    
            marshaller.marshal(object, System.out);
        }
        catch (JAXBException e1) {
            throw new RuntimeException(e1);
        }
        } catch (PrivilegedActionException e2) {
        throw new RuntimeException(e2);
        }
    }
    

    Hope it helps

    0 讨论(0)
提交回复
热议问题