上一篇讲到了java与discuz的测试程序,但是如果要整合已经存在的javaweb系统,还是要有很多地方要注意的。
一:本地测试
需要的条件:成套的javaweb系统,discuz,ucenter都安装在本地
步骤:
1.按照第一篇的步骤,建立好通讯。保证ucenter的应用管理中显示通讯成功。
2.找到javaweb系统的登录处理函数,在保证系统验证用户存在的情况下,加上一些同步登录的处理程序:其中包括
(1)如果uc_user_login()返回的是用户不存在,那么要注册的时候,要通过uc_user_regsiter()注册,然后再登录。
(2)如果uc_user_login()返回的是用户密码错误。那么就应该是用uc_user_edit()修改密码,保证同步,然后再登录。
(3)调用$result = uc_user_login()成功登录后,会返回一段js代码。这个时候,你就要想办法通过各种方式,让它返回到页面中,然后再跳转。测试程序用的是 out.write($result )。但是问题来了,一般登录页面登录后,直接用重定向respose.resender(url)跳到主页。由于实践得比较少,在这个问题一直纠结了很久。
想既用out.write()又想用respose.resender(url)。结果是不行的,会返回错误。因为write,flush()后就不能重复再向页面输出内容。这个时候就要回到基 础知识了。这是典型的向浏览器输送代码,然后跳转的问题。我们可以让它放在request的属性里面,然后再在jsp页面判断存不存在,存在的话就调用(用<%=%>,或者${XXX})都可以。输出后在用javascript的setTimeOut去调用window.location进行跳转。
登录需要加入的代码:(该代码结构混乱,仅做思路参考,请自行优化)
private void sync_login_forum(HttpServletRequest request,HttpServletResponse response,SysUser sysUser,String password){ String name= sysUser.getAccount(); String pwd = password; String email=name+"@utcip.com.cn"; System.out.println(name+","+pwd+","+email); Client uc = new Client(); String $result =uc.uc_user_login(name, pwd); System.out.println($result); String $ucsynlogin=""; LinkedList<String> rs = XMLHelper.uc_unserialize($result); String $username=null; String $password=null; int $uid=0; if(rs.size()>0){ $uid = Integer.parseInt(rs.get(0)); $username = rs.get(1); $password = rs.get(2); System.out.println($username+","+$password+","+$uid); if($uid > 0){ $ucsynlogin = uc.uc_user_synlogin($uid); System.out.println("ucsy:"+$ucsynlogin); }else if($uid == -1){ System.out.println("用户不存在,或者被删除,正在注册"); String $reg_return = uc.uc_user_register(name, pwd,email); System.out.println($reg_return); int $reg_uid = Integer.parseInt($reg_return); if($reg_uid <=0){ if($uid ==-1){System.out.println("用户名不合法或没有邮箱");} else if($uid == -2){System.out.println("包含要允许注册的词语");} else if($uid == -3){System.out.println("用户名已经存在");} else if($uid == -4){System.out.println("Email 格式有误");} else if($uid == -5){System.out.println("Email 不允许注册");} else {System.out.println("未定义");} }else { System.out.println("OK,正在登陆"); //登陆 $result =uc.uc_user_login(name, pwd); rs = XMLHelper.uc_unserialize($result); if(rs.size()>0){ $uid = Integer.parseInt(rs.get(0)); $username = rs.get(1); $password = rs.get(2); System.out.println($username+","+$password+","+$uid); if($uid > 0){ $ucsynlogin = uc.uc_user_synlogin($uid); System.out.println("ucsy:"+$ucsynlogin); } } } }else if($uid == -2){ System.out.println("密码错误"); String $res = uc.uc_user_edit(name, "123", pwd, email, 1); if("1".equals($res)){ System.out.println("修改密码成功"); System.out.println("正在登录"); $result =uc.uc_user_login(name, pwd); rs = XMLHelper.uc_unserialize($result); if(rs.size()>0){ $uid = Integer.parseInt(rs.get(0)); $username = rs.get(1); $password = rs.get(2); System.out.println($username+","+$password+","+$uid); if($uid > 0){ $ucsynlogin = uc.uc_user_synlogin($uid); System.out.println("ucsy:"+$ucsynlogin); } }else if("-1".equals($res)){ System.out.println("旧密码不正确"); }else{ System.out.println("修改密码不成功"); } }else{ System.out.println("未定义"); } }// else if //设置本地discuz登录的cookie信息,cookie存活时间 //直接访问论坛就可以有用户信息了 try{ response.setCharacterEncoding("utf-8"); //同步cookie信息 response.addHeader("P3P", "CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR\""); Cookie auth = new Cookie("auth", uc.uc_authcode($password+"\t"+$uid, "ENCODE")); auth.setMaxAge(31536000); auth.setDomain("localhost"); response.addCookie(auth); Cookie user = new Cookie("Discuz_loginuser", $username); response.addCookie(user); //request.getSession().setAttribute("$ucsynlogin", $ucsynlogin); request.setAttribute("$ucsynlogin", $ucsynlogin); // request.setAttribute("test", "<script type=\"text/javascript\">alert('11');</script>"); System.out.println(request.getContextPath()+"/platform/console/main.ht"); request.getRequestDispatcher("/platform/console/main.ht").forward(request, response); //response.sendRedirect("http://localhost/forum.php"); }catch(Exception e){ e.printStackTrace(); } } }
二:服务器测试
需要条件:discuz,ucenter,javaweb 必须安装在同一个主机上。
步骤:
1.首先,在服务器重新安装discuz,选择自带的ucenter。尽量不要复制代码去服务端,不然很多与数据库,ucenter的配置信息要改。
2.重新配置ucenter的应用通信,指导通讯成功为止。通讯成功了,不代表两边就可以正常通信。
3.尝试同步登录,会发现sax解析xml文件错误。断点调试下,发现$result = uc_user_login()返回的是“代理不合法,无法访问”的英文字样。这个时候自己要认真检查配置文件。特别是java端的config.propertes。确保discuz下的config/uc_ucenter的配置文件没有错,还有ucenter端的data/config.inc配置没错。都确认没错了,还是不行,请重启服务器。
4.成功登陆后,发现不能跳到主界面,那么应该就是jsp的跳转路径写错了。
5.java web那边已经有账号,但是在discuz是第一次登陆的时候,需要手动登录。解决方法:
在uc_server/model/user.php 文件中,找到adduser() 注意:网上很多说是uc_client,其实uc_client是discuz添加账号时所调用的函数。本人研究了很久,从javaweb添加账号,会先到uc服务器的,uc服务器调用uc_server/model/user.php
在后面加上
$this->db->query("INSERT INTO pre_common_member SET `uid`='".$uid."',`username`='".$username."',`password`='".$password."',`email`='".$email."',`adminid`='0',`groupid`='10',`regdate`='".$this->base->time."',`credits`='0',`timeoffset`='9999'"); $this->db->query("INSERT INTO pre_common_member_status SET `uid`='".$uid."',`regip`='".$regip."',`lastip`='".$regip."',`lastvisit`='1308642137',`lastactivity`='1308642137',`lastpost`='0',`lastsendmail`='0'"); $this->db->query("INSERT INTO pre_common_member_profile SET `uid`='".$uid."'"); $this->db->query("INSERT INTO pre_common_member_field_forum SET `uid`='".$uid."'"); $this->db->query("INSERT INTO pre_common_member_field_home SET `uid`='".$uid."'"); $this->db->query("INSERT INTO pre_common_member_count SET `uid`='".$uid."',`extcredits1`='0',`extcredits2`='0',`extcredits3`='0',`extcredits4`='0',`extcredits5`='0',`extcredits6`='0',`extcredits7`='0',`extcredits8`='0'");
function add_user($username, $password, $email, $uid = 0, $questionid = '', $answer = '', $regip = '') { $regip = empty($regip) ? $this->base->onlineip : $regip; $salt = substr(uniqid(rand()), -6); $password = md5(md5($password).$salt); $sqladd = $uid ? "uid='".intval($uid)."'," : ''; $sqladd .= $questionid > 0 ? " secques='".$this->quescrypt($questionid, $answer)."'," : " secques='',"; $this->db->query("INSERT INTO ".UC_DBTABLEPRE."members SET $sqladd username='$username', password='$password', email='$email', regip='$regip', regdate='".$this->base->time."', salt='$salt'"); $uid = $this->db->insert_id(); $this->db->query("INSERT INTO ".UC_DBTABLEPRE."memberfields SET uid='$uid'"); // BEGIN $this->db->query("INSERT INTO `dbname`.pre_common_member SET uid='$uid', username='$username', password='$password', email='$email', adminid='0', groupid='10', regdate='".$this->base->time."', credits='0', timeoffset='9999'"); $this->db->query("INSERT INTO `dbname`.pre_common_member_status SET uid='$uid', regip='$regip', lastip='$regip', lastvisit='".$this->base->time."', lastactivity='".$this->base->time."', lastpost='0', lastsendmail='0'"); $this->db->query("INSERT INTO `dbname`.pre_common_member_profile SET uid='$uid'"); $this->db->query("INSERT INTO `dbname`.pre_common_member_field_forum SET uid='$uid'"); $this->db->query("INSERT INTO `dbname`.pre_common_member_field_home SET uid='$uid'"); $this->db->query("INSERT INTO `dbname`.pre_common_member_count SET uid='$uid', extcredits1='0', extcredits2='0', extcredits3='0', extcredits4='0', extcredits5='0', extcredits6='0', extcredits7='0', extcredits8='0'"); // END return $uid; }