posts - 55,comments - 89,trackbacks - 0
             jforum是一個不錯的開源BBS論壇,支持中文,操作方便,容易擴展,是一個不錯的選擇。通過參考網上的資料,下面給出了jforum與web項目整合的方法:
          1、實現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("執行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”配置部分,將其修改為以下內容:
          authentication.type = sso-----------特別注意:sso用小寫,不能用大寫
          ##...
          sso.implementation = net.jforum.sso.CookieUserSSO----------你自己實現的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);//設置cookie的生命周期為:會話級,即瀏覽器關閉,該cookie就消失了
            response.addCookie(cookie);

              注銷:

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

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

          這就配置完成了。


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

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


          網站導航:
           
          主站蜘蛛池模板: 上思县| 汪清县| 乐清市| 县级市| 惠水县| 沁水县| 桃江县| 循化| 藁城市| 微山县| 交口县| 屏东市| 尼木县| 桑植县| 龙井市| 克拉玛依市| 贺州市| 塔河县| 刚察县| 天峨县| 延庆县| 舞阳县| 汽车| 耒阳市| 定西市| 阜新| 泉州市| 连州市| 万山特区| 洮南市| 峡江县| 彭山县| 延长县| 富源县| 陆良县| 勐海县| 北流市| 海盐县| 汉沽区| 黄平县| 夏邑县|