springmvc整合jfinal微信 微信服務號碼開發
最近研究微信服務號開發,發現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 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

SpringBoot和SpringMVC都是Java開發中常用的框架,但它們之間有一些明顯的差異。本文將探究這兩個框架的特點和用途,並對它們的差異進行比較。首先,我們來了解一下SpringBoot。 SpringBoot是由Pivotal團隊開發的,它旨在簡化基於Spring框架的應用程式的建立和部署。它提供了一種快速、輕量級的方式來建立獨立的、可執行

SpringBoot與SpringMVC的不同之處在哪裡? SpringBoot和SpringMVC是兩個非常流行的Java開發框架,用於建立Web應用程式。儘管它們經常分別被使用,但它們之間的差異也是很明顯的。首先,SpringBoot可以被看作是一個Spring框架的擴充或增強版。它旨在簡化Spring應用程式的初始化和配置過程,以幫助開發人

SpringBoot和SpringMVC是Java開發中常用的兩個框架,它們都是由Spring框架所提供的,但在功能和使用方式上有一些區別。本文將分別介紹SpringBoot和SpringMVC的特點和區別。一、SpringBoot的特點:簡化配置:SpringBoot透過約定優於配置的原則,大大簡化了專案的配置過程。它可以自動配置專案所需的參數,開發人

spring和springmvc的區別:1、定位和功能;2、核心功能;3、應用領域;4、擴展性。詳細介紹:1、定位和功能,Spring是一個綜合性的應用程式開發框架,提供了依賴注入、面向切面編程、事務管理等功能,旨在簡化企業級應用程式的開發,而Spring MVC是Spring框架中的一個模組,用於Web應用程式的開發,實現了MVC模式;2、核心功能等等。

springboot和springmvc區別是:1、意義不同;2、配置不同;3、依賴項不同;4、開發時間不同;5、生產力不同;6、實現JAR打包功能的方式不同;7、是否提供批次處理功能;8、作用不同;9、社群和文件支援不同;10、是否需要部署描述符。

隨著網路的發展,Web服務越來越普遍。 JavaAPI作為一種應用程式接口,也不斷地推出新的版本來適應不同的應用場景。而SpringMVC作為一種流行的開源框架,能夠幫助我們輕鬆地建立Web應用程式。本文將詳細講解在JavaAPI開發中,如何使用SpringMVC進行Web服務處理,包括配置SpringMVC、編寫控制器、使用

解析SpringBoot和SpringMVC之間的異同SpringBoot和SpringMVC是Java領域中非常重要的開發架構。雖然它們都屬於Spring框架的一部分,但在使用和功能上有一些明顯的區別。本文將對SpringBoot和SpringMVC進行比較,解析它們之間的異同。首先,讓我們來了解一下SpringBoot。 SpringBo

攔截器(interceptor)的作用SpringMVC的攔截器類似於Servlet開發中的過濾器Filter,用於對處理器進行預處理和後處理。將攔截器依一定的順序聯結成一條鏈,這條鏈稱為攔截器鏈(InterceptorChain)。在存取被攔截的方法或欄位時,攔截器鏈中的攔截器就會依其先前定義的順序被呼叫。攔截器也是AOP思想的具體實作。攔截器和過濾器區別區別過濾器(Filter)攔截器(Intercepter)使用範圍是servlet規格中的一部分,任何JavaWeb工程都可以使用是Spri
