2015-12-03 53 views
1

我將shiro auth從本機SQL更改爲JPA,並且我有一些quations。 我例如this linkthis link如何使用JPA創建apache shiro授權?

但我有錯誤。

[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. 

我不會忽視它是如何工作的。我創建JpaAuthorizingRealm類:

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); 
    } 

} 

和模型用戶,角色和權限。而在ini文件我註冊:

[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"] 

如果我改變JpaAuthorizingRealm extends AuthorizingRealmJpaAuthorizingRealm extends JdbcRealm誤差不顯示。

Maby somebode知道如何使用JPA創建shiro身份驗證?

回答

0

這似乎更像是一個鏈接錯誤而不是Shiro的問題。該錯誤表示您的代碼(或Shiro庫中的代碼)無法從commons-collections中找到FastHashMap類。

這很可能是因爲您的類路徑(您的應用程序,應用程序服務器等)中有不止一個commons-collections版本。問題可能是舊版本的commons-collections在更新的版本之前獲得了偏好,而舊版本不包括FastHashMap