在jforum工程下创建一个CookieUserSSO,实现SSO接口:
1package net.jforum.sso;
2
3import javax.servlet.http.Cookie;
4
5import net.jforum.ControllerUtils;
6import net.jforum.context.RequestContext;
7import net.jforum.entities.UserSession;
8import net.jforum.util.preferences.ConfigKeys;
9import net.jforum.util.preferences.SystemGlobals;
10
11import org.apache.log4j.Logger;
12
13/** *//**
14 * jforum 与 web 项目整合的的处理类
15 */
16public class CookieUserSSO implements SSO {
17 static final Logger logger = Logger
18 .getLogger(CookieUserSSO.class.getName());
19
20 public String authenticateUser(RequestContext request) {
21 // login cookie set by my web LOGIN application
22 // Cookie cookieNameUser =
23 // ControllerUtils.getCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_USER));//这种写法会获取null,不解
24 Cookie cookieNameUser = ControllerUtils
25 .getCookie("jforumSSOCookieNameUser");
26 String username = null;
27
28 if (cookieNameUser != null) {
29 username = cookieNameUser.getValue();
30 }
31 System.out.println(cookieNameUser + " ======== " + username
32 + " ==========");
33 return username;
34 // return username for jforum
35 // jforum will use this name to regist database or set in HttpSession
36 }
37
38 public boolean isSessionValid(UserSession userSession,
39 RequestContext request) {
40 Cookie cookieNameUser = ControllerUtils.getCookie(SystemGlobals
41 .getValue(ConfigKeys.COOKIE_NAME_USER)); // user cookie
42 String remoteUser = null;
43
44 if (cookieNameUser != null) {
45 remoteUser = cookieNameUser.getValue(); // jforum username
46 }
47
48 if (remoteUser == null
49 && userSession.getUserId() != SystemGlobals
50 .getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {
51 // user has since logged out
52 return false;
53 } else if (remoteUser != null
54 && userSession.getUserId() == SystemGlobals
55 .getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {
56 // anonymous user has logged in
57 return false;
58 } else if (remoteUser != null
59 && !remoteUser.equals(userSession.getUsername())) {
60 // not the same user (cookie and session)
61 return false;
62 }
63 return true; // myapp user and forum user the same. valid user.
64 }
65}
修改jforum全局配置文件systemglobals.properties文件中的SSO片段:
1#############################
2# SSO / User authentication
3#############################
4# Auhentication type: use one of the following options
5#
6# sso: SSO based authentication. The called class will be the one
7# specified by the key "sso.implementation", whic must be an implementation
8# of net.jforum.sso.SSO
9#
10# default: Non-SSO authentication, which relies on the key
11# "login.authenticator" to validate users. For more information, please see
12# net.jforum.sso.LoginAuthenticator and the default implementation.
13
14#authentication.type = default
15authentication.type = sso
16
17# The above key will be used when "authentication.type" is set to "default"
18# Can be any implementation of net.jforum.sso.LoginAuthenticator
19#
20# For LDAP authentication, set the value to net.jforum.sso.LDAPAuthenticator. Also,
21# see the LDAP section below
22login.authenticator = net.jforum.sso.DefaultLoginAuthenticator
23
24# When using authentication.type = default, you may choose to disable
25# the automatic login feature, which will prevents users to get
26# automatic logged in when they come back to the forum
27auto.login.enabled = true
28
29# The above key will be be used then "authentication.type" is set to "sso"
30# The default implementation (used here) only checks if request.getRemoteUser()
31# is not null. This may be enough for many situations.
32
33#sso.implementation = net.jforum.sso.RemoteUserSSO
34sso.implementation = net.jforum.sso.CookieUserSSO
35#cookie.name.user = jforumSSOCookieNameUser这里不需要重写cookie.name.user了,因为在下面还有一个这个属性,直接修改就可以了
36
37# Special attributes used when creating a new user
38# Only if auhentication.type = sso
39# The attribute name to search in the session for the password.
40sso.password.attribute = password
41
42# Same as above
43sso.email.attribute = email
44
45# The default email to use if sso.email.attribute is empty
46sso.default.email = sso@user
47
48# The default password to use if sso.password.attribute is empty
49sso.default.password = sso
50
51# Optional redirect for SSO
52#
53# If a value is set, the user will be redirected to the defined
54# URL, using the following logic:
55#
56# ${sso.redirect}?returnUrl=${forum.link} +
57#
58# The value MUST start with the protocol (http:// or https://)
59#
60sso.redirect = http://localhost/jforum
然后,在web项目的登陆处理中加入cookie的设置:
1//与jforum整合代码,设置cookic
2Cookie cookie = new Cookie("jforumSSOCookieNameUser", username);
3cookie.setMaxAge(-1);
4cookie.setPath("/");
5response.addCookie(cookie);
退出处理类中,加入:
1Cookie cookie = new Cookie("jforumSSOCookieNameUser", "");
2cookie.setMaxAge(0); // delete the cookie.
3cookie.setPath("/");
4response.addCookie(cookie);
然后发布就ok了。