How to get login attributes from a servlet/jsp or in web service? 2013

View previous topic View next topic Go down

How to get login attributes from a servlet/jsp or in web service? 2013

Post by Admin on Sun Sep 15, 2013 4:04 am

2013 2013 2013 <div dir="ltr" style="text-align: left;" trbidi="on">There are scenarios where we may require the user name and password of the system from where the servlet request is coming.<br><br> <b>What is the need to get the login attributes?</b><br> 1) The very basic requirement to do that is to authenticate the user from where the request is coming. We may require to authenticate the user who is requesting for a servlet for security purposes.<br> 2) To check for access rights. Say for example, we may block some user from accessing the servlet due to some violation. We can do the same in a servlet if we have the user information.<br><br> I would like to explain how to get login user name/username or password from the servlet request.<br><br><b>The below code is useful in a servlet.</b><br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">String name = request.getUserPrincipal().getName();<br /><br />String name = request.getRemoteUser();</code><br /></pre> <b>The below code is useful when you are in a web service and wants the user info.</b><br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">@Resource<br />WebServiceContext ctx; // Use this as a class variable.<br /><br />SecurityContextHolderAwareRequestWrapper map = (SecurityContextHolderAwareRequestWrapper) ctx<br /> .getMessageContext().get(MessageContext.SERVLET_REQUEST);<br /><br />String name = map.getUserPrincipal().getName();<br /><br />String name = map.getRemoteUser();</code><br /></pre><b>The below code is useful when you are in a JSP</b><br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">${pageContext.request.userPrincipal.name}<br /><br />${pageContext.request.remoteUser}</code></pre></br></div><br> 2013 2013 2013 <br><div dir="ltr" style="text-align: left;" trbidi="on">There are scenarios where we may require the user name and password of the system from where the servlet request is coming.<br><br> <b>What is the need to get the login attributes?</b><br> 1) The very basic requirement to do that is to authenticate the user from where the request is coming. We may require to authenticate the user who is requesting for a servlet for security purposes.<br> 2) To check for access rights. Say for example, we may block some user from accessing the servlet due to some violation. We can do the same in a servlet if we have the user information.<br><br> I would like to explain how to get login user name/username or password from the servlet request.<br><br><b>The below code is useful in a servlet.</b><br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">String name = request.getUserPrincipal().getName();<br /><br />String name = request.getRemoteUser();</code><br /></pre> <b>The below code is useful when you are in a web service and wants the user info.</b><br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">@Resource<br />WebServiceContext ctx; // Use this as a class variable.<br /><br />SecurityContextHolderAwareRequestWrapper map = (SecurityContextHolderAwareRequestWrapper) ctx<br /> .getMessageContext().get(MessageContext.SERVLET_REQUEST);<br /><br />String name = map.getUserPrincipal().getName();<br /><br />String name = map.getRemoteUser();</code><br /></pre><b>The below code is useful when you are in a JSP</b><br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">${pageContext.request.userPrincipal.name}<br /><br />${pageContext.request.remoteUser}</code></pre></br></div><br>2013 2013 2013 <br> <a href="http://www.matrixar.com/" title="Matrix ">المصفوفة : أجمل الخلفيات والصور</a>

Admin
Admin

Posts : 64122
Join date : 2013-02-22

View user profile http://prowebsites1.forumaroc.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum