论坛首页 Java企业应用论坛

DisCuz论坛与J2EE网站集成

浏览 2962 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-12-19   最后修改:2008-12-19

DisCuz论坛与网站集成

 

参考文件 http://www.discuz.net/usersguide/advanced_passport.htm#title

 

1)  adminadmin,admin)身份登陆bbs 点击系统设置 à 扩展设置à 通行证设置

2)  在通行证设置中应用程序URL地址填写网站的地址(http://localhost:8080/mall/

3)  通行证私有密匙(1234567890

4)  应用程序注册地址(member/regist.shtml

5)  应用程序登陆地址(member/welcome.shtml

6)  应用程序推出地址(member/logoff.shtml

7)  保存退出

 

Constants中设置参数

//用于discuz登陆验证和通行证

   

public static String DISCUZKEY="1234567890"; //通行密匙   

public static String DZURL="http://localhost:82/discuz/";可改

 

suning.properties

web.url=http://localhost:8080/mall/

bbs.url=http://localhost:82/discuz/

 

在网站的登陆程序(在com.arvato.suning.member.web.action.loginAction)中添加

 

查找当前数据库如果参数正确,且用户存在则登陆bbs

 

//设置登陆时所需的参数username,password,time,email是必填的具体的内容见参考文件

//member为会员的模型

       String info = "username=" + member.getNickname() + "&password="

                                          + member.getPassword() + "&time="

                                          + DiscuzPassport.time() + "&email=" + member.getEmail();

 

// Constants.DISCUZKEYadmindiscuz中设置的通行证私有密匙

 

String auth = DiscuzPassport.passport_encrypt(info,

                                          Constants.DISCUZKEY);

 

//disforwardforward的参数 在地址栏中可以得到forward的内容

       String discuzforward = disforward;

 

       String verify = DiscuzPassport.md5("login" + auth

                                          + discuzforward + Constants.DISCUZKEY);

 

       //authdiscuzford一定要经过

 

       auth=URLEncoder.encode(auth, "UTF-8");

                           

       discuzforward=URLEncoder.encode(discuzforward, "UTF-8");

      

//登陆discuz的路径 action ,auth,forward,verifty是必需的不能少,如果错了报Illegal request等错误,祥见参考文件               

       String disurl = PropertiesMgt.getPropertiesKey(Constants.PROPERTIES_FILE_PATH, "bbs.url")+"api/passport.php?action=login&auth=" + auth

                                          + "&forward=" + discuzforward + "&verify=" + verify;

      

 

       //跳转到设置好的路径,注意redirect一定为true

       request.setAttribute("disurl", disurl);

 

       forward = new ActionForward(disurl);

 

       forward.setRedirect(true);

 

       return forward;

 

在网站的注销程序(com.arvato.suning.member.web.action.LoginOffAction)中

 

Member memberModel = (Member) request.getSession().getAttribute(Constants.SESSION_MEMBER_MODEL);

request.getSession().removeAttribute(Constants.SESSION_MEMBER_MODEL);

 

//得到如果注销discuz后跳转的路径             

String discuzforward = request.getParameter("forward");

//得到配置文件中网站所在的ip及路径

String weburl = PropertiesMgt.getPropertiesKey(Constants.PROPERTIES_FILE_PATH, "web.url"); 

//得到配置文件中discuz所在的ip及路径

 

String discuzurl = PropertiesMgt.getPropertiesKey(Constants.PROPERTIES_FILE_PATH, "bbs.url"); 

 

//如果得到了forward则可以设定discuzforward的跳转路径                 

if (discuzforward != null && !discuzforward.equals("")){

                   

discuzforward = weburl+"/member/welcome.shtml";

 

    //verifydiscuz注销时需要的通行证包括(logout:action的动作,discuzforward:跳转的路径,Constants. DISCUZKEY admin设置在 discuz通行证私有密匙)

       

String verify = DiscuzPassport.md5("logout" + discuzforward+ Constants.DISCUZKEY);

 

//disurl为调用discuz注销的实际路径需要 actionforwarddiscuz路径

String disurl = discuzurl+"api/passport.php?action=logout&auth=&forward="

                            + discuzforward + "&verify=" + verify;

 

return new ActionForward(disurl, true);

}

else{

    return mapping.findForward(Constants.SUCCESS_KEY);

}

 

 

 

package com.arvato.suning.util;

import java.io.IOException;

import java.security.MessageDigest;

import java.util.Date;

import java.util.Random;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

public class DiscuzPassport {

      

       /**

        * 通行证编码

        *

        * @param txt

        * @param key

        * @return

        */

       public static String passport_encrypt(String txt, String key) {

              Random random = new Random();

              int dd = random.nextInt(32000);

              String encryptKey = md5(String.valueOf(dd));

              byte[] keybyte = encryptKey.getBytes();

              byte[] txtbyte = txt.getBytes();

              int ctr = 0;

              int j = 0;

              byte[] tmp = new byte[2 * txtbyte.length];

              for (int i = 0; i < txt.length(); i++) {

                     ctr = ctr == keybyte.length ? 0 : ctr;

                     tmp[j++] = keybyte[ctr];

                     tmp[j++] = xor(txtbyte[i], keybyte[ctr++]);

 

              }

              return new BASE64Encoder().encode(passport_key(tmp, key));

       }

 

       /**

        * 通行证密匙处理函数

        *

        * @param btxt

        * @param key

        * @return

        */

       public static byte[] passport_key(byte[] btxt, String key) {

              key =md5(key);

              byte[] keyAry = key.getBytes();

              int ctr = 0;

              byte[] tmp = new byte[btxt.length];

              for (int i = 0; i < btxt.length; i++) {

                     ctr = (ctr == keyAry.length) ? 0 : ctr;

                     tmp[i] = xor(btxt[i], keyAry[ctr++]);

              }

              return tmp;

       }

 

       /**

        * 通行证解码

        *

        * @param txt

        * @param key

        * @return

        * @throws IOException

        */

       public String passport_decrypt(String txt, String key) throws IOException {

              byte[] t = new BASE64Decoder().decodeBuffer(txt);

              byte[] btxt = passport_key(t, key);

              byte[] btmp = new byte[btxt.length];

              int j = 0;

              for (int i = 0; i < btxt.length; i++, j++)

                     btmp[j] = xor(btxt[i], btxt[++i]);

              return btmp.toString();

       }

 

       /**

        * 异或操作

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics