`
李瑞曦
  • 浏览: 44188 次
  • 性别: Icon_minigender_2
  • 来自: 大荔
社区版块
存档分类
最新评论

session 应用于登录

 
阅读更多
写道


/**

*用户登录注入信息

*

/

// 往 Session 中注入 userID 信息
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("userInfo.userID", userInfo.getUserID());
session.put("userInfo.username", userInfo.getUsername());
session.put("username", userInfo.getUsername());
// 更新登录 IP 地址和登录时间
userInfo.setLoginIP(getIpAddr());
userInfo.setLastLoginTime(new Date(System.currentTimeMillis()));
userInfoService.updateLoginIpAndTime(userInfo);

// 获取客户端真实的 IP 地址
private String getIpAddr() {
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
String clientIp = request.getHeader("x-forwarded-for");
if(clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
clientIp = request.getHeader("Proxy-Client-IP");
}
if(clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
clientIp = request.getHeader("WL-Proxy-Client-IP");
}
if(clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
clientIp = request.getRemoteAddr();
}

return clientIp;
}





/**

*获取用户登录信息

*

/

// 从 Session 中获得用户 ID
private Long getUserIDfromSession() {
Map<?,?> session = ActionContext.getContext().getSession();
return (Long) session.get("userInfo.userID");
}

// public String insertShop(){

Long userid = this.getUserIDfromSession();
shop.setUserID(userid);
shop.setCreateTime(new java.util.Date());
Long id = shopService.insertShop(shop);
if (id!=null) {
return SUCCESS;
}else{
this.addFieldError("shop.errortext", "添加失败");
return ERROR;
}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics