TB源码分析:LoginManager

类别:Java 点击:0 评论:0 推荐:
package com.terac.board;

import com.terac.util.CookieUtil;
import com.terac.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class LoginManager {

public static void login(HttpServletResponse response, int id, String userName, int cookieMaxAge) {
CookieUtil.setCookie(response, "userId", String.valueOf(id), cookieMaxAge);
try {
CookieUtil.setCookie(response, "userName", URLEncoder.encode(userName, "UTF-8"), cookieMaxAge);
} catch (UnsupportedEncodingException e) {
//
}
}

public static boolean isLogined(HttpServletRequest request) {
boolean result = false;
if (getUserId(request) > 0) {
result = true;
}
return result;
}

public static int getUserId(HttpServletRequest request) {
int userId = StringUtil.parseInt(CookieUtil.getString(request, "userId"));
return userId;
}

public static String getUserName(HttpServletRequest request) {
String userName = null;
try {
userName = URLDecoder.decode(CookieUtil.getString(request, "userName"), "UTF-8");
} catch (UnsupportedEncodingException e) {
//
}
return userName;
}

public static void logout(HttpServletResponse response) {
CookieUtil.setCookie(response, "userId", "", -1);
CookieUtil.setCookie(response, "userName", "", -1);
}


}

本文地址:http://com.8s8s.com/it/it14710.htm