Search code examples
javaweb-servicesinheritancesingle-sign-onhttpsession

Is it possible to clone a class in Java?


I have this monolith JAR in my current web server configuration. The whole system was drawn in a way that you can't have separate web apps, everything runs around this big jar.

In the process of solving this, I'm trying to take out the sso authentication from the original security package (which I have the source code), the problem I'm dealing with is related to a Usuario class.

From what I understand, the original class will set a 'usuario' atribute, after getting the atributes from the tokenid

usuario = ssoUtil.getAtributosUsuario(tokenId);

httpSession.setAttribute("usuario", usuario);
httpServletRequest.setAttribute("usuario", usuario);

I changed the Usuario class to UsuarioSSO, and implemented the whole authentication thing as a global server filter -since it's a requirement of the project that it is impossible to publish anything on the server without authentication.

The actual Usuario class - the one that is set - is just a bunch of private variables, getters, setters and two methods with the implementation of the classes Serializable and HttpSessionBindingListener.

The issue arises when any classes from the original INTL problem try to get the Usuario atributes from the httpSession:

java.lang.ClassCastException: pathToClass.UsuarioSSO cannot be cast to intlPathToClass.Usuario

What I'm trying to achieve is to get this cast to work. But it seems impossible to do a downcast, even thou both classes are identical.

Just FYI, I can change the INTL Usuario class at will, but I can't change the code that actually uses that class, everything would work fine if I could change all references to INTL Usuario to UsuarioSSO.

Any suggestions?


Solution

  • I agree with Erickson's comments. You can move the class to a separate jar. You can still preserve the original package name.

    Two jars can use the same package names. The only time it gets weird is if those packages have the same classes. But that should be a problem if you move the Usuario class.