简体   繁体   English

为什么即使我的密码正确,我在 Java 程序中尝试访问 MariaDB 也会给我一个“访问被拒绝”异常?

[英]Why does my attempt to access a MariaDB in my Java program get give me an 'access denied' exception even tho my password is right?

I'm writing a Java program that queries an SQL database, and pulls out my query, to see if a username and password exist in my database.我正在编写一个查询 SQL 数据库的 Java 程序,并提取我的查询,以查看我的数据库中是否存在用户名和密码。 My code is:我的代码是:

My code is:我的代码是:

public UserInfo getUserExists(final String userEmail) throws SQLException, ClassNotFoundException {

    if (!isBlank(userEmail)) {
        Connection conn;
        Statement stmt;

        try{
            Class.forName("org.mariadb.jdbc.Driver");
            System.out.println("Connecting to DB...");

            conn = DriverManager.getConnection("jdbc:mariadb://10.0.0.0:3306/mydb","myusername", "mypassword");

            System.out.println("Connected to DB successfully");
            System.out.println("querying if user is valid");

            stmt = conn.createStatement();

            ResultSet rs;
            rs = stmt.executeQuery("SELECT IF (EXISTS(select user_email, user_pass, meta_value from myorg_users left join myorg_usermeta on ( myorg_users.id = myorg_usermeta.user_id) where meta_value LIKE '%\"administrator\"%' and meta_key=\"myorg_capabilities\" AND user_email="+userEmail+"),1,0) AS result;");
            int result = 0;
            System.out.println(rs);

            while(rs.next()){
                result = Integer.parseInt(rs.getString("result"));
                System.out.println("result is " + result);
            }
            conn.close();
            if(result == 1){
                UserInfo SYSTEM_USER = new UserInfo.Builder(UserInfo.Username.valueOf("SYSTEM_USER")).withFirstName("System").withLastName("User").withEmail(userEmail).withUserId("sample").build();
                System.out.println(SYSTEM_USER);
                return SYSTEM_USER;
            }

        }
        catch(SQLException se){
            se.printStackTrace();
        }
        catch(Exception e){
            e.printStackTrace();
        }

    } else {
        throw new AuthenticationException("user does not exists in system");
    }
    System.out.println("executed new user exists");
    return null;
}

However, when I run my API, I get this, and I KNOW the password/username are correct -但是,当我运行 API 时,我得到了这个,并且我知道密码/用户名是正确的 -

java.sql.SQLSyntaxErrorException: Access denied for user 'myusername'@'10.%' to database 'mydb'
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:163)
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.getException(ExceptionMapper.java:106)
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1036)
    at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:490)
    at org.mariadb.jdbc.MariaDbConnection.newConnection(MariaDbConnection.java:144)
    at org.mariadb.jdbc.Driver.connect(Driver.java:90)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.intuit.wasabi.authentication.impl.DefaultAuthentication.getUserExists(DefaultAuthentication.java:168)
    at com.intuit.wasabi.api.AuthenticationResource.getUserExists(AuthenticationResource.java:214)
    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.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at com.google.inject.servlet.ServletDefinition.doServiceImpl(ServletDefinition.java:287)
    at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:277)
    at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:182)
    at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
    at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:85)
    at com.intuit.autumn.web.WebFilter.doFilter(WebFilter.java:65)
    at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:82)
    at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:119)
    at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:133)
    at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:130)
    at com.google.inject.servlet.GuiceFilter$Context.call(GuiceFilter.java:203)
    at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:130)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:581)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1160)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1092)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:119)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
    at org.eclipse.jetty.server.Server.handle(Server.java:518)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:308)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:244)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:273)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
    at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:246)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:156)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.sql.SQLException: Access denied for user 'myusername'@'10.%' to database 'mydb'
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.authentication(AbstractConnectProtocol.java:787)
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.handleConnectionPhases(AbstractConnectProtocol.java:713)
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connect(AbstractConnectProtocol.java:402)
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1032)
    ... 59 more

What is going on?到底是怎么回事? I've run this query in the DB and it works in MariaDB.我在数据库中运行了这个查询,它在 MariaDB 中工作。 Why can't I achieve what I need in Java?为什么我不能在 Java 中实现我所需要的?

well java aside, are you ssure you going to access your db on 10.0.0.0 ?除了java,你确定你会在10.0.0.0上访问你的数据库吗? I would make an educated guess your MariaDB is configured to listen on loopback interface (127.0.0.1) so if your MariaDB isnt configured to listen an other interface you would get a connection refused kinda case.我会做出一个有根据的猜测,您的 MariaDB 配置为侦听环回接口 (127.0.0.1),因此如果您的 MariaDB 未配置为侦听其他接口,您会遇到连接被拒绝的情况。 So i would start to check this if you are sure you can do it in the phpmyadmin (i guess you use that and it might be accessed over localhost?).因此,如果您确定可以在 phpmyadmin 中执行此操作,我将开始检查这一点(我猜您使用了它并且可以通过 localhost 访问它?)。 and 10.0.0.0 shouldnt be a possible ip.并且 10.0.0.0 不应该是一个可能的 IP。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的Java RSA加密会给我一个算术异常? - Why does my Java RSA encryption give me an Arithmetic Exception? 无法覆盖Java中的隐藏文件。 当我尝试使用我的代码时,出现java.io.FileNotFoundException(访问被拒绝) - Unable to overwrite a hidden file in Java. When I attempt to with my code I get java.io.FileNotFoundException (Access is denied) 为什么我的applet得到java.security.AccessControlException:访问被拒绝(java.net.SocketPermission ...),我该如何避免它? - Why does my applet get a java.security.AccessControlException: access denied (java.net.SocketPermission …), and how can I avoid it? 为什么捕获异常后我的Java程序退出? - Why does my Java program exit after an exception is caught? 为什么availablePermits()函数无法在我的代码中给我正确的结果? Java线程 信号量| - Why availablePermits() function not give me right result in my code | Java Threads | Semaphore | 为什么我的程序不能访问Java中的数学方法? - Why can't my program access the math methods in Java? 为什么我的JSON-Simple JSONArray给我一个nullpointer异常? - Why does my JSON-Simple JSONArray give me a nullpointer exception? 为什么我的程序不接受自定义异常? - Why does my program not accept a custom exception? 为什么我的程序打印出异常语句会不断给用户带来不好的输入? - Why does my program print out the exception statement endlessly give bad user input? Java例外:为什么我的程序没有终止? - Java exception: why my program is not terminating?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM