1 Reply Latest reply on Jul 4, 2019 9:33 AM by djapal

    Wildfly 15.0.1 custom principal

    djapal

      Hello all.

       

      We are trying to migrate our application from Wildfly 10.1 to Wilfyl 15.0.1.

      We have a custom login module which needs to be migrated.

      I found this article

      Creating custom security realm for WildFly Elytron · Honza

      that helped me create the custom login module. So far so good. The login module is not migrated yet, but I have tried with dummy settings for username and password which works fine.

      The problem is that we also have a custom Principal class that has some extra methods and we always get at the end the error

       

      java.lang.ClassCastException: org.wildfly.security.auth.principal.NamePrincipal cannot be cast to OUR_CUSTOM_PRINCIPAL_CLASS

       

      In my custom realm I have the following code:

       

      public RealmIdentity getRealmIdentity(final Principal principal) throws RealmUnavailableException {

        if ("testuser".equals(principal.getName())) {

         return new RealmIdentity() {

           public Principal getRealmIdentityPrincipal() {

             MyCustomPrincipal principalObj = new MyCustomPrincipal(principal.getName());

             principalObj.setRoles(Arrays.asList("admin"));

             return principalObj;

        }

       

      Is this valid? Can I return my custom principal here or do I need sth else?

       

      Thank you

        • 1. Re: Wildfly 15.0.1 custom principal
          djapal

          OK, so after a detailed search among forums/blog posts and the documentation, I think I found the solution.

           

          I had to create a custom principal  transformer which takes the NamedPrincipal class and creates a new object instance of the custom principal class.

          This custom transformer is then registered as pre-realm-principal-transformer.

          So

          return new RealmIdentity() {

           

          is now returning the original Principal object which is an instance of the custom principal class.