1 22 package org.jboss.test.web.servlets; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.security.Principal ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NamingException ; 29 import javax.servlet.ServletConfig ; 30 import javax.servlet.ServletException ; 31 import javax.servlet.http.HttpServlet ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.security.auth.login.LoginContext ; 35 import javax.security.auth.login.LoginException ; 36 37 import org.jboss.security.auth.callback.UsernamePasswordHandler; 38 import org.jboss.test.web.interfaces.StatelessSession; 39 import org.jboss.test.web.interfaces.StatelessSessionHome; 40 41 46 public class ClientLoginServlet extends HttpServlet 47 { 48 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 49 throws ServletException , IOException 50 { 51 LoginContext lc = null; 52 String echoMsg = null; 53 try 54 { 55 lc = doLogin("jduke", "theduke"); 56 InitialContext ctx = new InitialContext (); 57 StatelessSessionHome home = (StatelessSessionHome) ctx.lookup("java:comp/env/ejb/SecuredEJB"); 58 StatelessSession bean = home.create(); 59 echoMsg = bean.echo("ClientLoginServlet called SecuredEJB.echo"); 60 } 61 catch(LoginException e) 62 { 63 throw new ServletException ("Failed to login to client-login domain as jduke", e); 64 } 65 catch(Exception e) 66 { 67 throw new ServletException ("Failed to access SecuredEJB", e); 68 } 69 finally 70 { 71 if( lc != null ) 72 { 73 try 74 { 75 lc.logout(); 76 } 77 catch(LoginException e) 78 { 79 } 80 } 81 } 82 83 response.setContentType("text/html"); 84 PrintWriter out = response.getWriter(); 85 out.println("<html>"); 86 out.println("<head><title>ClientLoginServlet</title></head>"); 87 out.println("<h1>ClientLoginServlet Accessed</h1>"); 88 out.println("<body>Login as user=jduke succeeded.<br>SecuredEJB.echo returned:"+echoMsg+"</body>"); 89 out.println("</html>"); 90 out.close(); 91 } 92 93 protected void doGet(HttpServletRequest request, HttpServletResponse response) 94 throws ServletException , IOException 95 { 96 processRequest(request, response); 97 } 98 99 protected void doPost(HttpServletRequest request, HttpServletResponse response) 100 throws ServletException , IOException 101 { 102 processRequest(request, response); 103 } 104 105 private LoginContext doLogin(String username, String password) throws LoginException 106 { 107 UsernamePasswordHandler handler = new UsernamePasswordHandler(username, password.toCharArray()); 108 LoginContext lc = new LoginContext ("client-login", handler); 109 lc.login(); 110 return lc; 111 } 112 } 113 | Popular Tags |