Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Cookie is not getting generated - please help me  RSS feed

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a struts2 webapplication which i am integrating with another forum webapplication.When the user logs into the Struts2 application, i want to maintani his session in forum also and forum should not ask him to login again.For thsi i am using a cookie which is not at all getting generated.
I changed the code in Jsp and installed the Web developer toolbar. The cookie is not at all coming when i enter Jforum application from Main application. I have 2 separate wars. I am highly puzzled what else i have to do? Please find the jsp,Util class files.
I am not doing cookie.setDomain(domain); is it causing the issue?


This is my main application URL from where i will move to Jforum after clicking a link in this jsp


This is my Jforum URL which i get when the user moves to Jforum from main application


I have the following code in jsp where cookie is formed and then the user clicks on the hiper link. Is this not the way???

<authz:authorize ifAnyGranted="ROLE_ADMIN,ROLE_EMPLOYER,ROLE_EMPLOYEE">
User userObj = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = userObj.getEmail();
String password = userObj.getPassword();
String encrypted = CookieUtil.makeSSOCookieValue(username, password);
Cookie cookie = new Cookie("sso-auto-login", encrypted);
cookie.setMaxAge(120000); // 10 hours in seconds (can be -1 for session).
//cookie.setDomain(domain); // Full qualified server domain.
<a href="http://localhost:8080/jforum/" cssClass="thf_line">COOKIE FORUMS</a>
</authz:authorize> </div>


package com.mob.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class CookieUtil {
static SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyyMMddHHmm");
static final String md5Salt = "someWeirdStringSharedByTheApps";
* Makes the MD5 token used for authentication in cookies.
* @param userName The user id used for encryption.
* @param email The user's email (or "" if none).
* @param groupInfo The category of groups the user is to be allowed
* access to.
* @param timestamp A yyyyddMMhhmm representation of when the cookie
* was created.
* @return The hex string representation of the MD5 digested string.
* @exception NoSuchAlgorithException If for some reason, MD5 is
* not available?
protected static String makeMD5Token (String userName, String password,
String timestamp )
throws NoSuchAlgorithmException {
MessageDigest md;
byte[] sig;
String plainText;
StringBuffer encryptedText = new StringBuffer();
plainText = md5Salt + "/" + userName + "/"+ password +
"/" + timestamp;
md = MessageDigest.getInstance("MD5");
sig = md.digest(plainText.getBytes());
for ( int i = 0; i < sig.length; i++ ) {
encryptedText.append(Integer.toString(( sig[i] & 0xff ) +
0x100, 16 ).substring(1));
return encryptedText.toString();

public static String makeSSOCookieValue(String userName, String password)
throws NoSuchAlgorithmException
String timestamp = dateFormatter.format(new Date());
return userName + "/" + password + "/" +
makeMD5Token(userName, password, timestamp );


  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!