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.servlet.ServletException ; 28 import javax.servlet.http.HttpServlet ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.servlet.http.HttpSession ; 32 import javax.security.auth.Subject ; 33 34 import org.jboss.security.SecurityAssociation; 35 36 42 public class SecureServlet extends HttpServlet 43 { 44 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 45 throws ServletException , IOException 46 { 47 Principal user = request.getUserPrincipal(); 48 String validateSubject = request.getParameter("validateSubject"); 49 if( validateSubject != null && Boolean.valueOf(validateSubject).booleanValue() ) 50 { 51 Subject subject = SecurityAssociation.getSubject(); 53 if( subject == null ) 54 throw new ServletException ("No valid subject found, user="+user); 55 } 56 HttpSession session = request.getSession(false); 57 response.setContentType("text/html"); 58 PrintWriter out = response.getWriter(); 59 out.println("<html>"); 60 out.println("<head><title>SecureServlet</title></head>"); 61 out.println("<h1>SecureServlet Accessed</h1>"); 62 out.println("<body>"); 63 out.println("You have accessed this servlet as user:"+user); 64 if( session != null ) 65 out.println("<br>The session id is: "+session.getId()); 66 else 67 out.println("<br>There is no session"); 68 out.println("</body></html>"); 69 out.close(); 70 } 71 72 protected void doGet(HttpServletRequest request, HttpServletResponse response) 73 throws ServletException , IOException 74 { 75 processRequest(request, response); 76 } 77 78 protected void doPost(HttpServletRequest request, HttpServletResponse response) 79 throws ServletException , IOException 80 { 81 processRequest(request, response); 82 } 83 84 } 85 | Popular Tags |