百度空间 | 百度首页 
 
查看文章
 
JForum和web的SSO集成
2008-10-13 14:21
  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. * @author Rafael Steil
  16. * @version $Id: $
  17. */  
  18. public class CookieUserSSO implements SSO{   
  19.     static final Logger logger = Logger.getLogger(CookieUserSSO.class.getName());   
  20.        
  21.     public String authenticateUser(RequestContext request) {   
  22.         // login cookie set by my web LOGIN application   
  23. //       Cookie cookieNameUser = ControllerUtils.getCookie(SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_USER));//这种写法会获取null,不解啊   
  24.          Cookie cookieNameUser = ControllerUtils.getCookie("jforumSSOCookieNameUser");   
  25.          String username = null;   
  26.   
  27.         if (cookieNameUser != null) {   
  28.          username = cookieNameUser.getValue();   
  29.          }   
  30.          System.out.println(cookieNameUser+" ======== "+username+" ==========");   
  31.         return username; // return username for jforum   
  32.         // jforum will use this name to regist database or set in HttpSession   
  33.   
  34.      }   
  35.   
  36.     public boolean isSessionValid(UserSession userSession,   
  37.              RequestContext request) {   
  38.          Cookie cookieNameUser = ControllerUtils.getCookie(SystemGlobals   
  39.                  .getValue(ConfigKeys.COOKIE_NAME_USER)); // user cookie   
  40.                  String remoteUser = null;   
  41.   
  42.                 if (cookieNameUser != null) {   
  43.                  remoteUser = cookieNameUser.getValue(); // jforum username   
  44.                  }   
  45.   
  46.                 if (remoteUser == null  
  47.                  && userSession.getUserId() != SystemGlobals   
  48.                  .getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {   
  49.                 // user has since logged out   
  50.                 return false;   
  51.                  } else if (remoteUser != null  
  52.                  && userSession.getUserId() == SystemGlobals   
  53.                  .getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {   
  54.                 // anonymous user has logged in   
  55.                 return false;   
  56.                  } else if (remoteUser != null  
  57.                  && !remoteUser.equals(userSession.getUsername())) {   
  58.                 // not the same user (cookie and session)   
  59.                 return false;   
  60.                  }   
  61.                 return true; // myapp user and forum user the same. valid user.   
  62.   
  63.      }   
  64.   
  65. }  



修改systemglobals.properties文件中的SSO片段

Java代码
  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:8082/jforum  


然后,在web项目的登陆处理中加入cookie的设置

Java代码
  1. //与jforum整合代码,设置cookic   
  2.              Cookie cookie = new Cookie("jforumSSOCookieNameUser", username);   
  3.              cookie.setMaxAge(-1);   
  4.              cookie.setPath("/");   
  5.              response.addCookie(cookie);  



退出处理类中,加入

Java代码
  1. Cookie cookie = new Cookie("jforumSSOCookieNameUser", "");   
  2.          cookie.setMaxAge(0); // delete the cookie.   
  3.          cookie.setPath("/");   
  4.          response.addCookie(cookie);  


类别:java学习笔记 | 添加到搜藏 | 浏览() | 评论 (0)
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu