Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

getUserPrincipal query in jsp

Posted on 2010-09-14
8
Medium Priority
?
1,909 Views
Last Modified: 2012-05-10
Hello
I'm trying to implement single sign on jsp where in the jsp should retrive the details of the user(username & password) logged into the machine.I'm using the jsp code below
the web.xml has security roles configured already


-------------------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

<%@page import="javax.security.auth.callback.*,javax.security.auth.*,java.util.*,java.security.*,java.io.*,java.net.*,java.text.*,javax.servlet.http.*"%>

<%

Principal userPrincipal = request.getUserPrincipal();
String username=weblogic.security.SubjectUtils.getUsername(weblogic.security.Security.getCurrentSubject());
out.println("Logged in user >>>>> " + " " + username +"<br>");
CallbackHandler handler = new weblogic.security.SimpleCallbackHandler("xyz","uuu");
Subject mySubject = weblogic.security.services.Authentication.login(handler);
weblogic.servlet.security.ServletAuthentication.runAs(mySubject, request);
out.println("Principal " + " " + request.getUserPrincipal()+"<br>");

%>

but when I run this jsp , the principal is displayed as null for the first time and when I refresh the page it comes back with the user principal..can I know how it can retrive it in first place when I access this page..
I would also want to know how I can replace the hardcoded username & password values with something dynamic so it can use them
CallbackHandler handler = new weblogic.security.SimpleCallbackHandler("xyz","uuu");

thanks
0
Comment
Question by:kalyandm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 33674386
request.getUserPrincipal will always work.  Your problem is probably with the weblogic.security.SubjectUtils.getUsername call.

If you use this line, you'll get the username:

String username = userPrincipal.getName();

0
 

Author Comment

by:kalyandm
ID: 33693291
Hello
I tried userPrincipal.getName() but the very first time it returns null and subsequently returns the principal correctly.since this is the first jsp page that is accessed I would want to be able to get the username first time itself..please suggest
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 33694313
If the person is not logged in, then there won't be a username.  There's nothing you can do about that.

The username can only be found once the user logs in.  Where were you thinking you would get the username if the person is not logged in?  Were you thinking it would be in the cookie or in a parameter?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kalyandm
ID: 33699243
The user is logged in via the machine and within weblogic the security constraints are configured and a user group created , the sso configuration using kerberos with active directoy is setup.So when user logs into the machine to access the jsp the username should be available I thought ..is it not the case?

thanks
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 33699324
It is not the case.  The servlet container has to know about the login.  Don't users have to log in to see your page?  

If users do not have to login, then you must have an action which automatically logs them in from the servlet container perspective.  Your calls to weblogic.security.services.Authentication.login and weblogic.servlet.security.ServletAuthentication.runAs are supposed to do that for you.  Do you see the user logged in after the runAs?
0
 

Author Comment

by:kalyandm
ID: 33699352
the users don't have a login page..The idea is that the sso principle where the user logs into the machine should be used to go into the application.

regarding your second question..

yes I do but for the first time it comes back as null and after i refresh the page it comes back with the correct user.I can assure you that the authentication part is fine becoz if I put any user that doesn't exist in the system it comes back with an error which is how it should be.It should take the user I supplied in weblogic.security.services.Authentication.login(handler);
 and then validate it agaianst weblogic configured group..this aprt is working fine ..my doubt is how do I get the username/password that I need to pass onto
weblogic.servlet.security.ServletAuthentication.runAs if that is possible

thanks
0
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 750 total points
ID: 33711724
In your example above, your user is not logged in until you have the hardcoded username and password.  You have to collect the username and password from the user to use this scheme.  It is unlikely that you can get the password from the weblogic environment anyway - the code you're using is for the case where you have decided to write your own login page, and you are handing the username and password to the weblogic security system.

So no, you can't collect the username and password without asking the user.  Which means making a login form and having them fill it in.

If you haven't already read it, the book Weblogic: The Definitive Guide is helpful on this topic:
http://flylib.com/books.php?ln=en&n=2&p=107&c=26&p1=1&c1=1&c2=187&view=1


0
 

Author Closing Comment

by:kalyandm
ID: 33830747
it partly answered my query
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An overview of cyber security, cyber crime, and personal protection against hackers. Includes a brief summary of the Equifax breach and why everyone should be aware of it. Other subjects include: how cyber security has failed to advance with technol…
With so many activities to perform, Exchange administrators are always busy in organizations. If everything, including Exchange Servers, Outlook clients, and Office 365 accounts work without any issues, they can sit and relax. But unfortunately, it…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question