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?

Jerry :

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. 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 -

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. Why can't I achieve what I need in Java?

Markus Rosjat :

well java aside, are you ssure you going to access your db on 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. 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?). and 10.0.0.0 shouldnt be a possible ip.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=357594&siteId=1