标签:oauth springmvc 微信企业号oauth2验证 企业号 oauth2验证
微信企业号OAuth2验证接口(使用SpringMVC)具体处理流程如下图:
此处源码包括:微信企业号的接入及消息的简单处理,在此基础上添加OAuth2的验证实例。
源码下载地址:http://download.csdn.net/detail/rzg813/8015527
具体实现代码:
创建拦截器:OAuth2Interceptor
package org.oms.qiye.interceptor; import java.io.UnsupportedEncodingException; import java.lang.reflect.Method; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class OAuth2Interceptor implements HandlerInterceptor { /** * 在DispatcherServlet完全处理完请求后被调用 * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion() */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("**执行顺序: 3、afterCompletion**"); } /** * 在业务处理器处理请求执行完成后,生成视图之前执行的动作 */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView modelAndView) throws Exception { System.out.println("**执行顺序: 2、postHandle**"); } /** * 在业务处理器处理请求之前被调用 如果返回false 从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链 * 如果返回true 执行下一个拦截器,直到所有的拦截器都执行完毕 再执行被拦截的Controller 然后进入拦截器链, * 从最后一个拦截器往回执行所有的postHandle() 接着再从最后一个拦截器往回执行所有的afterCompletion() */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("**执行顺序: 1、preHandle**"); String url = request.getRequestURL().toString(); HttpSession session = request.getSession(); // 先判断是否有注解 HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); OAuthRequired annotation = method.getAnnotation(OAuthRequired.class); if (annotation != null) { System.out.println("OAuthRequired:你的访问需要获取登录信息!"); Object objUid = session.getAttribute("UserId"); if (objUid == null) { String resultUrl = request.getRequestURL().toString(); String param=request.getQueryString(); if(param!=null){ resultUrl+= "?" + param; } System.out.println("resultUrl="+resultUrl); try { resultUrl = java.net.URLEncoder.encode(resultUrl, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } //请求的路径 String contextPath=request.getContextPath(); response.sendRedirect(contextPath + "/oauth2.do?resultUrl=" + resultUrl); return false; } } return true; } }
package org.oms.qiye.interceptor; import java.lang.annotation.*; /** * 验证OAuth2注解 * @author Sunlight * */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface OAuthRequired { }
package org.oms.qiye.util; public class Constants { /** * 常量说明: * 此处定义的常量需要持久化,可以保存在数据库中,在需要的地方读取。 * 在多企业号中,最好以每个应用来定义。 */ public static final int AGENTID = 1; public static final String TOKEN = "sunlight"; public static final String CORPID = "你的企业号ID"; public static final String ACCESS_TOKEN = "你的企业号_ACCESS_TOKEN"; public static final String encodingAESKey = "s8vFF4f6AWay3uAdJh79WD6imaam4BV6Kl4eL4UzgfM"; }
package org.oms.qiye.web; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.oms.qiye.pojo.AccessToken; import org.oms.qiye.util.Constants; import org.oms.qiye.util.QiYeUtil; import org.oms.qiye.util.Result; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; /** * OAuth2 处理控制器 * @author Sunlight * */ @Controller public class OAuth2Controller { /** * 构造参数并将请求重定向到微信API获取登录信息 * * @param index * @return */ @RequestMapping(value = { "/oauth2.do", "/oauth2" }) public String Oauth2API(HttpServletRequest request, @RequestParam String resultUrl) { // 此处可以添加获取持久化的数据,如企业号id等相关信息 String CropId = Constants.CORPID; String redirectUrl = ""; if (resultUrl != null) { String reqUrl =request.getLocalAddr(); String backUrl ="http://" + reqUrl + "/oauth2url.do?oauth2url=" + resultUrl; System.out.println("backUrl="+backUrl); redirectUrl = oAuth2Url(CropId, backUrl); } return "redirect:" + redirectUrl; } /** * 根据code获取Userid后跳转到需要带用户信息的最终页面 * * @param request * @param code * 获取微信重定向到自己设置的URL中code参数 * @param oauth2url * 跳转到最终页面的地址 * @return */ @RequestMapping(value = { "/oauth2url.do" }) public String Oauth2MeUrl(HttpServletRequest request, @RequestParam String code, @RequestParam String oauth2url) { AccessToken accessToken = QiYeUtil.getAccessToken(Constants.CORPID, Constants.ACCESS_TOKEN); HttpSession session = request.getSession(); if (accessToken != null && accessToken.getToken() != null) { String Userid = getMemberGuidByCode(accessToken.getToken(), code, Constants.AGENTID); if (Userid != null) { session.setAttribute("UserId", Userid); } } // 这里简单处理,存储到session中 return "redirect:" + oauth2url; } /** * 构造带员工身份信息的URL * * @param corpid * 企业id * @param redirect_uri * 授权后重定向的回调链接地址,请使用urlencode对链接进行处理 * @param state * 重定向后会带上state参数,企业可以填写a-zA-Z0-9的参数值 * @return */ private String oAuth2Url(String corpid, String redirect_uri) { try { redirect_uri = java.net.URLEncoder.encode(redirect_uri, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String oauth2Url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + corpid + "&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_base&state=sunlight#wechat_redirect"; System.out.println("oauth2Url=" + oauth2Url); return oauth2Url; } /** * 调用接口获取用户信息 * * @param token * @param code * @param agentId * @return * @throws SQLException * @throws RemoteException */ public String getMemberGuidByCode(String token, String code, int agentId) { System.out.println("code==" + code + "\ntoken=" + token + "\nagentid=" + agentId); Result<String> result = QiYeUtil.oAuth2GetUserByCode(token, code, agentId); System.out.println("result=" + result); if (result.getErrcode() == "0") { if (result.getObj() != null) { // 此处可以通过微信授权用code还钱的Userid查询自己本地服务器中的数据 return result.getObj(); } } return ""; } }
package org.oms.qiye.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.oms.qiye.interceptor.OAuthRequired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; /** * 需要验证OAuth2控制器 * @author Sunlight * */ @Controller public class UserController { /** * 加载个人信息,此处添加了@OAuthRequired注解 * @param model * @return */ @RequestMapping(value={"/userInfo.do"}) @OAuthRequired public String load(HttpServletRequest request,Model model){ System.out.println("Load a User!"); HttpSession session = request.getSession(); model.addAttribute("Userid", session.getAttribute("Userid")); return "user"; } }
微信企业号调用类 {"errcode":0,"errmsg":"ok"} 此结果表示调用方法成功返回QiYeUtil.class
返回结果处理类Result.class
枚举EnumMethod.class
以上类不在列出,在其他文章已存在!
处理页面user.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Model And List</title> </head> <body> <h1>Welcome To SpringMVC! This is OAuth2 UserInfo</h1> <h3>获取到的Userid是:${UserId}</h3> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:component-scan base-package="org.oms.qiye.web"></context:component-scan> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 静态文件处理 --> <mvc:resources mapping="/images/**" location="/images/" /> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/css/**" location="/css/" /> <!-- OAuth2拦截器 --> <mvc:interceptors> <mvc:interceptor> <!-- 对所有的请求拦截使用/** ,对某个模块下的请求拦截使用:/myPath/* --> <mvc:mapping path="/**" /> <ref bean="oauth2Interceptor" /> </mvc:interceptor> </mvc:interceptors> <bean id="oauth2Interceptor" class="org.oms.qiye.interceptor.OAuth2Interceptor"> </bean> </beans>
服务器测试结果图:
手机微信客户端测试结果图:
转载请注明出处,以免惨不忍睹!
技术交流请加入QQ群:点击链接加入群【微信企业号开发交流】:http://jq.qq.com/?_wv=1027&k=RgbtOX
微信企业号OAuth2验证接口实例(使用SpringMVC)
标签:oauth springmvc 微信企业号oauth2验证 企业号 oauth2验证
原文地址:http://blog.csdn.net/omsvip/article/details/39926493