posts - 55,comments - 89,trackbacks - 0
             jforum是一個不錯的開源BBS論壇,支持中文,操作方便,容易擴(kuò)展,是一個不錯的選擇。通過參考網(wǎng)上的資料,下面給出了jforum與web項(xiàng)目整合的方法:
          1、實(shí)現(xiàn)SSO類:

          package net.jforum.sso;

          import javax.servlet.http.Cookie;

          import net.jforum.ControllerUtils;
          import net.jforum.context.RequestContext;
          import net.jforum.entities.UserSession;
          import net.jforum.util.preferences.ConfigKeys;
          import net.jforum.util.preferences.SystemGlobals;
          import org.apache.log4j.Logger;
           
            public class CookieUserSSO implements SSO {     
              
              static final Logger logger = Logger.getLogger(CookieUserSSO.class.getName());     
              
              public String authenticateUser(RequestContext request) { 
                   // myapp login cookie, contain logged username     
                  Cookie myCookie = ControllerUtils.getCookie("jforumSSOCookieNameUser");     
                       
                  String username = null;          
                  if (myCookie != null) username = myCookie.getValue();      
                  System.out.println("cookie_name1="+myCookie.getName());   
                  System.out.println("cookie value1="+myCookie.getValue());  
                       
                  if (myCookie == null || username.trim().equals("")) {     
                      //JForumExecutionContext.setRedirect(SystemGlobals.getValue(ConfigKeys.SSO_REDIRECT));     
                      return null; // no cookie found     
                  }      
                  System.out.println("cookie_name2="+myCookie.getName());  
                  System.out.println("cookie value2="+myCookie.getValue());  
                  return username; // jforum username     
              }     
              
              public boolean isSessionValid(UserSession userSession, RequestContext request) {   
            System.out.println("執(zhí)行isSessionValid方法");
                  Cookie SSOCookie = ControllerUtils.getCookie("jforumSSOCookieNameUser"); // myapp login cookie      
                                 
                  String remoteUser = null;     
                       
                  if (SSOCookie != null) remoteUser = SSOCookie.getValue(); //  jforum username     
              
                  // user has since logged out     
                  if(remoteUser == null &&      
                          userSession.getUserId() != SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {     
                      return false;     
                  // user has since logged in     
                  } else if(remoteUser != null &&      
                          userSession.getUserId() == SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {     
                      return false;     
                  // user has changed user     
                  } else if(remoteUser != null && !remoteUser.equals(userSession.getUsername())) {     
                      return false;     
                  }     
                  return true; // myapp user and forum user the same     
              }     
          }    

          把該類放在jforum\WEB-INF\classes下,然后用javac  -d  .  CookieUserSSO .java 命令編譯,.class文件存放在jforum\WEB-INF\classes\net\jforum\sso下。
          2、修改SystemGlobals.properties
             有些JForum版本為jforum-custom.conf文件。
             查找“SSO”字樣,找到“SSO / User authentication”配置部分,將其修改為以下內(nèi)容:
          authentication.type = sso-----------特別注意:sso用小寫,不能用大寫
          ##...
          sso.implementation = net.jforum.sso.CookieUserSSO----------你自己實(shí)現(xiàn)的SSO類
          ##...
          sso.redirect=http://localhost:port/jforum---------------例如:sso.redirect=http://localhost:8082/jforum
          3、在程序的登錄或注銷部分加入如下代碼:
               登錄:
          Cookie cookie = new Cookie("jforumSSOCookieNameUser",name);-------name為從登錄界面取得的用戶名,把它加入到cookie里面
            cookie.setPath("/");
            cookie.setMaxAge(-1);//設(shè)置cookie的生命周期為:會話級,即瀏覽器關(guān)閉,該cookie就消失了
            response.addCookie(cookie);

              注銷:

            Cookie cookie =  new    Cookie(jforumSSOCookieNameUser, "");
            cookie.setMaxAge(0); // delete the cookie.
            response.addCookie(cookie);

          4、在html/jsp頁面加入超鏈接:
          <a href="/jforum">轉(zhuǎn)到論壇</a>

          這就配置完成了。


          posted on 2008-11-18 11:39 jiafang83 閱讀(1919) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 开封市| 横山县| 弥渡县| 鄂托克旗| 榆林市| 安阳县| 清镇市| 小金县| 镇江市| 姚安县| 浦城县| 宜州市| 宣汉县| 湖口县| 桐城市| 耿马| 建水县| 杭锦后旗| 南部县| 隆化县| 当雄县| 海阳市| 金昌市| 定州市| 宜章县| 图片| 崇仁县| 东光县| 通州市| 济南市| 龙口市| 金乡县| 新蔡县| 通山县| 巫山县| 涟水县| 商都县| 邢台县| 山丹县| 纳雍县| 栖霞市|