jfinal shiro启动报错

错误信息:

严重: Exception sending context initialized event to listener instance of class org.apache.shiro.web.env.EnvironmentLoaderListener
org.apache.shiro.config.ConfigurationException: Unable to instantiate class [org.ccloud.shiro.ShiroDbRealm] for object named 'shiroDbRealm'.  Please ensure you've specified the fully qualified class name correctly.
    at org.apache.shiro.config.ReflectionBuilder.createNewInstance(ReflectionBuilder.java:151)
    at org.apache.shiro.config.ReflectionBuilder.buildObjects(ReflectionBuilder.java:119)
    at org.apache.shiro.config.IniSecurityManagerFactory.buildInstances(IniSecurityManagerFactory.java:161)
    at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:124)
    at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:102)
    at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:88)
    at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:46)
    at org.apache.shiro.config.IniFactorySupport.createInstance(IniFactorySupport.java:123)
    at org.apache.shiro.util.AbstractFactory.getInstance(AbstractFactory.java:47)
    at org.apache.shiro.web.env.IniWebEnvironment.createWebSecurityManager(IniWebEnvironment.java:203)
    at org.apache.shiro.web.env.IniWebEnvironment.configure(IniWebEnvironment.java:99)
    at org.apache.shiro.web.env.IniWebEnvironment.init(IniWebEnvironment.java:92)
    at org.apache.shiro.util.LifecycleUtils.init(LifecycleUtils.java:45)
    at org.apache.shiro.util.LifecycleUtils.init(LifecycleUtils.java:40)
    at org.apache.shiro.web.env.EnvironmentLoader.createEnvironment(EnvironmentLoader.java:221)
    at org.apache.shiro.web.env.EnvironmentLoader.initEnvironment(EnvironmentLoader.java:133)
    at org.apache.shiro.web.env.EnvironmentLoaderListener.contextInitialized(EnvironmentLoaderListener.java:58)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5118)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5634)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.shiro.util.UnknownClassException: Unable to load class named [org.ccloud.shiro.ShiroDbRealm] from the thread context, current, or system/application ClassLoaders.  All heuristics have been exhausted.  Class could not be found.
    at org.apache.shiro.util.ClassUtils.forName(ClassUtils.java:148)
    at org.apache.shiro.util.ClassUtils.newInstance(ClassUtils.java:164)
    at org.apache.shiro.config.ReflectionBuilder.createNewInstance(ReflectionBuilder.java:144)
    ... 25 more

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <listener>
        <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>ShiroFilter</filter-name>
        <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>ShiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    <filter>
        <filter-name>jfinal</filter-name>
        <filter-class>com.jfinal.core.JFinalFilter</filter-class>
        <init-param>
            <param-name>configClass</param-name>
            <param-value>org.ccloud.Config</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>jfinal</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

shiro.ini配置

[main]
#设置sessionCookie
sessionIdCookie=org.apache.shiro.web.servlet.SimpleCookie
sessionIdCookie.name=_ccloud_shiro_cookie_name
#sessionIdCookie.domain=localhost:8080
sessionIdCookie.path=/
#cookie最大有效期,单位秒
sessionIdCookie.maxAge=1800
sessionIdCookie.httpOnly=true

#\u5bc6\u7801\u6821\u9a8c\u65b9\u6cd5
passwordService = org.apache.shiro.authc.credential.DefaultPasswordService
passwordMatcher = org.apache.shiro.authc.credential.PasswordMatcher
passwordMatcher.passwordService = $passwordService

#realm
shiroDbRealm = org.ccloud.shiro.ShiroDbRealm
shiroDbRealm.credentialsMatcher = $passwordMatcher
shiroDbRealm.authorizationCacheName=_shiro_authorization_cache
securityManager.realms = $shiroDbRealm

authc.loginUrl = /admin/login

#\u7b56\u7565
#authcStrategy = com.lion.sys.plugin.shiro.ext.AnySuccessfulStrategy
#securityManager.authenticator.authenticationStrategy = $authcStrategy

#cache manager
cacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager
securityManager.cacheManager = $cacheManager

#session 基于缓存sessionDao,如果缓存已经实现共享,那么session也同样实现共享
sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
sessionDAO.activeSessionsCacheName = _shiro_active_session_cache

#设置session会话管理
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
sessionManager.sessionDAO = $sessionDAO
sessionManager.sessionIdCookie=$sessionIdCookie
sessionManager.sessionIdCookieEnabled=true

#session过期时间,单位毫秒,默认两天
securityManager.sessionManager = $sessionManager
securityManager.sessionManager.globalSessionTimeout = 1800000

[urls]
/admin/login = anon
/admin/logout = authc
/admin/* = authc
/index.jsp = anon
/ = anon
/mobile= anon

ShiroDbRealm

package org.ccloud.shiro;

import java.util.List;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.ccloud.model.User;
import org.ccloud.model.query.OperationQuery;
import org.ccloud.model.query.RoleQuery;
import org.ccloud.model.query.UserQuery;
import org.ccloud.utils.EncryptUtils;

public class ShiroDbRealm extends AuthorizingRealm {

    public ShiroDbRealm(){
        setAuthenticationTokenClass(CaptchaUsernamePasswordToken.class);
    }
    
    /**
     * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        User userInPrincipal = (User) principals.getPrimaryPrincipal();
        //根据用户获取权限
        List<String> roles = RoleQuery.me().getPermissions(userInPrincipal.getGroupId());
        List<String> stringPermissions = OperationQuery.me().getPermissions(roles);
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addRoles(roles);
        info.addStringPermissions(stringPermissions);
        return info;
    }

    /**
     * 认证回调函数,登录时调用.
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken authcToken = (UsernamePasswordToken) token;
        User user = UserQuery.me().findUserByUsername(authcToken.getUsername());
        if (user != null) {
            String password = EncryptUtils.encryptPassword(new String(authcToken.getPassword()), user.getSalt());
            if(!user.getPassword().equals(password)){
                throw new AuthenticationException("密码错误");
            }
            return new SimpleAuthenticationInfo(user, user.getPassword(),user.getUsername());
        } else {
            throw new AuthenticationException("用户不存在");
        }
    }

    /**
     * 更新用户授权信息缓存.
     */
    public void clearCachedAuthorizationInfo(String principal) {
        SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
        clearCachedAuthorizationInfo(principals);
    }

    /**
     * 清除所有用户授权信息缓存.
     */
    public void clearAllCachedAuthorizationInfo() {
        Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
        if (cache != null) {
            for (Object key : cache.keys()) {
                cache.remove(key);
            }
        }
    }
}



评论区

JFinal

2017-10-30 17:58

注意异常的这部分: Caused by: org.apache.shiro.util.UnknownClassException: Unable to load class

没有用过 Shiro , 没法帮到你

祥子

2018-03-09 14:12

今天遇到了这个问题,没有经过严格测试,将编译环境的jre换成 jdk 就好了,不知道是否确实是这样

热门反馈

扫码入社