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 阅读(1912)
评论(0) 编辑 收藏