欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
java單點(diǎn)登錄的實(shí)現

最近公司要做一個(gè)單點(diǎn)登陸的應用,整合論壇和網(wǎng)站。到網(wǎng)上找了好些資料,終于圓滿(mǎn)解決,博客中記錄一下一面忘掉。

單點(diǎn)登錄首先首先要保持數據庫數據的一致性,這個(gè)實(shí)現方式很多我就不多廢話(huà)了。

剛開(kāi)始我的想法是在一個(gè)應用中登陸的時(shí)候發(fā)送一個(gè)http請求到另一個(gè)應用保證兩個(gè)應用同時(shí)擁有session,后來(lái)考慮到session過(guò)期不同步的問(wèn)題只能放棄。為了保持session的完全同步我只能求助于cookie,首先說(shuō)一下思路:
1.為了兩個(gè)應用個(gè)應用能同時(shí)訪(fǎng)問(wèn)到cookie,cookie設置的時(shí)候要設置path在根目錄(同一個(gè)服務(wù)器下的不
同應用,不在同一個(gè)應用下可以用共用域名比如說(shuō) .baidu.com),必須保證cookie在統一一個(gè)域下
2.在訪(fǎng)問(wèn)一個(gè)應用的時(shí)候首先檢測session如果存在就是已經(jīng)登錄保存cookie(保證cookie是最新的)
3.如果session不存在檢測cookie如果cooki不存在說(shuō)明在另一應用中也沒(méi)有登錄
4.cookie存在檢測創(chuàng )建時(shí)間(當然保存的時(shí)候要設置一個(gè)時(shí)間)如果沒(méi)有過(guò)期就用cookie中保存的用戶(hù)名密碼調用一下登錄方法
5.登陸成功保存cookie

為了保證每次請求都能檢測我使用了filter下面貼一下filter代碼
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginFilter implements Filter {

private String loginInfoPath;
public void destroy() {
   loginInfoPath = null;
}

public void doFilter(ServletRequest sreq, ServletResponse sresp,
    FilterChain chain) throws IOException, ServletException {
   HttpServletRequest req = (HttpServletRequest) sreq;
   HttpServletResponse resp = (HttpServletResponse) sresp;
   Class loginInfoClass = null;
   LoginInfoBase loginInfo = null;
   try {
    loginInfoClass = Class.forName(loginInfoPath);
    loginInfo = (LoginInfoBase) loginInfoClass.newInstance();
   } catch (ClassNotFoundException e) {
    e.printStackTrace();
   } catch (InstantiationException e) {
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    e.printStackTrace();
   }
   loginInfo.setReq(req);
   loginInfo.setResp(resp);
   //session值為空
   if(loginInfo.isSessionEmpty()){
    if(loginInfo.init()){
     boolean loginResult = loginInfo.doLogin();
     if(loginResult){
      loginInfo.saveCookie();
     }
    }
   }else{
    if(loginInfo.init()){
     //另外一套系統已經(jīng)退出
     if("out".equals(loginInfo.getLoginState())){
      loginInfo.doLogout();
      CookieUtil.delCookie(resp, LoginInfoBase.COOKIE_NAME);
     }
    }
   }
   chain.doFilter(sreq, sresp);
}

public void init(FilterConfig config) throws ServletException {
   this.loginInfoPath = config.getInitParameter("loginInfoPath");
}

public String getLoginInfoPath() {
   return loginInfoPath;
}

public void setLoginInfoPath(String loginInfoPath) {
   this.loginInfoPath = loginInfoPath;
}

}
loginInfoPath 在web.xml中配置指明LoginInfoBase的子類(lèi)的完整路徑用來(lái)反射

LoginInfoBase代碼:
package com.yt.util;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class LoginInfoBase {
public static final String SEPARATION = "-->";
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final long COOKIE_TIME = 1800000;
public static final String COOKIE_NAME = "loginInfo";
public static final String COOKIE_PATH = "/";

protected HttpServletRequest req;
protected HttpServletResponse resp;

protected Date sessionTime;
protected String userName;
protected String userPass;
protected String loginState;

public LoginInfoBase() {
   super();
}

public LoginInfoBase(HttpServletRequest req, HttpServletResponse resp) {
   super();
   this.req = req;
   this.resp = resp;
}

public LoginInfoBase(String userName, String pwd, Date sessionTime) {
   this.userName = userName;
   this.userPass = pwd;
   this.sessionTime = sessionTime;
}

abstract void doLogout();
abstract boolean isSessionEmpty();
abstract boolean doLogin();

public HttpServletRequest getReq() {
   return req;
}
public void setReq(HttpServletRequest req) {
   this.req = req;
}
public HttpServletResponse getResp() {
   return resp;
}
public void setResp(HttpServletResponse resp) {
   this.resp = resp;
}

//初始化登錄對象
protected boolean init(){
   String loginInfoStr = CookieUtil.getValue(req, LoginInfoBase.COOKIE_NAME);
   if(loginInfoStr == null || "".equals(loginInfoStr)){
    return false;
   }
   String[] infoArray = loginInfoStr.split(SEPARATION);
   if(infoArray.length>3){
    this.userName = DigestUtil.getFromBASE64(infoArray[0]);
    this.userPass = DigestUtil.getFromBASE64(infoArray[1]);
    this.sessionTime = buildDate(infoArray[2]);
    this.loginState = infoArray[3];
   }
   if(new Date().getTime() - getSessionTime().getTime() > LoginInfo.COOKIE_TIME){
    CookieUtil.delCookie(resp, LoginInfo.COOKIE_NAME);
    return false;
   }
   return true;
}

protected void saveCookie(){
   setSessionTime(new Date());
   setLoginState("in");
   CookieUtil.addCookie(resp, LoginInfo.COOKIE_NAME, toString(), "/");
}

public void clearCookie(){
   setUserName("XX");
   setUserPass("XX");
   setSessionTime(new Date());
   setLoginState("out");
   CookieUtil.addCookie(resp, LoginInfo.COOKIE_NAME, toString(), "/");
}

@Override
public String toString() {
   return DigestUtil.getBASE64(userName)+SEPARATION+DigestUtil.getBASE64(userPass)+SEPARATION+formateSessionTime()+SEPARATION+loginState;
}

private String formateSessionTime(){
   SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
   String timeStr =df.format(sessionTime);
   return timeStr;
}
private Date buildDate(String sessionTime) {
   SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
   Date date = null;
     try {
         date = df.parse(sessionTime);
     }catch (Exception ex){
         System.out.println(ex.getMessage());
     }
   return date;
}

public Date getSessionTime() {
   return sessionTime;
}

public void setSessionTime(Date sessionTime) {
   this.sessionTime = sessionTime;
}

public String getUserName() {
   return userName;
}

public void setUserName(String userName) {
   this.userName = userName;
}

public String getUserPass() {
   return userPass;
}

public void setUserPass(String userPass) {
   this.userPass = userPass;
}

public String getLoginState() {
   return loginState;
}

public void setLoginState(String loginState) {
   this.loginState = loginState;
}

}
doLogin(),doLogout(),isSessionEmpty()必須在子類(lèi)中實(shí)現實(shí)現
分別用來(lái)登錄系統,注銷(xiāo)系統,判斷是否登錄

ok,比較亂不懂的可以提問(wèn)

有人要求Util的代碼,無(wú)語(yǔ),貼出來(lái)吧

package net.heart.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class CookieUtil {

 public static void addCookie(HttpServletResponse response,String name,String value,int maxAge){
  try {
   Cookie cookie=new Cookie(name,URLEncoder.encode(value, "utf-8"));
   cookie.setMaxAge(maxAge);
   response.addCookie(cookie);
   
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public static String getValue(HttpServletRequest request,String name){
  String value=null;
  Cookie[] cookies=request.getCookies();
  if(cookies!=null){
   for(Cookie cookie:cookies){
    if(name.equals(cookie.getName())){
     try {
      value=URLDecoder.decode(cookie.getValue(),"utf-8");
     } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     break;
    }
   }
  }
  return value;
 }
 public static void delCookie(HttpServletResponse response,String name){
  Cookie cookie=new Cookie(name,"");
  cookie.setMaxAge(0);
  response.addCookie(cookie);
 }
}

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
jsp+servlet實(shí)現注冊登錄,不用數據庫
JavaWeb 開(kāi)發(fā) 06 —— smbms項目實(shí)踐
《Java Web應用程序開(kāi)發(fā)》08 JSP(三)
在spring mvc框架中顯示xml視圖
Servlet基本用法(一)基本配置
Servlet學(xué)習筆記(六)-----Servlet之間的通信
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久