【问题标题】:Shiro SessionDAO class loading issue in shared library共享库中的 Shiro SessionDAO 类加载问题
【发布时间】:2015-10-24 08:19:46
【问题描述】:

我收到了java.lang.ClassNotFoundException: com.example.pricing.shared.MyClass,但我不明白为什么该类位于类路径上。

这是我正在使用的配置: 我在同一个 Tomcat 7 实例中有 2 个战争文件:

  1. ROOT.war 指向 www.example.com/
  2. app.war指向 www.example.com/app/*

两个文件都包含 2 个共享库:

  1. commons.jar
  2. pricing.jar

commons.jar 库包含扩展 Shiro 的 AbstractSessionDAOSessionDAO 类。我的 SessionDAO 类是一个基本实现,它将 Shiro Session 保存在 MySQL 表中(VARCHAR id,BLOB Session 对象)。

现在,当我尝试访问 app.war 模块的 URL 时,在从数据库中反序列化 Session 时出现以下异常:

org.apache.shiro.session.SessionException: Class not found exception while reading Session from input stream!
com.example.dao.SessionDAO.deserialize(SessionDAO.java:97)
... (omitted)

Root cause:
java.lang.ClassNotFoundException: com.example.pricing.shared.MyClass 
java.net.URLClassLoader$1.run(URLClassLoader.java:366)

错误的反序列化方法如下:

private static Session deserialize(ResultSet resultSet, String col){
    ByteArrayInputStream bais=null;
    ObjectInputStream ins=null;
    Session res=null;
    try{
        byte[] barray=resultSet.getBytes(col);
        if(barray!=null){
            bais=new ByteArrayInputStream(barray);
            ins=new ObjectInputStream(bais);
            res=(Session)ins.readObject();
        }
    }catch(IOException e){
        throw new SessionException("IO exception while reading Session from input stream!",e);
    }catch(ClassNotFoundException e){
        throw new SessionException("Class not found exception while reading Session from input stream!",e);//the one causing the issue
    }catch(SQLException e){
        throw new SessionException("SQL exception while reading Session from input stream!",e);
    }finally{
        try{
            ins.close();
        }catch(NullPointerException|IOException e){}
        try{
            bais.close();
        }catch(NullPointerException|IOException e){}
    }
    return res;
}

你知道如何解决这个问题吗?

【问题讨论】:

    标签: java deserialization classnotfoundexception shiro


    【解决方案1】:

    Shiro 的库位于 Tomcat/lib 文件夹中,而 2 个 jar 位于 WEB-INF/lib 文件夹中。 在 WEB-INF/lib 中移动 Shiro 的库解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-10
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      相关资源
      最近更新 更多