java - 如何使用JPA创建apache shiro授权?

标签 java jpa shiro

我将 shiro auth 从 native SQL 更改为 JPA,并且我有一些公式。 例如我制作 this linkthis link

但我有错误。

[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 AuthorizingRealm 更改为 JpaAuthorizingRealm extends JdbcRealm 错误不会显示。

也许有人知道如何使用 JPA 创建 shiro auth 吗?

最佳答案

这看起来更像是链接错误,而不是 Shiro 的问题。该错误意味着您的代码(或 Shiro 库中的代码)无法从 commons-collections 中找到 FastHashMap 类。

这很可能是因为您的类路径(您的应用程序、应用程序服务器等)中有多个版本的 commons-collections。问题可能是旧版本的 commons-collections 在新版本之前获得优先权,并且旧版本不包含 FastHashMap

关于java - 如何使用JPA创建apache shiro授权?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34057192/

相关文章:

java - 在 eclipse 中使用 Runtime.getRuntime().exec

mysql - 如何使用 Eclipselink 在 MySql 中创建 BOOLEAN 列

java - ClassNotFoundException : org. apache.shiro.web.env.EnvironmentLoaderListener

java - 如何从 Java 中的 map 列表中获取所有值?

Java 复制和粘贴文件 NoSuchFileException

java - 在桌面上运行 Java 程序

mysql - Grails Shiro 插件创建只读表

mysql - EcliplseLink2.1.2中涉及一对多和多对一关系的外键约束问题

spring - @Query返回对象而不是实体

Grails Assets 管道 - 没有可用的