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; } }
接下来就是使用了
@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
