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做注解用
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(检查方法是否有上方注解 如果有 就进行微信验证)
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;@Component
public class NeedOpenId implements IMethodInvokerIntercepter {@Autowiredprivate AdminInfoService adminInfoService;@Autowiredprivate StudentService studentService;@Overridepublic 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) {// 是请求微信之后返回来的// 带着openidSnsAccessToken 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")
@Openid
public 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";}
Springmvc集成jfinal微信 微信服务号开发相关推荐
- 【微信服务号开发】01.接入指南
前言 当作为小白,来开发微信的时候,只依据官方文档来开发是很痛苦的,怎么配置,怎么编写代码文件,怎么让映射到外网访问,问题很多,比较痛苦. 下面内容来解决这些痛点,有不懂的问题,可以在下面留言评论哦. ...
- 微信服务号开发-获取用户位置信息
微信服务号开发-获取用户位置信息 在微信公众号开发的中,获取用户位置信息是非常常见的功能需求,通过用户的位置信息,可以做一些地图导航,以及基于LBS的营销活动. 下面将介绍微信服务号获取用户位置信息的 ...
- 微信服务号开发的完整人性化版攻略
前言: 本次要讲述的是一个本人完整微信服务号开发的经验分享,微信服务号的作品:请搜索微信号:zjaisino,名称:爱信诺Aisino一站式服务平台.(这里声明,这不是打广告,只是为了方便各位开花攻城 ...
- 微信公众号_订阅号+服务号开发工具包-翟东平-专题视频课程
微信公众号_订阅号+服务号开发工具包-15114人已学习 课程介绍 "微信公众平台深度开发Java版 v2.0"系列课程共有6季,使用JAVA语言,系统讲解微信公众 ...
- 微信服务号开发时获取授权遇到的问题
1.问题 (遇到的问题)微信服务号开发时获取授权遇到的问题 公众平台返回原始数据为: 错误代码-40164,错误信息-invalid ip, not in whitelist hint: [59FKq ...
- 微信公共服务平台开发(.Net 的实现)13-------网页授权(下 :C#代码的实现 )
接着上次的理论,我们这次来研究用代码实现"网页授权获取用户基本信息",首先我们需要一个链接指向微信的授权页面,在微信开发平台中已经说了,这个链接必须在微信客户端中打开,那么我们就干 ...
- php微信地图定位导航,微信公众服务号下实现地图语音导航的方案
微信公众服务号下实现地图语音导航的方案 如何在微信公众号里面实现多商家用户,店铺详情页的语音导航功能这个问题一直困惑着我. 现在有一个解决方案供大家参考. 具体步骤如下: 1.打开:http://ma ...
- 微信公众号文章复制到服务器后台,微信个人公众号开发-打通后台服务器任督二脉...
上篇文章 和大家分享了如何搭建免费的微信个人公众号开发所需要的后台服务器. 这篇就来继续说说如何让你的公众号与后台服务器交互. 一. 微信公众号接口准备 这里我们要用前面搭建的aws服务器上部署的fl ...
- php微信公众号开发调试工具,微信公众帐号开发调试工具发布介绍
最近在做微信公众帐号开发方面的事,但用手机调试发生错误时,就什么都没回复了,靠日志记录调试效率太低下,于是乎就有了这个小工具软件的诞生. 功能简介: 在开发微信公众平台时,大多数都是用手机通过微信发送 ...
最新文章
- CCNA系列课程(1) 网络基础
- Python初学者请注意!别这样直接运行python命令,否则电脑等于“裸奔”
- android圆形菜单
- java 正方形字符串_java编程:怎么画一个正方形?
- 如何分表分库 Mycat 与shadingjdbc 区别? Mycat 是基于服务器端的形式实现数据库代理 基于服务器端的形式 改写sql语句 shadingjdbc 基于客户端 改写sql语
- JVM实用参数(四)内存调优
- kohana php,[php框架]kohana中文译本.pdf
- 基于visual Studio2013解决C语言竞赛题之1071打印工资
- ReportView使用
- PHP程序员面临的成长瓶颈
- Express入门( node.js Web应用框架 )
- iOS - JSON 数据解析
- java自动化静态代码检查_Jenkins+findbugs对java代码进行静态代码分析
- google浏览器flash插件
- 螺钉 螺母 硬币三种物体的识别
- 做一个心无杂念的平凡人
- 踩坑记录:关于低版本firefox43.0.1在控件中定义onclick=remove(),点击按钮,按钮会消失。
- python中itertools模块zip_longest函数详解
- chua系统matlab代码
- 如何获取最新的EI期刊杂志目录