1

I change shiro auth from native SQL to JPA and I have some quations. I make for example this link and this link

but i have errors.

[2015-12-03 08:58:33,087] Artifact ear:ear exploded: Artifact is being deployed, please wait...
[2015-12-03 08:59:06,931] Artifact ear:ear exploded: Error during artifact deployment. See server log for details.
[2015-12-03 08:59:06,932] Artifact ear:ear exploded: java.io.IOException: com.sun.enterprise.admin.remote.RemoteFailureException: Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.NoClassDefFoundError: org/apache/commons/collections/FastHashMap. Please see server.log for more details.

I not understend how it work. I Create JpaAuthorizingRealm class:

public class JpaAuthorizingRealm extends AuthorizingRealm {

    public static final String REALM_NAME = "MY_REALM";
    public static final int HASH_ITERATIONS = 200;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(final PrincipalCollection principals) {
        Long userId = (Long) principals.fromRealm(getName()).iterator().next();
        User user = ShiroDao.me().userById(userId);
        if (user != null) {
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            for (Role role : user.getRoles()) {
                info.addRole(role.getRoleName());
                for (Permission permition : user.getPermissions()) {
                    info.addStringPermission(permition.getPermission());
                }
            }
            return info;
        } else {
            return null;
        }
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(final AuthenticationToken authToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authToken;
        User user = ShiroDao.me().userByname(token.getUsername());
        if (user != null) {
            return new SimpleAuthenticationInfo(user.getId(), user.getPassword(), getName());
        } else {
            return null;
        }
    }

    @Override
    @Inject
    public void setCredentialsMatcher(final CredentialsMatcher credentialsMatcher) {
        super.setCredentialsMatcher(credentialsMatcher);
    }

}

And models User, Role and Permission. And in ini file i registered :

[main]
cacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager
securityManager.cacheManager = $cacheManager

# realms to be used
adRealm = myPackeg.CustomActiveDirectoryRealm
adRealm.url = ldap://myIP

noPassWordCredentialMatcher=myPackeg.CustomNoPassMatcher

userRealm=myPackeg.JpaAuthorizingRealm
userRealm.permissionsLookupEnabled=true
userRealm.credentialsMatcher=$noPassWordCredentialMatcher

authc.loginUrl = /login.xhtml
user.loginUrl = /login.xhtml
authc.successUrl  = /index.xhtml?faces-redirect=true
roles.unauthorizedUrl = /error/ErrorInsufficientPrivileges.xhtml?faces-redirect=true

securityManager.realms= $adRealm, $customSecurityRealm
authcStrategy = org.apache.shiro.authc.pam.AllSuccessfulStrategy
securityManager.authenticator.authenticationStrategy = $authcStrategy

;multipleroles = myPackeg.MultipleRolesAuthorizationFilter
multipleroles = myPackeg.MultipleRolesAuthorizationFilter

[roles]

[urls]
/javax.faces.resource/** = anon
/error/ = anon
/login.xhtml = authc
/logout = logout
#/admin/ChangePassword.xhtml= authc, roles[user]
/admin/**= authc, roles[administrator]
/reports/qcforcc_report.xhtml= authc, roles[user]
/reports/**= authc, roles[administrator]
/** = authc, roles[user]
#/** = user, multipleroles["administrator", "user"]

And if i change JpaAuthorizingRealm extends AuthorizingRealm to JpaAuthorizingRealm extends JdbcRealm error not shows.

Maby somebode know how create shiro auth with JPA?

Community
  • 1
  • 1
user5620472
  • 2,722
  • 8
  • 44
  • 97

2 Answers2

0

This seems to more like a linkage error than a problem with Shiro. The error means that your code (or code in Shiro library) cannot find FastHashMap class from commons-collections.

This is most probably because have more than a single version of commons-collections in your classpath (your application, app server, etc.). The problem might be that an older version of commons-collections is getting preference before the newer version, and that the older version does not include FastHashMap.

OndroMih
  • 7,280
  • 1
  • 26
  • 44
0

I have implemented shiro with jpa and you can find the source code at https://github.com/nmojir/rest-basic-auth.