• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I hope someone replies to this one

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I have the below java file. Looks up user credentials validates them with backend db table and if successful returns a token

Can someone please show me how i put this token in a soap header so that client web service can use it to make calls to the db to make sure the toekn is valid and so that the user doesn't have to keep logging n

Am i going about this the right way

My code:



public class LoginHandler extends HttpServlet
{
String sqlResults = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement ps = null;
ResultSetMetaData rsmd = null;

public String validateUser (String userName, String password)
{
// pass userName and password values back to db to see if they exist
// connect to ConnectionFactory
String sql = "SELECT * FROM tblUsers WHERE userName = '"+userName+"' AND password = '"+password+"'";

try
{
conn = ConnectionFactory.getConection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
ps = conn.prepareStatement(sql);

// iterate through recordset and get data
if (rs.next())
{
sqlResults = rs.getString("userName");
}
}
catch (Exception e)
{
System.out.println("unsuccessful");
e.printStackTrace();
}
finally
{
ConnectionFactory.closeStatement(stmt);
ConnectionFactory.closeConnection(conn);
}

return sqlResults;
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// get userName and password entered by user
String userName = request.getParameter("userName");
String password = request.getParameter("password");

// get application Id from calling web service
String applicationId = request.getParameter("applicationId");

// call validateUser method to validate userName and password
String validateUserName = validateUser(userName, password);

if (validateUserName == null)
{
// redirect to login page
// either typo error or not registered
}
else
{
//HttpSession session = request.getSession(true);

//String sessionId = session.getId();
//session.setAttribute("sessionId", sessionId);

// get a unique user id
String uniqueUserId = WebServiceToken.getUniqueId();

// set expiry date 7 days from now
Calendar expiryDate = Calendar.getInstance();
expiryDate.roll(Calendar.DATE, 7);

applicationId = "wsAppTest";

// insert token details
WebServiceToken.setTokenDetails(userName, uniqueUserId, expiryDate, applicationId);

// return a token and use in the calling web service
String token = WebServiceToken.getToken(uniqueUserId, applicationId);

// now put this token in the SOAP header, so calling web service can use it
}
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic