3 Replies Latest reply on Apr 26, 2004 2:17 AM by martin.weidig

    LoginContext for Servlet

    martin.weidig

      When I try to use a secure bean with this method in a servlet a
      javax.servlet.ServletException: EJBException:; nested exception is: javax.ejb.EJBException: checkSecurityAssociation; CausedByException is:
      Authentication exception, principal=null
      was thrown.

      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      System.setProperty("java.security.auth.login.config", "file://d:/wudo/auth.conf");
      try {
      org.jboss.security.auth.callback.UsernamePasswordHandler handler = new org.jboss.security.auth.callback.UsernamePasswordHandler("weidig", "wunden".toCharArray());
      javax.security.auth.login.LoginContext loginContext = new javax.security.auth.login.LoginContext("handlerName", handler);
      loginContext.login();
      java.util.Hashtable JNDIParm = new java.util.Hashtable();
      JNDIParm.put(Context.PROVIDER_URL, "localhost");
      JNDIParm.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      javax.naming.InitialContext ejbContext = new javax.naming.InitialContext(JNDIParm);

      AnyBeanHome bh = (AnyBeanHome)ejbContext.lookup("AnyBean");
      AnyBean b = bh.create();
      b.doAnything();
      out.println("ok!");
      } catch (Exception ex) {
      throw new ServletException(ex);
      }
      }

      This code works perfect when I use it in an application!
      What is my mistake?