为了账号安全,请及时绑定邮箱和手机立即绑定

在视频2分23秒处运行出错

这是JdbcRealmTest里的代码

package com.imooc.test;


import com.alibaba.druid.pool.DruidDataSource;

import org.apache.shiro.SecurityUtils;

import org.apache.shiro.authc.UsernamePasswordToken;

import org.apache.shiro.mgt.DefaultSecurityManager;

import org.apache.shiro.realm.jdbc.JdbcRealm;

import org.apache.shiro.subject.Subject;

import org.junit.Test;


public class JdbcRealmTest {

    //创建一个数据源

    DruidDataSource dataSource = new DruidDataSource();


    {

        //设置jdbc的url

        dataSource.setUrl("jdbc:mysql://localhost:3306/test");

        //设置数据库的访问用户和密码

        dataSource.setUsername("root");

        dataSource.setPassword("root");

    }


    @Test

    public void testAuthentication() {

        //创建一个jdbcRealm对象,需要访问数据库

        JdbcRealm jdbcRealm = new JdbcRealm();


        //设置jdbcRealm的数据源

        jdbcRealm.setDataSource(dataSource);


        //1.构建SecurityManager环境

        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();

        defaultSecurityManager.setRealm(jdbcRealm);


        //2.主体提交认证请求

        SecurityUtils.setSecurityManager(defaultSecurityManager);

        Subject subject = SecurityUtils.getSubject();


        UsernamePasswordToken token = new UsernamePasswordToken("Mark", "123456");

        subject.login(token);


        System.out.println("isAuthenticated:" + subject.isAuthenticated());

    }

}


这是报错信息

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

四月 20, 2019 4:51:59 下午 com.alibaba.druid.pool.DruidDataSource info

信息: {dataSource-1} inited


org.apache.shiro.authc.AuthenticationException: There was a SQL error while authenticating user [Mark]


at org.apache.shiro.realm.jdbc.JdbcRealm.doGetAuthenticationInfo(JdbcRealm.java:260)

at org.apache.shiro.realm.AuthenticatingRealm.getAuthenticationInfo(AuthenticatingRealm.java:571)

at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doSingleRealmAuthentication(ModularRealmAuthenticator.java:180)

at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:267)

at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)

at org.apache.shiro.mgt.AuthenticatingSecurityManager.authenticate(AuthenticatingSecurityManager.java:106)

at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:274)

at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:260)

at com.imooc.test.JdbcRealmTest.testAuthentication(JdbcRealmTest.java:40)

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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)

at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:365)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)

at org.junit.runners.ParentRunner$4.run(ParentRunner.java:330)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:78)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:328)

at org.junit.runners.ParentRunner.access$100(ParentRunner.java:65)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:292)

at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)

at org.junit.runners.ParentRunner.run(ParentRunner.java:412)

at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.users' doesn't exist

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)

at com.mysql.jdbc.Util.getInstance(Util.java:408)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)

at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484)

at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)

at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966)

at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeQuery(DruidPooledPreparedStatement.java:228)

at org.apache.shiro.realm.jdbc.JdbcRealm.getPasswordForUser(JdbcRealm.java:290)

at org.apache.shiro.realm.jdbc.JdbcRealm.doGetAuthenticationInfo(JdbcRealm.java:227)

... 33 more



Process finished with exit code -1

如果将Mark和密码123456改为root,还是报相同的错误。如果注释mark以及下面那一行,则显示认证失败

求大神解答

正在回答

2 回答

你们谁有老师的数据库啊,能发一份?

0 回复 有任何疑惑可以回复我~

知道为啥了  是因为数据库里啥也没有 需要建表才能运行成功

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

在视频2分23秒处运行出错

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信