jsp 应用系统整合discuz论坛用户系统

关键代码
/**
     * MD5算法
     * 
@param strToEncrypt
     * 
@return
     
*/

    
public   static  String  Md5(String strToEncrypt)
    
{
        
char hexDigits[] = '0''1''2''3''4''5''6''7''8''9',
                
'a''b''c''d''e''f' }
;
            
try 
            
{
                
byte[] strTemp = strToEncrypt.getBytes();
                MessageDigest mdTemp 
= MessageDigest.getInstance("MD5");
                mdTemp.update(strTemp);
                
byte[] md = mdTemp.digest();
                
int j = md.length;
                
char str[] = new char[j * 2];
                
int k = 0;
                
for (int i = 0; i < j; i++
                
{
                    
byte byte0 = md[i];
                    str[k
++= hexDigits[byte0 >>> 4 & 0xf];
                    str[k
++= hexDigits[byte0 & 0xf];
                }

                
return new String(str);
            }

            
catch (Exception e)
            
{
                
return "";
            }


    }
整合函数
package  com.tecamo.cms.util;

import  java.io.UnsupportedEncodingException;
import  java.util.Map;
import  java.util.Random;
import  java.util.Set;

import  com.tecamo.util.encrypt.EncryptString;

import  sun.misc.BASE64Decoder;


public   class  DiscuzPassportUtils 
{

        
public static String passportEncrypt(String src, String key) {
                Random random 
= new Random();
                random.setSeed(System.currentTimeMillis());
                String rand 
= "" + random.nextInt() % 32000;
                String encryptKey 
= EncryptString.encryptMD5(rand);

                
int ctr = 0;
                String tmp 
= "";

                
for (int i = 0; i < src.length(); ++i) {
                        ctr 
= (ctr == encryptKey.length() ? 0 : ctr);
                        tmp 
+= encryptKey.charAt(ctr);
                        
char c = (char) (src.charAt(i) ^ encryptKey.charAt(ctr));
                        tmp 
+= c;
                        ctr
++;
                }

                String passportKey 
= passportKey(tmp, key);
                
return new sun.misc.BASE64Encoder().encode(passportKey.getBytes());
        }


        
public static String passortDecrypt(String src, String key) {
                
byte[] bytes = null;
                
try {
                        bytes 
= new BASE64Decoder().decodeBuffer(src);
                        src 
= new String(bytes);
                }
 catch (Exception e) {
                        
return null;
                }

                src 
= passportKey(src, key);

                String tmp 
= "";
                
for (int i = 0; i < src.length(); ++i) {
                        
char c = (char)(src.charAt(i) ^ src.charAt(++i));
                        tmp 
+= c;
                }

                
return tmp;
        }


        
public static String passportKey(String src, String key) {
                String encryptKey 
= EncryptString.encryptMD5(key);
                
int ctr = 0;
                String tmp 
= "";
                
for (int i = 0; i < src.length(); ++i) {
                        ctr 
= (ctr == encryptKey.length() ? 0 : ctr);
                        
char c = (char) (src.charAt(i) ^ encryptKey.charAt(ctr));
                        tmp 
+= c;
                        ctr
++;
                }

                
return tmp;
        }


        
        
public static String passportEncode(Map<String, String> data) {
                Set
<String> keys = data.keySet();
                String ret 
= "";
                
for (String key : keys) {
                        
try {
                                ret 
+= java.net.URLEncoder.encode(key, "UTF-8"+ "=" + java.net.URLEncoder.encode(data.get(key), "UTF-8"+ "&";
                        }
 catch (UnsupportedEncodingException e) {
                                
return "";
                        }

                }

                
if (ret.length() > 0)
                        
return ret.substring(0, ret.length() - 1);
                
return "";
        }

}



调用方法:
login.jsp
<% @ page language = " java "   import = " java.util.* "  pageEncoding = " gbk " %>
<% @page  import = " com.tecamo.util.web.MyRequest " %>
<% @page  import = " com.tecamo.cms.util.DiscuzPassportUtils " %>
<% @page  import = " com.tecamo.cms.util.ShareFunction " %>
<% @page  import = " com.tecamo.util.encrypt.EncryptString " %>
<% @page  import = " java.net.URLEncoder " %>
<%

String strUser
= (String) session.getAttribute( " username " );
if (strUser != null   &&   ! strUser.equals( "" ))
{
    out.println(
"当前用户:" + strUser);
}


String forward
= MyRequest.getString(request, " forward " , "" );
String action
= MyRequest.getString(request, " action " , "" );
if (action.equals( " login " ))
{
    String forumURL
="http://devserver:8066";
    String userName
=MyRequest.getString(request,"account","");
    String email
=userName;
    String pwd
=MyRequest.getString(request,"pwd","");
    Map
<String, String> mb = new LinkedHashMap<String, String>();

    mb.put(
"time"""+System.currentTimeMillis());
    mb.put(
"username", userName);
    mb.put(
"email", email);
    mb.put(
"password", pwd);
    String key 
= "12345678901"
    String enc
=DiscuzPassportUtils.passportEncode(mb);
    String auth 
= DiscuzPassportUtils.passportEncrypt(enc, key);
    String verify 
= "login" + auth + forward + key;
    verify 
= EncryptString.encryptMD5(verify);                                        
    String location 
= forumURL+"/api/passport.php?action=login&auth="+URLEncoder.encode(auth, "UTF-8")+"&forward="+URLEncoder.encode(forward, "UTF-8")+"&verify="+URLEncoder.encode(verify, "UTF-8");

    response.sendRedirect(location);
    
    session.setAttribute(
"username",userName);
}


 
%>
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
  
< head >
  
  
</ head >
  
  
< body >
      
< form >
          
< table >
              
< tr >
                  
< td > 帐号 </ td >< td >< input type = " text "  name = " account " ></ td >
              
</ tr >
              
< tr >< td > 密码 </ td >< td >< input type = " password "  name = " pwd " ></ td ></ tr >
              
< tr >< td colspan = " 2 " >
              
< input type = " hidden "  value = " <%=forward%> " >
              
< input type = " hidden "  name = " action "  value = " login " >
              
< input type = " submit "  value = " 登录 " >   </ td ></ tr >
          
</ table >
      
</ form >
  
</ body >
</ html >

logout.jsp
<% @ page language = " java "   import = " java.util.* "  pageEncoding = " ISO-8859-1 " %>
<% @page  import = " com.tecamo.util.encrypt.EncryptString " %>
<% @page  import = " java.net.URLEncoder " %>
<% @page  import = " com.tecamo.util.web.MyRequest " %>
<%
//discuz 地址
String forumURL
= http://dddd:8066 ;
String forward
= MyRequest.getString(request, " forward " , "" );
//discuz 配置的私钥
String key 
=   " 12345678901 " ;                                        
String verify 
=   " logout "   +  forward  +  key;
verify 
=  EncryptString.encryptMD5(verify);        
String location 
=  forumURL + " /api/passport.php?action=logout&forward= " + URLEncoder.encode(forward,  " UTF-8 " ) + " &verify= " + URLEncoder.encode(verify,  " UTF-8 " );
response.sendRedirect(location);
session.invalidate();
%>

你可能感兴趣的:(discuz)