把jforum-2.1.8版本的论坛系统整合到自己的项目中。主要的步骤有这么几步
1.去http://jforum.net/releases/jforum-2.1.8-src.zip下载jforum源码。这个下下来后需要把它变成一个WEB项目。其实这个很简单自己建个WEB项目然后参考那个WEB的一些文件信息在把jforum做相应修改就可以成功的把它import进myeclipse里。项目结构如图所示
WebRoot目录下的结构
2.在net.jforum.sso新建一个CookieUserSSO类,这个类作为单点登录的连接类,代码如下,如果是第一次整合完全可以整个COPY去。
import java.io.UnsupportedEncodingException;
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());
String userName;
String userID;
String email;
public final String COOKIE_NAME = "jforumUserId";
public String authenticateUser(RequestContext request) {
// login cookie set by my web LOGIN application
// Cookie cookieNameUser = ControllerUtils.getCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_USER));//这种写法会获取null,不解啊
Cookie cookieNameUser = ControllerUtils.getCookie("jforumSSOCookieNameUser");
String username = null;
if (cookieNameUser != null) {
try {
username = java.net.URLDecoder.decode(cookieNameUser.getValue(),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(cookieNameUser+" ======== "+username+" ==========");
return username; // return username for jforum
// jforum will use this name to regist database or set in HttpSession
}
public boolean isSessionValid(UserSession userSession,
RequestContext request) {
Cookie cookieNameUser = ControllerUtils.getCookie(SystemGlobals
.getValue(ConfigKeys.COOKIE_NAME_USER)); // user cookie
String remoteUser = null;
if (cookieNameUser != null) {
remoteUser = cookieNameUser.getValue(); // jforum username
}
if (remoteUser == null
&& userSession.getUserId() != SystemGlobals
.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {
// user has since logged out
return false;
} else if (remoteUser != null
&& userSession.getUserId() == SystemGlobals
.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {
// anonymous user has logged in
return false;
} else if (remoteUser != null
&& !remoteUser.equals(userSession.getUsername())) {
// not the same user (cookie and session)
return false;
}
return true; // myapp user and forum user the same. valid user.
}
}
3.修改WEB-INF/config目录下的SystemGlobals.properties文件找到SSO需要修改这几个地方
#authentication.type = default
authentication.type = sso
#sso.implementation = net.jforum.sso.RemoteUserSSO
sso.implementation = net.jforum.sso.CookieUserSSO
sso.redirect = http://domain:port/webappname/url
好了修改好这几个地方jforum这边的工作基本就算可以了。
4.回到自己的项目中。在登录的方法里加上这些代码
Cookie cookie = new Cookie("jforumSSOCookieNameUser", username);
cookie.setMaxAge(-1);
cookie.setPath("/");
response.addCookie(cookie);
5.给个链接 <a href="/JForum">论坛</a> 。
6.部署项目,启动TOMCAT。搞定。
7.可能在中文用户名的情况下会出现乱码可以参考我写的另一篇博客http://bei-jin-520.iteye.com/admin/blogs/642970