在jforum工程下創(chuàng)建一個CookieUserSSO,實現(xiàn)SSO接口:
1
package net.jforum.sso;
2
3
import javax.servlet.http.Cookie;
4
5
import net.jforum.ControllerUtils;
6
import net.jforum.context.RequestContext;
7
import net.jforum.entities.UserSession;
8
import net.jforum.util.preferences.ConfigKeys;
9
import net.jforum.util.preferences.SystemGlobals;
10
11
import org.apache.log4j.Logger;
12
13
/**
14
* jforum 與 web 項目整合的的處理類
15
*/
16
public 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片段:
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

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
15
authentication.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
22
login.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
27
auto.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
34
sso.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.
40
sso.password.attribute = password
41
42
# Same as above
43
sso.email.attribute = email
44
45
# The default email to use if sso.email.attribute is empty
46
sso.default.email = sso@user
47
48
# The default password to use if sso.password.attribute is empty
49
sso.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
#
60
sso.redirect = http://localhost/jforum
然后,在web項目的登陸處理中加入cookie的設置:
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

1
//與jforum整合代碼,設置cookic
2
Cookie cookie = new Cookie("jforumSSOCookieNameUser", username);
3
cookie.setMaxAge(-1);
4
cookie.setPath("/");
5
response.addCookie(cookie);
退出處理類中,加入:
2

3

4

5

1
Cookie cookie = new Cookie("jforumSSOCookieNameUser", "");
2
cookie.setMaxAge(0); // delete the cookie.
3
cookie.setPath("/");
4
response.addCookie(cookie);
然后發(fā)布就ok了。

2

3

4
