RedisSessionUtil报错,求助

发布于 2022-09-06 16:01:18 字数 17339 浏览 14 评论 0

使用框架为ssm,用户登录时,使用shiro校验。
我的工作环境在mac上,但同样一套代码在windows下运行正常???

Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(member.getUsername(),Md5Util.saltPassword(member.getUsername(), password));
usernamePasswordToken.setRememberMe(true);
subject.login(usernamePasswordToken);

执行到上面的 subject.login(usernamePasswordToken); 语句时报如下错误:

2018-02-28 14:52:50,298 ERROR [com.cc.aop.ResponseBodyAround] - <Could not initialize class com.cc.cache.redis.util.RedisSessionUtil>
java.lang.NoClassDefFoundError: Could not initialize class com.cc.cache.redis.util.RedisSessionUtil
    at com.cc.shiro.session.RedisSessionDAO.doCreate(RedisSessionDAO.java:60)
    at org.apache.shiro.session.mgt.eis.AbstractSessionDAO.create(AbstractSessionDAO.java:116)
    at org.apache.shiro.session.mgt.DefaultSessionManager.create(DefaultSessionManager.java:177)
    at org.apache.shiro.session.mgt.DefaultSessionManager.doCreateSession(DefaultSessionManager.java:158)
    at org.apache.shiro.session.mgt.AbstractValidatingSessionManager.createSession(AbstractValidatingSessionManager.java:136)
    at org.apache.shiro.session.mgt.AbstractNativeSessionManager.start(AbstractNativeSessionManager.java:99)
    at org.apache.shiro.mgt.SessionsSecurityManager.start(SessionsSecurityManager.java:152)
    at org.apache.shiro.subject.support.DelegatingSubject.getSession(DelegatingSubject.java:340)
    at org.apache.shiro.subject.support.DelegatingSubject.getSession(DelegatingSubject.java:316)
    at org.apache.shiro.mgt.DefaultSubjectDAO.mergePrincipals(DefaultSubjectDAO.java:207)
    at org.apache.shiro.mgt.DefaultSubjectDAO.saveToSession(DefaultSubjectDAO.java:165)
    at org.apache.shiro.mgt.DefaultSubjectDAO.save(DefaultSubjectDAO.java:146)
    at org.apache.shiro.mgt.DefaultSecurityManager.save(DefaultSecurityManager.java:387)
    at org.apache.shiro.mgt.DefaultSecurityManager.createSubject(DefaultSecurityManager.java:354)
    at org.apache.shiro.mgt.DefaultSecurityManager.createSubject(DefaultSecurityManager.java:187)
    at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:287)
    at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:260)
    at com.cc.xy.action.LoginAction.logon(LoginAction.java:55)
    at com.cc.xy.action.LoginAction$$FastClassBySpringCGLIB$$4c65fff3.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89)
    at com.cc.aop.ResponseBodyAround.processResponseBodyException(ResponseBodyAround.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:643)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:632)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
    at com.cc.xy.action.LoginAction$$EnhancerBySpringCGLIB$$34215121.logon(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:871)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:777)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.cc.web.filter.ActionContextFilter.doFilter(ActionContextFilter.java:24)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
    at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
    at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
    at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
    at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:387)
    at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
    at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

RedisSessionUtil的代码如下所示:

public final class RedisSessionUtil {
    private static ShardedJedisPool pool = null;

    static {
        Properties p = new Properties();

        try {
            p.load(RedisSessionUtil.class.getClassLoader().getResourceAsStream("config/session.properties"));
        } catch (IOException var18) {
            throw new RuntimeException("config/session.properties not config ~");
        }

        String blockWhenExhausted = p.getProperty("session.blockWhenExhausted");
        String evictionPolicyClassName = p.getProperty("session.evictionPolicyClassName");
        String jmxNamePrefix = p.getProperty("session.jmxNamePrefix");
        String lifo = p.getProperty("session.lifo");
        String maxIdle = p.getProperty("session.maxIdle");
        String maxTotal = p.getProperty("session.maxTotal");
        String maxWaitMillis = p.getProperty("session.maxWaitMillis");
        String minEvictableIdleTimeMillis = p.getProperty("session.minEvictableIdleTimeMillis");
        String minIdle = p.getProperty("session.minIdle");
        String numTestsPerEvictionRun = p.getProperty("session.numTestsPerEvictionRun");
        String softMinEvictableIdleTimeMillis = p.getProperty("session.softMinEvictableIdleTimeMillis");
        String testOnBorrow = p.getProperty("session.testOnBorrow");
        String testOnReturn = p.getProperty("session.testOnReturn");
        String testWhileIdle = p.getProperty("session.testWhileIdle");
        String timeBetweenEvictionRunsMillis = p.getProperty("session.timeBetweenEvictionRunsMillis");
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        if(StringUtil.isNotEmpty(blockWhenExhausted)) {
            poolConfig.setBlockWhenExhausted(Boolean.valueOf(blockWhenExhausted).booleanValue());
        }

        if(StringUtil.isNotEmpty(evictionPolicyClassName)) {
            poolConfig.setEvictionPolicyClassName(evictionPolicyClassName);
        }

        if(StringUtil.isNotEmpty(jmxNamePrefix)) {
            poolConfig.setJmxNamePrefix(jmxNamePrefix);
        }

        if(StringUtil.isNotEmpty(lifo)) {
            poolConfig.setLifo(Boolean.valueOf(lifo).booleanValue());
        }

        if(StringUtil.isNotEmpty(maxIdle)) {
            poolConfig.setMaxIdle(Integer.parseInt(maxIdle));
        }

        if(StringUtil.isNotEmpty(maxTotal)) {
            poolConfig.setMaxTotal(Integer.parseInt(maxTotal));
        }

        if(StringUtil.isNotEmpty(maxWaitMillis)) {
            poolConfig.setMaxWaitMillis((long)Integer.parseInt(maxWaitMillis));
        }

        if(StringUtil.isNotEmpty(minEvictableIdleTimeMillis)) {
            poolConfig.setMinEvictableIdleTimeMillis(Long.parseLong(minEvictableIdleTimeMillis));
        }

        if(StringUtil.isNotEmpty(minIdle)) {
            poolConfig.setMinIdle(Integer.parseInt(minIdle));
        }

        if(StringUtil.isNotEmpty(numTestsPerEvictionRun)) {
            poolConfig.setNumTestsPerEvictionRun(Integer.parseInt(numTestsPerEvictionRun));
        }

        if(StringUtil.isNotEmpty(softMinEvictableIdleTimeMillis)) {
            poolConfig.setSoftMinEvictableIdleTimeMillis(Long.parseLong(softMinEvictableIdleTimeMillis));
        }

        if(StringUtil.isNotEmpty(testOnBorrow)) {
            poolConfig.setTestOnBorrow(Boolean.valueOf(testOnBorrow).booleanValue());
        }

        if(StringUtil.isNotEmpty(testOnReturn)) {
            poolConfig.setTestOnReturn(Boolean.valueOf(testOnReturn).booleanValue());
        }

        if(StringUtil.isNotEmpty(testWhileIdle)) {
            poolConfig.setTestWhileIdle(Boolean.valueOf(testWhileIdle).booleanValue());
        }

        if(StringUtil.isNotEmpty(timeBetweenEvictionRunsMillis)) {
            poolConfig.setTimeBetweenEvictionRunsMillis(Long.parseLong(timeBetweenEvictionRunsMillis));
        }

        String servers = p.getProperty("session.servers");
        pool = new ShardedJedisPool(poolConfig, JedisShardInfoUtil.getShareInfo(servers));
    }

    private RedisSessionUtil() {
    }

    public static <T> T get(String key) {
        ShardedJedis shardedJedis = null;

        try {
            shardedJedis = pool.getResource();
            byte[] e = key.getBytes("utf-8");
            Object value = SerializeUtil.unserialize(shardedJedis.get(e));
            Object var5 = value;
            return var5;
        } catch (Exception var8) {
            var8.printStackTrace();
        } finally {
            if(shardedJedis != null) {
                shardedJedis.close();
            }

        }

        return null;
    }

    public static void put(String key, Object o, int exp) {
        ShardedJedis shardedJedis = null;

        try {
            shardedJedis = pool.getResource();
            byte[] e = key.getBytes("utf-8");
            byte[] bValue = SerializeUtil.serialize(o);
            shardedJedis.setex(e, exp, bValue);
        } catch (Exception var9) {
            var9.printStackTrace();
        } finally {
            if(shardedJedis != null) {
                shardedJedis.close();
            }

        }

    }

    public static void put(String key, Object o) {
        ShardedJedis shardedJedis = null;

        try {
            shardedJedis = pool.getResource();
            byte[] e = key.getBytes("utf-8");
            byte[] bValue = SerializeUtil.serialize(o);
            shardedJedis.set(e, bValue);
        } catch (Exception var8) {
            var8.printStackTrace();
        } finally {
            if(shardedJedis != null) {
                shardedJedis.close();
            }

        }

    }

    public static void remove(String key) {
        ShardedJedis shardedJedis = null;

        try {
            shardedJedis = pool.getResource();
            byte[] e = key.getBytes("utf-8");
            shardedJedis.del(e);
        } catch (Exception var6) {
            var6.printStackTrace();
        } finally {
            if(shardedJedis != null) {
                shardedJedis.close();
            }

        }

    }

    public static void expire(String key, int seconds) {
        ShardedJedis shardedJedis = null;

        try {
            shardedJedis = pool.getResource();
            byte[] e = key.getBytes("utf-8");
            shardedJedis.expire(e, seconds);
        } catch (Exception var7) {
            var7.printStackTrace();
        } finally {
            if(shardedJedis != null) {
                shardedJedis.close();
            }

        }

    }

    public static Set<String> keys(String key) {
        HashSet sets = new HashSet();
        ShardedJedis shardedJedis = null;

        try {
            shardedJedis = pool.getResource();
            byte[] e = key.getBytes("utf-8");
            Collection jedises = shardedJedis.getAllShards();
            Iterator var6 = jedises.iterator();

            while(var6.hasNext()) {
                Jedis jedis = (Jedis)var6.next();
                Set set = jedis.keys(e);
                Iterator var9 = set.iterator();

                while(var9.hasNext()) {
                    byte[] bs = (byte[])var9.next();
                    String k = new String(bs, "utf-8");
                    sets.add(k);
                }
            }
        } catch (Exception var14) {
            var14.printStackTrace();
        } finally {
            if(shardedJedis != null) {
                shardedJedis.close();
            }

        }

        return sets;
    }

    protected void finalize() throws Throwable {
        super.finalize();
        if(pool != null) {
            pool.close();
            pool = null;
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

究竟谁懂我的在乎 2022-09-13 16:01:18

我的出错原因在于如下语句:

p.load(RedisSessionUtil.class.getClassLoader().getResourceAsStream("config/session.properties"));

其中的 session.properties 该配置文件未放置在 config


但我有一点不懂不明白,为什么同一套代码在windows下能正常运行,在mac上却报错???

梦罢 2022-09-13 16:01:18

java.lang.NoClassDefFoundError: Could not initialize class com.cc.cache.redis.util.RedisSessionUtil...
RedisSessionUtil的静态代码块出现异常导致无法初始化,debug一下看看

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文