我正在尝试设置一个使用Spark和Apache shiro的简单Web应用程序(用于身份验证和授权)。
我希望从MySQL数据库中进行身份验证,但是我找到了无法找到它的ClassNotFoundException
。
这是我的shiro.ini配置文件:
[main]
jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
ds = com.mysql.jdbc.jdbc2.optional.MysqlDataSource
ds.serverName = localhost
ds.user = ...
ds.password = ...
ds.databaseName = mydb
jdbcRealm.dataSource = $ds
jdbcRealm.permissionsLookupEnabled = true
jdbcRealm.authenticationQuery = "SELECT password FROM users WHERE user_name = ?"
jdbcRealm.userRolesQuery = "SELECT role_name FROM user_roles WHERE user_name = ?"
jdbcRealm.permissionsQuery = "SELECT permission FROM roles_permissions WHERE role_name = ?
我的build.gradle文件
group 'test'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'application'
mainClassName = "Main"
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'com.sparkjava:spark-core:2.5.4'
compile 'ch.qos.logback:logback-classic:1.1.8'
compile 'org.apache.shiro:shiro-core:1.1.0'
compile 'mysql:mysql-connector-java:6.0.5'
compile 'commons-logging:commons-logging:1.2'
compile 'commons-dbcp:commons-dbcp:1.4'
}
jar {
from {
(configurations.runtime).collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
我得到的例外是:
Exception in thread "main" org.apache.shiro.config.ConfigurationException: Unable to instantiate class [com.mysql.jdbc.jdbc2.optional.MysqlDataSource] for object named 'ds'. Please ensure you've specified the fully qualified class name correctly.
at org.apache.shiro.config.ReflectionBuilder.createNewInstance(ReflectionBuilder.java:143)
at org.apache.shiro.config.ReflectionBuilder.buildObjects(ReflectionBuilder.java:114)
at org.apache.shiro.config.IniSecurityManagerFactory.buildInstances(IniSecurityManagerFactory.java:170)
at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:119)
at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:97)
at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:83)
at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:41)
at org.apache.shiro.config.IniFactorySupport.createInstance(IniFactorySupport.java:123)
at org.apache.shiro.util.AbstractFactory.getInstance(AbstractFactory.java:47)
at Main.main(Main.java:23)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.apache.shiro.util.UnknownClassException: Unable to load class named [com.mysql.jdbc.jdbc2.optional.MysqlDataSource] 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:136)
... 14 more
虽然我不是100%肯定,但我认为问题是mysql驱动程序在运行时不在类路径上...
答案 0 :(得分:1)
尝试使用
com.mysql.cj.jdbc.MysqlDataSource
代替
com.mysql.jdbc.jdbc2.optional.MysqlDataSource
您正在编译mysql-connector-java:6.0.5
。