1、设置原来Web应用的一个子域(JBoss、Tomcat貌似一样,修改server.xml)
<Context
docBase="/home/guo/Java/apache-tomcat-6.0.33/webapps/jforum"
path="/portal/jforum" ## /portal门户主页,/portal/jforum 论坛主页
reloadable="true"
source="org.eclipse.jst.jee.server:jforum"/>
2、在登录页面上写Cookie(一般都在login.js中修改)
pathname = location.pathname;
myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
var largeExpDate = new Date ();
largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));
try{
SetCookie2('jforumSSOCookieAWSSid',document.frmLogin.userid.value,1,'/');
if(document.frmLogin.rememberMeUid.checked==true){
SetCookie('username',document.frmLogin.userid.value,largeExpDate,myDomain);
}else{
SetCookie('username',null,null);
}
if(document.frmLogin.rememberMePwd.checked==true){
SetCookie('userpassword',document.frmLogin.pwd.value,largeExpDate,myDomain);
}else{
SetCookie('userpassword',null,null);
}
}catch(e){alert(e);}
2+1、在退出页面清理Cookie(在top.js中修改)
if(commandType=='Exit'){
resetProcessBar();
if ( false==confirm(确认离开系统吗 ) ){
return false;
}
SetCookie2('jforumSSOCookieAWSSid','',1,'/');
frmTop.cmd.value="Logout";
frmTop.target="_parent";
frmTop.submit();
}
2+2、SetCookie2()方法的实现(effect.js中)
function SetCookie2( cookieName, cookieValue, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );
/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = cookieName + "=" +escape( cookieValue ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure=" + secure : "" );
}
3、JForum的单点登录接口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) {
// login cookie set by my web LOGIN application
Cookie cookieNameUser = ControllerUtils.getCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_USER));
String username = null;
if (cookieNameUser != null) {
username = cookieNameUser.getValue();
}
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
}
System.out.println("remoteUser="+remoteUser);
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.
}
}
4、JForum全局配置文件SystemGlobals.properties(/jforum/WebContent/WEB-INF/config/SystemGlobals.properties)修改
authentication.type = sso
login.authenticator = net.jforum.sso.DefaultLoginAuthenticator
auto.login.enabled = true
sso.implementation = net.jforum.sso.CookieUserSSO ## 自己的实现类
cookie.name.user = jforumSSOCookieAWSSid ## 使用这个关键字获取用户名
5、JForum客户定义配置文件jforum-custom.conf(/jforum/WebContent/WEB-INF/config/jforum-custom.conf)修改
forum.link=http\://172.16.4.176\:8088/portal/jforum
homepage.link=http\://172.16.4.176\:8088/portal