java 鎵弿寰俊鍏紬鍙蜂簩缁寸爜锛屽叧娉ㄥ苟鐧诲綍閫昏緫

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 鎵弿寰俊鍏紬鍙蜂簩缁寸爜锛屽叧娉ㄥ苟鐧诲綍閫昏緫相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e8%b0%83%e7%94%a8' title='璋冪敤'>璋冪敤   cte   ati   鍒锋柊   turn   col   cert   rust   

鍦烘櫙锛氭埛鎵弿寰俊鍏紬鍙风殑浜岀淮鐮侊紝鍏虫敞鍚庤嚜鍔ㄧ櫥褰曠綉绔欙紝鑻ュ凡鍏虫敞鍒欑洿鎺ョ櫥褰曘€?/p>

閫昏緫锛?/p>

  1. 绯荤粺鐢熸垚甯﹀弬鏁扮殑涓存椂浜岀淮鐮侊細鍙傛暟 scene_str 鑷畾涔変负鍞竴鍊硷紙鍙互鏄痷uid锛夛紝涓存椂浜岀淮鐮佺殑鐢熸垚鏂瑰紡鍙傜収瀹樻柟鎺ュ彛鐨勫紑鍙戞枃妗?/li>
  2. 鐢ㄦ埛浣跨敤寰俊鎵弿璇ヤ簩缁寸爜锛屽叧娉ㄥ悗寰俊鏈嶅姟鍣ㄤ細灏嗗叧娉ㄤ簨浠剁殑鍝嶅簲鏁版嵁杩斿洖缁欐垜浠殑搴旂敤鏈嶅姟鍣細寰俊閫氳繃鎴戜滑鍦ㄥ叕浼楀彿閰嶇疆鐨?ldquo;鏈嶅姟鍣ㄥ湴鍧€(URL)”锛屽皢鍝嶅簲鏁版嵁杩斿洖缁欐垜浠殑搴旂敤鏈嶅姟鍣紝鍝嶅簲鏁版嵁鍖呮嫭 鍙傛暟 scene_str 鍜?openid
  3. 搴旂敤鏈嶅姟鍣ㄥ皢鎺ユ敹鍒扮殑openid鍐嶆鍚戝井淇℃湇鍔″櫒鍙戣捣璇锋眰锛岃幏鍙栬鐢ㄦ埛鐨勪俊鎭紙鏄电О銆佸ご鍍忋€佸湴鍩熴€乽nionid(鑻ョ粦瀹氫簡寰俊寮€鏀惧钩鍙帮紝鍒欐湁姝ゅ弬鏁?锛?/li>
  4. 灏嗚繑鍥炵殑淇℃伅瀛樺偍鍒版暟鎹簱锛岀敤浜庣櫥褰曪細鐗瑰埆鏄?scene_str 涔熻璁板綍锛屽洜涓洪渶瑕佹牴鎹繖涓敮涓€鍊兼潵鍒ゆ柇褰撳墠鏄皝鍦ㄦ壂鎻忓叧娉ㄥ苟鐧诲綍

鍑嗗宸ヤ綔锛氱櫥褰曞井淇″叕浼楀彿骞冲彴锛屽湪鍩烘湰閰嶇疆涓嬭幏鍙?AppID锛孉ppSecret锛?ldquo;鏈嶅姟鍣ㄥ湴鍧€(URL)”鍗?ldquo;鍥炶皟鍑芥暟”锛屼护鐗孴oken銆傚涓嬪浘锛?/p>

銆€銆€鎶€鏈浘鐗? src=

 

 鏈汉鐢ㄧ殑寮€鍙戞鏋舵槸 springboot锛屼唬鐮佸涓嬶細

  1. 鍚庡彴浠g爜锛?ol>
  2. HttpClientKit.java锛氬皝瑁卙ttps璇锋眰锛岃皟鐢ㄥ井淇℃帴鍙?br />
    鎶€鏈浘鐗? id=
    package com.ht.website.utils;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.URL;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSocketFactory;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    
    import com.alibaba.fastjson.JSONObject;
    
    public class HttpClientKit 
    
        /**
         * https璇锋眰淇′换绠$悊鍣?
         * @author Timely-03
         */
        private static class TrustAnyTrustManager implements X509TrustManager 
            
            public X509Certificate[] getAcceptedIssuers() 
                return null;  
            
            
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException 
                
            
            
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException 
                
            
        
        
        /**
         * 鍙戦€乭ttps璇锋眰
         */
        private static JSONObject https(String requestUrl, String requestMethod, String submitData) 
            HttpsURLConnection httpsURLCon = null;
            
            try 
                // 璁剧疆https璁块棶妯″紡锛岄噰鐢⊿SL鍔犲瘑
                TrustManager[] tm =  new TrustAnyTrustManager() ;
                
                // 鍒涘缓SSLContext瀵硅薄锛屽苟浣跨敤鎴戜滑鎸囧畾鐨勪俊浠荤鐞嗗櫒鍒濆鍖?/span>
                SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
                sslContext.init(null, tm, new java.security.SecureRandom());
                
                // 浠巗slContext鑾峰彇SSLSocketFactory
                SSLSocketFactory ssf = sslContext.getSocketFactory();
                URL url = new URL(requestUrl);
                httpsURLCon = (HttpsURLConnection) url.openConnection();
                httpsURLCon.setSSLSocketFactory(ssf);
                httpsURLCon.setDoInput(true);
                httpsURLCon.setDoOutput(true);
                httpsURLCon.setUseCaches(false);
                httpsURLCon.setRequestMethod(requestMethod);// 璁剧疆璇锋眰鏂瑰紡get锛沺ost
                
                if ("GET".equalsIgnoreCase(requestMethod)) 
                    httpsURLCon.connect();
                
                
                // 褰撻渶瑕佹湁鏁版嵁鎻愪氦缁欏井淇℃帴鍙f椂
                if (submitData != null) 
                    OutputStream outputStream = httpsURLCon.getOutputStream();
                    outputStream.write(submitData.getBytes(WSConst.ENCODE_UTF_8));
                    outputStream.close();
                
                
                // 浠庤緭鍏ユ祦璇诲彇杩斿洖鍐呭
                InputStream inputStream = httpsURLCon.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, WSConst.ENCODE_UTF_8);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String str = null;
                StringBuffer buffer = new StringBuffer();
                while ((str = bufferedReader.readLine()) != null) 
                    buffer.append(str);
                
     
                // 閲婃斁璧勬簮
                bufferedReader.close();
                inputStreamReader.close();
                inputStream.close();
                inputStream = null;
                httpsURLCon.disconnect();
     
                return JSONObject.parseObject(buffer.toString());
                
             catch (Exception e) 
                throw new RuntimeException(e);
             finally 
                if (httpsURLCon != null) 
                    httpsURLCon.disconnect();
                
            
        
        
        /**
         * 鍙戦€乭ttps璇锋眰锛岃姹傛柟娉曪細POST
         * @param requestUrl 璇锋眰鍦板潃锛屾瘮濡傦細https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=
         * @param submitData 鎻愪氦鍙傛暟
         * @return 杩斿洖璇锋眰缁撴灉瀛楃涓?
         */
        public static JSONObject httpsPost(String requestUrl, String submitData) 
            return https(requestUrl, "POST", submitData);
        
        
        /**
         * 鍙戦€乭ttps璇锋眰锛岃姹傛柟娉曪細GET
         * @param requestUrl 璇锋眰鍦板潃锛屾瘮濡傦細https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=
         * @param submitData 鎻愪氦鍙傛暟
         * @return 杩斿洖璇锋眰缁撴灉瀛楃涓?
         */
        public static JSONObject httpsGet(String requestUrl, String submitData) 
            return https(requestUrl, "GET", submitData);
        
        
    
    View Code
  3. application.properties锛氶厤缃井淇ppid锛宎ppSecret
    鎶€鏈浘鐗? id=
    server.port=8088
    
    ##############鏁版嵁杩炴帴寮€濮?
    #mysql
    spring.datasource.url=jdbc:mysql://10.10.10.10:3306/database_wc?useUnicode=true&characterEncoding=utf8
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.dbcp2.validation-query=SELECT 1
    spring.datasource.dbcp2.time-between-eviction-runs-millis=3600000
    ##############鏁版嵁搴撹繛鎺ョ粨鏉?
    
    ##############鍓嶇妯℃澘寮€濮?
    spring.thymeleaf.suffix=.html
    spring.thymeleaf.cache=false
    spring.thymeleaf.prefix=/
    ##############鍓嶇妯℃澘缁撴潫
    
    ##############鏃ュ織閰嶇疆寮€濮?
    logging.level.root=INFO
    logging.level.org.springframework.web=INFO
    ##############鏃ュ織閰嶇疆缁撴潫
    
    ##############寰俊閰嶇疆鍙傛暟寮€濮?
    proconfig.wechatAppId = wx21abcdbafe0aaac
    proconfig.wechatAppSecret = e472b01d2fdsadfsafdsafdsafad
    ##############寰俊閰嶇疆鍙傛暟缁撴潫
    View Code
  4. WSConfig.java锛氳幏鍙栭厤缃枃浠堕厤缃俊鎭?br />
    鎶€鏈浘鐗? id=
    package com.ht.website.utils;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    /**
     * 閰嶇疆鏂囦欢绫伙紝鑾峰彇瀵瑰簲閰嶇疆鏂囦欢涓互 proconfig 寮€澶寸殑灞炴€у€?
     * @author Timely-03
     */
    @ConfigurationProperties(prefix = "proconfig")
    public class WSConfig 
    
        //寰俊AppId
        private String wechatAppId;
        //寰俊AppSecret
        private String wechatAppSecret;
        
        public String getWechatAppId() 
            return wechatAppId;
        
    
        public void setWechatAppId(String wechatAppId) 
            this.wechatAppId = wechatAppId;
        
    
        public String getWechatAppSecret() 
            return wechatAppSecret;
        
    
        public void setWechatAppSecret(String wechatAppSecret) 
            this.wechatAppSecret = wechatAppSecret;
        
    
    
    View Code
  5. WeChatAccessToken.java锛氬畾鏃惰幏鍙栧井淇ccess_token鐨勭被锛岃繖閲岀敤鐨凷pringboot鐨刢ache 鍜?瀹氭椂鍣ㄦ潵绠$悊access_token
    鎶€鏈浘鐗? id=
    package com.ht.website.wechat;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cache.annotation.CachePut;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Service;
    
    import com.alibaba.fastjson.JSONObject;
    import com.ht.website.utils.HttpClientKit;
    import com.ht.website.utils.WSConfig;
    
    /**
     * 寰俊 access_token 鐢熸垚骞剁紦瀛?
     * 1.璋冪敤寰俊鎺ュ彛锛岃幏鍙朼ccess_token
     * 2.瀛樺叆缂撳瓨锛岀紦瀛樺悕绉颁负“accesstoken”
     * 3.瀹氭椂鑾峰彇access_token锛屾洿鏂?ldquo;accesstoken”缂撳瓨
     * @author Timely-03
     *
     */
    @Service
    public class WeChatAccessToken 
        
        private static final Logger _log = LoggerFactory.getLogger(WeChatAccessToken.class);
        
        // 鑾峰彇access_token url
        private static final String _URL_GET_ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
    
        // 璁板綍鏇存柊
        private int _fixedRateCount = 0;
        
        // 灞炴€ч厤缃?ProConfig
        @Autowired
        private WSConfig _wsConfig;
        
        /**
         * 缂撳瓨 token
         * @return token淇℃伅
         */
        @Cacheable(cacheNames= "accesstokencache")
        public String getAccessToken() 
    
            return getAccessTokenStr();
        
        
        /**
         * 鏇存柊缂撳瓨token锛屽畾鏃舵洿鏂?100鍒嗛挓鏇存柊涓€娆?
         * @return token淇℃伅
         */
        @CachePut(cacheNames = "accesstokencache")
        @Scheduled(fixedRate = 100*60*1000)
        public String updateAccessToken() 
            
            return getAccessTokenStr();
        
        
        /**
         * 鑾峰彇access_token瀛楃涓?
         * @return 鑾峰彇"access_token":"ACCESS_TOKEN","expires_in":7200涓殑access_token
         */
        private String getAccessTokenStr() 
            // 璋冪敤鎺ュ彛锛岃幏鍙朼ccess_token
            JSONObject accessTokenInfo = getAccessTokenInfo();
            if (accessTokenInfo == null) 
                return null;
            
            
            return accessTokenInfo.containsKey("access_token") ? accessTokenInfo.getString("access_token") : null;
        
        
        /**
         * 璋冪敤寰俊鎺ュ彛锛岃幏鍙朼ccess_token
         * @return 姝e父鎯呭喌涓嬭繑鍥烇細"access_token":"ACCESS_TOKEN","expires_in":7200<br/>閿欒鏃惰繑鍥為敊璇爜锛歿"errcode":40013,"errmsg":"invalid appid"
         */
        private JSONObject getAccessTokenInfo() 
            // 寰俊appId
            String appId = _wsConfig.getWechatAppId();
            // 寰俊 appSecret
            String appSecret = _wsConfig.getWechatAppSecret();
            
            if (appId.isEmpty() || appSecret.isEmpty()) 
                return null;
            
            
            _fixedRateCount++;
            
            _log.info("fixedRateCount = " + _fixedRateCount);
            
            //璋冪敤寰俊鎺ュ彛url锛岃繑鍥瀉ccess_token淇℃伅
            JSONObject responseJson = HttpClientKit.httpsGet(_URL_GET_ACCESS_TOKEN.replace("APPID", appId).replace("APPSECRET", appSecret), null);
            
            if (responseJson.containsKey("access_token") == false) 
                _log.info("瀹氭椂鍒锋柊access_token澶辫触锛屽井淇¤繑鍥炵殑淇℃伅鏄? + responseJson.toJSONString());
            
            
            return responseJson;
        
    
    
    View Code
  6. WeChatService.java锛氳皟鐢ㄥ井淇℃帴鍙f湇鍔$被
    鎶€鏈浘鐗? id=
    package com.ht.website.wechat;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.alibaba.fastjson.JSONObject;
    import com.ht.website.utils.HttpClientKit;
    import com.ht.website.utils.WSConst;
    
    @Service
    public class WeChatService 
        
        private static final Logger _log = LoggerFactory.getLogger(WeChatService.class);
    
        // 鐢熸垚甯﹀弬鏁扮殑浜岀淮鐮?/span>
        public static final String _URL_CREATE_QRCODE = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=";
        // 鑾峰彇甯﹀弬鏁扮殑浜岀淮鐮?/span>
        public static final String _URL_GET_QRCODE    = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=";
        
        // 鑾峰彇鍏虫敞鐢ㄦ埛淇℃伅
        public static final String _URL_GET_USERINFO    = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=";
        
        // 浜岀淮鐮佷笟鍔$被鍨嬶細銆怮RLOGIN:鐧诲綍浜岀淮鐮併€?/span>
        public static final String _QR_TYPE_LOGIN = "WSLOGIN-";
        public static final String _QR_TYPE_LOGIN_SUPPLIER = "WSLOGIN-SUPPLIER-";
        public static final String _QR_TYPE_LOGIN_CUSTOMER = "WSLOGIN-CUSTOMER-";
        
        @Autowired
        private WeChatAccessToken _wechatAccessToken;
        
        /**
         * 鑾峰彇甯﹀弬鏁扮殑 涓存椂浜岀淮鐮侊紙鐧诲綍鐢級
         * 濡傛灉杩斿洖access_token杩囨湡锛屽垯閲嶆柊璋冪敤涓€娆¤幏鍙朼ccess_token鐨勬柟娉曪紝鍐嶇敤鏂扮殑access_token鍘荤敓鎴愪簩缁寸爜
         * @return 
         */
        public JSONObject getQRCodeInfoLogin(String scene_str) 
            // 鐢熸垚涓存椂浜岀淮鐮?/span>
            JSONObject qrcodeInfo = getQrCodeInfo(scene_str);
            
            // 鐢熸垚浜岀淮鐮佽繑鍥炴秷鎭細濡傛灉access_token杩囨湡锛屽垯鎵ц涓€娆¤幏鍙朼ccess_token锛屽苟涓旈噸鏂扮敓鎴愪复鏃朵簩缁寸爜
            if (WSConst.WECHAT_ERROR_CODE_40001.equals(qrcodeInfo.getString("errcode"))) 
                _wechatAccessToken.updateAccessToken();
                
                // 鐢熸垚涓存椂浜岀淮鐮?
                qrcodeInfo = getQrCodeInfo(scene_str);
            
            
            return qrcodeInfo;
        
        
        
        /**
         * 鑾峰彇甯﹀弬鏁扮殑 涓存椂浜岀淮鐮?
         * @return
         */
        private JSONObject getQrCodeInfo(String scene_str) 
    
            _log.info("access_token = " + _wechatAccessToken.getAccessToken());
            
            // access_token楠岃瘉
            String accessToken = _wechatAccessToken.getAccessToken();
            
            // 璇锋眰鍙傛暟锛?scene锛堝満鏅瓧绗︿覆锛?/span>
            JSONObject scene = new JSONObject();
            scene.put("scene_str", scene_str);    // 闀垮害锛? + 36 = 44
            
            // 璇锋眰鍙傛暟锛歛ction_info
            JSONObject actioninfo = new JSONObject();
            actioninfo.put("scene", scene);
            
            // 鑾峰彇浜岀淮鐮佹彁浜ょ殑鍙傛暟
            JSONObject param = new JSONObject();
            param.put("expire_seconds", 120);            // 浜岀淮鐮佽繃鏈熸椂闂达細120绉?/span>
            param.put("action_name", "QR_STR_SCENE");    // 瀛楃涓插舰寮忕殑浜岀淮鐮佸弬鏁?   
            param.put("action_info", actioninfo);
            
            return HttpClientKit.httpsPost(_URL_CREATE_QRCODE + accessToken, param.toJSONString());
        
        
        /**
         * 閫氳繃openid锛岃幏鍙栧叧娉ㄧ敤鎴蜂俊鎭?
         * @param openid 
         * @return 
         * @throws Exception
         */
        public JSONObject getUserInfoByOpenId(String openid) throws Exception 
            
            return HttpClientKit.httpsGet(_URL_GET_USERINFO + _wechatAccessToken.getAccessToken(), null);
        
    
    View Code
  7. WeChatController.java锛氱敓鎴愪复鏃朵簩缁寸爜锛屽皢浜岀淮鐮佺殑 URL 杩斿洖鍒板墠绔紱鎺ユ敹寰俊鐨勫叧娉ㄤ簨浠讹紱鐧诲綍楠岃瘉
    鎶€鏈浘鐗? id=
    package com.ht.website.controller;
    
    import java.io.InputStream;
    import java.util.List;
    import java.util.UUID;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.util.StringUtils;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.alibaba.fastjson.JSONObject;
    import com.ht.website.entity.WeChatUser;
    import com.ht.website.service.WeChatUserService;
    import com.ht.website.wechat.WeChatService;
    
    /**
     * 寰俊鐩稿叧鎺у埗鍣?
     * @author Timely-03
     */
    @Controller
    @RequestMapping(value="/wechat")
    public class WeChatController 
        
        private static final Logger _log = LoggerFactory.getLogger(WeChatController.class);
        
        //寰俊鎺ュ彛鏈嶅姟绫?/span>
        @Autowired
        private WeChatService _weChatService;
        
        //寰俊鐢ㄦ埛鏈嶅姟绫?/span>
        @Autowired
        private WeChatUserService _weChatUserService;
        
        /**
         * 璺冲埌login椤甸潰锛屽苟鐢熸垚鐧诲綍浜岀淮鐮佺殑URL
         */
        @RequestMapping(value = "/login",method = RequestMethod.GET)
        public String login(Model model, String eventKey) 
            
            // 甯﹀弬鏁颁簩缁寸爜鐨?scene_str锛歈RLOGIN-UUID锛屾瀛楃涓蹭細褰撲綔鍏虫敞浜嬩欢鐨別ventKey杩斿洖锛屽啓鍏ュ埌鏁版嵁搴撹〃鐨別ventKey涓?/span>
            String scene_str = WeChatService._QR_TYPE_LOGIN + UUID.randomUUID();
            
            // 鑾峰彇鐧诲綍浜岀淮鐮?/span>
            JSONObject qrcodeInfo = _weChatService.getQRCodeInfoLogin(scene_str);
            if (qrcodeInfo.containsKey("ticket")) 
                model.addAttribute("qrcodeurl", WeChatService._URL_GET_QRCODE + qrcodeInfo.getString("ticket"));
            
            
            model.addAttribute("eventKey", scene_str);
            
            _log.info("鑾峰彇鐧诲綍浜岀淮鐮侊細" + qrcodeInfo.toJSONString());
    
            return "login";
        
        
        /**
         * 鍥炶皟鍑芥暟锛岄厤缃湪寰俊鍏紬鍙风殑鏈嶅姟鍣ㄥ湴鍧€URL涓紝鎺ユ敹寰俊鐨勬帹閫佷簨浠?
         * @param httpServletRequest
         * @throws Exception
         */
        @RequestMapping(value = "/channel")
        public void channel(HttpServletRequest httpServletRequest) throws Exception 
            JSONObject callBackInfo = xmlToJson(httpServletRequest); //鑾峰彇鍥炶皟淇℃伅
            //涓嬮潰鏄繑鍥炵殑xml
            //<xml><ToUserName><![CDATA[gh_f6b4da984c87]]></ToUserName> //寰俊鍏紬鍙风殑寰俊鍙?
            //<FromUserName><![CDATA[oJxRO1Y2NgWJ9gMDyE3LwAYUNdAs]]></FromUserName> //openid鐢ㄤ簬鑾峰彇鐢ㄦ埛淇℃伅锛屽仛鐧诲綍浣跨敤
            //<CreateTime>20190531</CreateTime> //鍥炶皟鏃堕棿
            //<MsgType><![CDATA[event]]></MsgType>
            //<Event><![CDATA[SCAN]]></Event>
            //<EventKey><![CDATA[lrfun.com.UxJkWC1531967386903]]></EventKey> //涓婇潰鑷畾涔夌殑鍙傛暟锛坰cene_str锛?
            //<Ticket><![CDATA[gQF57zwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyY2ljbjB3RGtkZWwxbExLY3hyMVMAAgTvM0NbAwSAOgkA]]></Ticket> //鎹㈠彇浜岀淮鐮佺殑ticket
            //</xml>
            if (callBackInfo != null && StringUtils.isEmpty(callBackInfo.getString("FromUserName")) == false) 
                // 閫氳繃openid鑾峰彇鐢ㄦ埛淇℃伅
                JSONObject wechatUserInfo = _weChatService.getUserInfoByOpenId(callBackInfo.getString("FromUserName"));
                // 灏嗘暟鎹啓鍏ュ埌鏁版嵁搴撲腑锛屽墠闈㈣嚜瀹氫箟鐨勫弬鏁帮紙scene_str锛変篃闇€璁板綍鍒版暟鎹簱涓紝鍚庨潰鐢ㄤ簬妫€娴嬪尮閰嶇櫥褰?
                // INSERT INTO wechat_user_info......(鏁版嵁搴撴搷浣?
            
        
        
        /**
         * 鏄惁鐧诲綍楠岃瘉锛屽鏋滃湪琛ㄤ腑鎵惧埌浜嗗綋鍓嶇殑eventKey锛屽垯琛ㄧず鏄綋鍓嶇敤鎴风殑鍏虫敞鐧诲綍鍔ㄤ綔锛?
         * 1. eventKey鍦ㄧ敤鎴峰叧娉ㄥ悗锛屽井淇″皢鍏虫敞鐨勪簨浠讹紝閫氳繃閰嶇疆鐨?ldquo;鏈嶅姟鍣ㄥ湴鍧€URL”鎺ㄩ€佸嚭鏉ワ紝杩斿洖鍒癱hannel鏂规硶涓?
         * 2. channel鏂规硶灏唀ventKey鍐欏叆鍒扮敤鎴疯〃涓?
         * 3. 鍓嶇鐢ㄥ畾鏃跺櫒锛屼互1绉掔殑棰戠巼涓嶅仠鐨勮皟鐢╟heckLogin璇锋眰锛岄€氳繃eventKey鍒扮敤鎴疯〃涓娴嬫槸鍚﹀瓨鍦ㄨ繖涓猠ventKey鐨勬暟鎹?
         */
        @RequestMapping(value = "/checkLogin")
        @ResponseBody
        public JSONObject checkLogin(String eventKey) 
            JSONObject retObj = new JSONObject();
            
            _log.info("eventKey = " + eventKey);
            
            // 鍙栫敤鎴蜂俊鎭?/span>
            List<WeChatUser> wechatUserList = _weChatUserService.getUserByEventKey(eventKey);
            if (wechatUserList.size() > 0) 
                retObj.put("errCode",  "0");
                retObj.put("errMsg",   "鐧诲綍鎴愬姛锛?);
                retObj.put("eventKey", eventKey);
             else 
                retObj.put("errCode", "-1");
                retObj.put("errMsg",  "鐧诲綍澶辫触");
            
            
            return retObj;
        
    
        // xml杞负map
        private JSONObject xmlToJson(HttpServletRequest httpServletRequest) 
            JSONObject info = new JSONObject();
            
            try 
                InputStream inputStream = httpServletRequest.getInputStream();
                SAXReader reader = new SAXReader(); // 璇诲彇杈撳叆娴?/span>
                org.dom4j.Document document = reader.read(inputStream);
                Element root = document.getRootElement(); // 寰楀埌xml鏍瑰厓绱?/span>
                List<Element> elementList = root.elements(); // 寰楀埌鏍瑰厓绱犵殑鎵€鏈夊瓙鑺傜偣
                
                // 閬嶅巻鎵€鏈夊瓙鑺傜偣
                for (Element e : elementList) 
                    info.put(e.getName(), e.getText());
                
                
                // 閲婃斁璧勬簮
                inputStream.close();
                inputStream = null;
                
                return info;
                
             catch (Exception e) 
                e.getMessage();
            
            
            return null;
        
        
    
    View Code
  • 鍓嶇浠g爜锛?ol>
  • 鎶€鏈浘鐗? id=
    <!DOCTYPE html>
    <!-- saved from url=(0058)http://www.17sucai.com/preview/1/2017-11-05/nav/index.html -->
    <html lang="zh">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>鐧诲綍鐣岄潰</title>
    
    </head>
    <body>
    
        <!-- 鍐呭 -->
        <div id="content" class="bg-color-gray">
            <div class="login-container">
    
                <div class="row padding-20">
                    <div class="col-sm-12">
                        <div class="padding-20">
                            <span id="loginMsg" style="font-size: 25px;">鎵弿浜岀淮鐮佹敞鍐?鐧诲綍</span>
                        </div>
                        <img th:src="$qrcodeurl" /> <input type="hidden" id="eventKey"
                            name="eventKey" th:value="$eventKey">
                        <div class="padding-20">
                            <i class="glyphicon glyphicon-qrcode"></i>&nbsp;寰俊鎵弿浜岀淮鐮?
                        </div>
                    </div>
                </div>
            </div>
        </div>
    
        <script type="text/javascript">
               var wechatCheckLoginFunTimer = null;
               
               // 浠?姣忛殧1绉掔殑棰戠巼璇锋眰鍚庡彴鐨?"/wechat/checkLogin"
            $(document).ready(function () 
                wechatCheckLoginFunTimer = setInterval("wechatCheckLogin()", 1000);
            );
        
            function wechatCheckLogin()
                $.post("/wechat/checkLogin", eventKey:$("input#eventKey").val(), function(data)
                    
                    if(data.errCode == "0")
                        // 鍏虫敞骞剁櫥褰曟垚鍔燂紝娓呴櫎瀹氭椂鍣?/span>
                        clearInterval(wechatCheckLoginFunTimer);
                        $("#loginMsg").text(data.errMsg);
                        
                        //鍋滈】1绉掞紝璋冭浆鍒扮櫥褰曟垚鍔熺殑椤甸潰
                        setTimeout(function()
                            window.location.href = "/user/loginSuccess?eventKey=" + data.eventKey;
                        ,1000)
                        
                     else 
                        $("input#eventKey").innerHTML = data.errMsg;
                    
    
                , "JSON");
            
        </script>
    
    </body>
    </html>
    View Code

    鎵弿鐧诲綍閫昏緫鍒版瀹屾垚锛?/p>

  • 以上是关于java 鎵弿寰俊鍏紬鍙蜂簩缁寸爜锛屽叧娉ㄥ苟鐧诲綍閫昏緫的主要内容,如果未能解决你的问题,请参考以下文章

    [鏁欐潗绛旀][鏁版嵁缁撴瀯棰橀泦瑙g瓟(C璇█鐗?][娓呭崕澶у鍑虹増绀綸

    .net寰俊鍏紬鍙峰紑鍙戔€斺€旀秷鎭笌浜嬩欢

    濡備綍鍦ㄧ▼搴忓憳闈㈣瘯涓劚棰栬€屽嚭

    銆愮綉鐩樿祫婧愩€戦鍝ySQL鍏ㄩ泦

    銆夾pache銆? 鐖靛+澶т箰闃熺増

    RHEL7.X绯诲垪鍙婂懆杈筁inux鍙戣鐗堜腑锛屽叧浜嶮BR涓嶨PT鐨勯€夋嫨涓€浜涙€濊€冧笌寤鸿