the project generates a number immediately after the user has successfully logged in. "token" is added to cookie as the key. Running in eclipse and using 360, Google browser can see this cookie value. However, when deployed to a remote tomcat server, you will not see the cookie value if you visit it again.
@ RequestMapping (value= "/ user/login", method=RequestMethod.POST)
@ResponseBody
public E3Result login(String username, String password,
HttpServletRequest request, HttpServletResponse response) {
E3Result e3Result = loginService.userLogin(username, password);
//
if(e3Result.getStatus() == 200) {
String token = e3Result.getData().toString();
//tokencookie
CookieUtils.setCookie(request, response, "token", token);
}
//
return e3Result;
}
public E3Result userLogin (String username, String password) {
// 1
//
TbUserExample example = new TbUserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo(username);
//
List<TbUser> list = userMapper.selectByExample(example);
if (list == null || list.size() == 0) {
//
return E3Result.build(400, "");
}
//
TbUser user = list.get(0);
//
if (!DigestUtils.md5DigestAsHex(password.getBytes()).equals(user.getPassword())) {
// 2
return E3Result.build(400, "");
}
// 3token
String token = UUID.randomUUID().toString();
// 4rediskey:token value:
user.setPassword(null);
jedisClient.set("SESSION:" + token, JsonUtils.objectToJson(user));
// 5Session
jedisClient.expire("SESSION:" + token, SESSION_EXPIRE);
// 6token
return E3Result.ok(token);
}
private static final void doSetCookie (HttpServletRequest request, HttpServletResponse response,
String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) {
try {
if (cookieValue == null) {
cookieValue = "";
} else if (isEncode) {
cookieValue = URLEncoder.encode(cookieValue, "utf-8");
}
Cookie cookie = new Cookie(cookieName, cookieValue);
if (cookieMaxage > 0)
cookie.setMaxAge(cookieMaxage);
if (null != request) {// cookie
String domainName = getDomainName(request);
System.out.println(domainName);
if (!"localhost".equals(domainName)) {
cookie.setDomain(domainName);
}
}
cookie.setPath("/");
response.addCookie(cookie);
} catch (Exception e) {
e.printStackTrace();
}
}