最近研究微信服務號開發,發現jfinal家封裝的SDK還是不錯的,於是就定下來用它了。
那麼問題來了:git上有demo,那麼如何整合到自己的專案中呢?研究研究唄。我們框架使用的是springmvc,下面記錄一下整合方法,以及遇到的一些問題:
1.自己專案中首先需要引用jfinal微信需要的jar包以及jfinal的sdk包,這個是必須的。
2.web.xml中配置,啟動jfinal相關配置(必須放到所有攔截器上方)
<filter> <filter-name>jfinal</filter-name> <filter-class>com.jfinal.core.JFinalFilter</filter-class> <init-param> <param-name>configClass</param-name> <param-value>com.jfinal.weixin.demo.WeixinConfig</param-value> </init-param> </filter> <filter-mapping> <filter-name>jfinal</filter-name> <url-pattern>/weixin/*</url-pattern> </filter-mapping>
3.繼承類別WeixinMsgController並重寫方法,可以實現訊息接收功能
4.這裡主要講的是
自己寫一個攔截器,攔截微信請求
<bean id="methodInvokerIntercepterManager" class="org.springframework.web.servlet.mvc.annotation.MethodInvokerIntercepterManager"> <property name="intercepters"> <list> <bean class="com.xtwl.reporter.weixin.NeedOpenId"></bean><!-- 处理是否微信认证 --> <bean class="com.xtwl.framework.security.utils.MethodPrivilegeIntercepter"> <property name="loginPage" value="/workspace/login" /> <property name="noauthPage" value="global/error/401.ftl" /> <property name="rejectMessage" value="您无权访问" /> <property name="loginPrompt" value="请先登录" /> </bean> </list> </property> </bean>
寫一個類Openid做註解用
package com.xtwl.reporter.weixin; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; //检查是否需要登录 @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface Openid { }
驗證是否登入NeedOpenId(檢查方法是否有上方註解如果有就進行微信驗證)
package com.xtwl.reporter.weixin;import java.lang.reflect.Method;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.ui.Model;import org.springframework.web.servlet.mvc.annotation.IMethodIntercepterHolder;import org.springframework.web.servlet.mvc.annotation.IMethodInvokerIntercepter;import com.jfinal.kit.PropKit;import com.jfinal.weixin.sdk.api.ApiConfig;import com.jfinal.weixin.sdk.api.ApiConfigKit;import com.jfinal.weixin.sdk.api.SnsAccessToken;import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;import com.xtwl.reporter.business.StudentService;import com.xtwl.reporter.domain.Student;import com.xtwl.water.business.AdminInfoService;import com.xtwl.water.domain.AdminInfo;@Componentpublic class NeedOpenId implements IMethodInvokerIntercepter { @Autowired private AdminInfoService adminInfoService; @Autowired private StudentService studentService; @Override public Object invokeHandlerMethod(Method handlerMethod, Object handler, HttpServletRequest request, HttpServletResponse response, Model model, IMethodIntercepterHolder chain) throws Exception { if (handlerMethod.isAnnotationPresent(Openid.class)) { ApiConfigKit.setThreadLocalApiConfig(getApiConfig()); Openid access = handlerMethod.getAnnotation(Openid.class); if (access != null) {// 如果有标签 System.out.println("需要检查openid"); //String openid = WeiXinSession.getOpenid(); String openid="oWDxdt8F5UTP8L3XV-KcmZdLmP2Q";//测试用的 System.out.println("session中的openid 为:" + openid); System.out.println(request.getRequestURL()+"=======request.getRequestURL()2"); if (openid != null && openid.length() > 5) {// session中已经有了。放行 return todo(chain, handlerMethod, handler, request, response, model, openid); } if (openid == null || openid.length() < 5) {// 没有openid 需要重新获取 String url = SnsAccessTokenApi.getAuthorizeURL(ApiConfigKit .getApiConfig().getAppId(), request.getRequestURL().toString(), true); String code = (String) request.getParameter("code"); System.out.println("code为:" + code + " 这是微信返回的请求"); if (code != null && code.length() > 1) {// 是请求微信之后返回来的 // 带着openid SnsAccessToken sn = SnsAccessTokenApi .getSnsAccessToken(ApiConfigKit.getApiConfig().getAppId(),ApiConfigKit.getApiConfig().getAppSecret(), code); System.out.println("微信返回的openid:" + sn.getOpenid()); WeiXinSession.setOpenid(sn.getOpenid()); return todo(chain, handlerMethod, handler, request, response, model, sn.getOpenid()); } else { System.out.println("重定向到微信获取openid:" + url); return "redirect:" + url; } } } } return chain.doChain(handlerMethod, handler, request, response, model); } private Object todo(IMethodIntercepterHolder chain, Method handlerMethod, Object handler, HttpServletRequest request, HttpServletResponse response, Model model, String openid) throws Exception { try { System.out.println(request.getRequestURL()+"=======request.getRequestURL()1"); if(request.getRequestURL().toString().indexOf("/mobile/post_bind/")>0){//绑定页面 放行 return chain.doChain(handlerMethod, handler, request, response, model); } // 根据openid获取用户信息 本地的 AdminInfo admin = adminInfoService.getItemByOpenid(openid); System.out.println(admin + "========数据库中的admin"); if (admin == null || admin.getId() == null) {// 未绑定 System.out.println("redirect:/mobile/bind_phone"); return "redirect:/mobile/bind_phone"; } else { Student s = studentService.getItemById(admin.getOtherid()); WeiXinSession.setWeiXinAdminInfo(admin); WeiXinSession.setWeiXinStudent(s); } return chain.doChain(handlerMethod, handler, request, response, model); } catch (Exception e) { e.printStackTrace(); return chain.doChain(handlerMethod, handler, request, response, model); } } public ApiConfig getApiConfig() { ApiConfig ac = new ApiConfig(); // 配置微信 API 相关常量 ac.setToken(PropKit.get("token")); ac.setAppId(PropKit.get("appId")); ac.setAppSecret(PropKit.get("appSecret")); /** * 是否对消息进行加密,对应于微信平台的消息加解密方式: * 1:true进行加密且必须配置 encodingAesKey * 2:false采用明文模式,同时也支持混合模式 */ ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false)); ac.setEncodingAesKey(PropKit.get("encodingAesKey", "setting it in config file")); return ac; } }
reee
接下來就是使用了@RequestMapping("/mobile/myself")@Openidpublic String toIndex(Map<String, Object>model, Principal principal,HttpServletRequest request){ model.put("principal", principal); try { AdminInfo admin = WeiXinSession.getWeiXinAdminInfo();; Student s = WeiXinSession.getWeiXinStudent(); model.put("classes", classes); model.put("admin", admin); model.put("student", s); } catch (Exception e) { e.printStackTrace(); } return "mobile/person.ftl"; }
🎜

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),