users@glassfish.java.net

Re: JAAS jdbcRealm error

From: <glassfish_at_javadesktop.org>
Date: Wed, 10 Oct 2007 03:09:03 PDT

Did you test the jdbc-resource? Here's an easy way to test it.

1) create a simple web module -- very simple. No realms, no nothing.
2) Use this code for processRequest:

import java.io.*;
import java.net.*;
import java.sql.*;
import javax.naming.*;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet TestDBServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet TestDBServlet at " + request.getContextPath () + "</h1>");

        try
        {
            InitialContext ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup("jdbc/userauth");
            Connection connection = ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("select " +
                     usertable.userid,password,groupid " +
                "from usertable,grouptable where usertable.userid=grouptable.userid");

            while(rs.next())
            {
                out.println("user, pw, group: " + rs.getString(1)
                    + ", " + rs.getString(2) + ", " + rs.getString(3) + "<br>");
            }
        }
        catch (Exception ex)
        {
            out.println("Got an Exception: " + ex);
        }

        out.println("</body>");
        out.println("</html>");
        out.close();
    }
--------------------------------
Run the servlet. Do you see a line for every USER--Role combination?

Here's my output:
user, pw, group: admin, 21232f297a57a5a743894a0e4a801fc3, ADMINISTRATORS
user, pw, group: admin, 21232f297a57a5a743894a0e4a801fc3, USERS
user, pw, group: bnevins, e6df6f20f9fbdca5d524b3c412fc4957, ADMINISTRATORS
user, pw, group: bnevins, e6df6f20f9fbdca5d524b3c412fc4957, SUPER_USERS
user, pw, group: bnevins, e6df6f20f9fbdca5d524b3c412fc4957, USERS
user, pw, group: fake, 7694f4a66316e53c8cdd9d9954bd611d, ADMINISTRATORS
user, pw, group: fake, 7694f4a66316e53c8cdd9d9954bd611d, MEGA
user, pw, group: fake, 7694f4a66316e53c8cdd9d9954bd611d, SUPER_USERS
user, pw, group: fake, 7694f4a66316e53c8cdd9d9954bd611d, USERS
user, pw, group: k2, 7694f4a66316e53c8cdd9d9954bd611d, ADMINISTRATORS
user, pw, group: k2, 7694f4a66316e53c8cdd9d9954bd611d, SUPER_USERS
user, pw, group: k2, 7694f4a66316e53c8cdd9d9954bd611d, USERS
user, pw, group: k2, 7694f4a66316e53c8cdd9d9954bd611d, zzz
user, pw, group: qbert, 28431c85d7c3ddfeb73c49c2addce419, ADMINISTRATORS
user, pw, group: qbert, 28431c85d7c3ddfeb73c49c2addce419, SUPER_USERS
user, pw, group: qbert, 28431c85d7c3ddfeb73c49c2addce419, USERS
[Message sent by forum member 'bnevins' (bnevins)]

http://forums.java.net/jive/thread.jspa?messageID=239273