Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Servlets Code Examples

Get security information

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
Get security information package com.ack.web.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class GetSecurityInformation extends HttpServlet { public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { res.setContentType( "text/html" ); PrintWriter pw = res.getWriter(); /** * security information made available within servlets */ pw.println( "<br>user principal: " + req.getUserPrincipal().getName() ); pw.println( "<br>authentication type: " + req.getAuthType() ); pw.println( "<br>user in footie role: " + req.isUserInRole( "footie" ) ); pw.println( "<br>are we using HTTPS: " + req.isSecure() ); } } /** * Here is a dump of the web.xml file containing the security * configuration details. * <web-app> <servlet> <servlet-name>footie_time</servlet-name> <servlet-class>com.ack.web.servlet.BasicAuthenticationWebResource</servlet-class> </servlet> <servlet-mapping> <servlet-name>footie_time</servlet-name> <url-pattern>/protected/footie/get_time</url-pattern> </servlet-mapping> <servlet> <servlet-name>security_info</servlet-name> <servlet-class>com.ack.servlet.GetSecurityInfo</servlet-class> </servlet> <servlet-mapping> <servlet-name>security_info</servlet-name> <url-pattern>/protected/footie/info</url-pattern> </servlet-mapping> <security-constraint> <web-resource-collection> <web-resource-name>protected_zone</web-resource-name> <url-pattern>/protected/footie/*</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> <http-method>PUT</http-method> <http-method>DELETE</http-method> <http-method>HEAD</http-method> <http-method>OPTIONS</http-method> <http-method>TRACE</http-method> </web-resource-collection> <auth-constraint> <role-name>footie</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>NONE</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name>pure genius football club</realm-name> </login-config> <security-role> <description>the footie guys</description> <role-name>footie</role-name> </security-role> </web-app> */