jforum是一個(gè)不錯(cuò)的開源BBS論壇,支持中文,操作方便,容易擴(kuò)展,是一個(gè)不錯(cuò)的選擇。通過參考網(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的生命周期為:會(huì)話級(jí),即瀏覽器關(guān)閉,該cookie就消失了
response.addCookie(cookie);
注銷:
Cookie cookie = new Cookie(jforumSSOCookieNameUser, "");
cookie.setMaxAge(0); // delete the cookie.
response.addCookie(cookie);
4、在html/jsp頁(yè)面加入超鏈接:
<a href="/jforum">轉(zhuǎn)到論壇</a>
這就配置完成了。
posted on 2008-11-18 11:39
jiafang83 閱讀(1918)
評(píng)論(0) 編輯 收藏