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