jform sso的实现原理及其应用

JForum是一个功能强大的开放源代码的BBS论坛。我们在
JForum -基于BSD授权的免费论坛
JForum安装详解
等文章中已经对JForum的概要,安装步骤等作了比较详细的介绍。

本文介绍 JForum与已有的web应用的整合,以及JForum单点登陆原理与配置。

JForum可以单独作为一个BBS论坛运行,但是很多情况,我们需要在我们已有的WEB应用中集成JForum。
JForum的与已有WEB应用的集成有很多种方法。本文介绍JForum官方推荐的通过自定义SSO类实现来完成JForum与已有WEB应用的集成。


目标:

实现SSO (Single Sign On) :单点登陆。
一般来说,两个不同系统的整合的难点部分,便是需要解决单点登陆问题。
对于我们已有的WEB应用中的用户,若该用户已经登陆,并通过 联结迁移到JForum页面时,JForum要能够识别该用户已经登陆(不需要二次登陆)才不会让用户感到别扭(对用户来说,就好像使用的是同一个系统似的)。
但由于分属于2个不同的系统,所以它们之间不能共用同一套Session,这就需要使用一种特殊的机制来实现它们之间的互相通信。

好在,JForum为我们考虑到了这一点,它提供了灵活的SSO接口与配置机制,我们只需要简单地实现一个SSO类,同时在JForum的配置文件中加以配置即可。


JForum SSO机制的原理

- 当一个用户访问JForum时,JForum便会检查是否配置SSO,如果配置了SSO,JForum便会调用authenticateUser()方法。该方法简单地返回username或null。
- 若返回了一个不为空的username时,JForum将会检查是否匹配JForum数据库的userid。
- 若没有匹配的userid,JForum将动态加以创建
- JForum设置该user为登陆状态
- 若返回了一个null,则设置为“Anonymous”
- 若一个“Anonymous”用户试图访问权限以外的页面,JForum将根据SSO的设置导航到登陆页面,同时传递给一个登陆成功后应该迁移到的地址参数给login页面。


JForum SSO的实现

上面,我们大致了解了一下JForum的SSO机制的原理。根据SSO机制的原理,我们已经知道该怎么实现SSO。
1,需要实现一个SSO类,该类需要取得从另外一个系统登陆进来的用户信息。
一种最简单的方法是使用Cookie来实现。让已有的WEB应用在用户登陆时写入Cookie信息,然后JForum的SSO实现类中将该Cookie取出即可。
另外,还可以使用web-service,文件的写入等等方式来实现。反正一个最基本的原则是:用户登陆时写入用户信息,然后在JForum一方能取出相同信息即可。
2,配置JForum。
下面,我们以使用Cookie的方式为例来实现JForum SSO:
1,实现SSO类:
CookieUserSSO.java

view plaincopy to clipboardprint?
package net.jforum.sso;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import net.jforum.ActionServletRequest;
import net.jforum.ControllerUtils;
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(ActionServletRequest 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, HttpServletRequest 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.
}
}

package net.jforum.sso;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import net.jforum.ActionServletRequest;
import net.jforum.ControllerUtils;
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(ActionServletRequest 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, HttpServletRequest 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.
}
}

JForum本身也提供了一个RemoteUserSSO.java实现,由于该实现的authenticateUser方法只是简单地返回request.getRemoteUser(),不能显示已登陆的用户名。

2,修改SystemGlobals.properties
有些JForum版本为jforum-custom.conf文件。
查找“SSO”字样,找到“SSO / User authentication”配置部分,将其修改为以下内容:

authentication.type = sso
##...
sso.implementation = net.jforum.sso.CookieUserSSO
##...
sso.redirect=http://yourdomain/yourLogin.page?url=


并根据实际情况,修改
cookie.name.user
设置

3,修改你的web应用用户登陆/注销部分的代码逻辑部分:

登陆部分加入以下内容:

view plaincopy to clipboardprint?
public void doLogin() {
...
Cookie cookie = new Cookie(jforumSSOCookieNameUser, loginUser.getUsername());
cookie.setMaxAge(-1);
response.addCookie(cookie);
...
}

public void doLogin() {
...
Cookie cookie = new Cookie(jforumSSOCookieNameUser, loginUser.getUsername());
cookie.setMaxAge(-1);
response.addCookie(cookie);
...
}


注销部分加以以下内容:

view plaincopy to clipboardprint?
public void doLogin() {
...
Cookie cookie = new Cookie(jforumSSOCookieNameUser, "");
cookie.setMaxAge(0); // delete the cookie.
response.addCookie(cookie);
...
}

public void doLogin() {
...
Cookie cookie = new Cookie(jforumSSOCookieNameUser, "");
cookie.setMaxAge(0); // delete the cookie.
response.addCookie(cookie);
...
}


jforumSSOCookieNameUser为cookie.name.user的设置内容。

4,在你的HTML/JSP里加上

view plaincopy to clipboardprint?
<a href="/jforum">论坛</a>

<a href="/jforum">论坛</a>

联结。

至此,基本配置完成。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值