微信企业号简单的OAuth2验证接口实例(使用SpringMVC非注解方式)

微信企业号简单的OAuth2验证接口实例(使用SpringMVC非注解方式)目前企业号只提供了scope为"snsapi_base"的应用授权作用域,也就是不会跳转到让用户授权的页面。

之前写了使用注解方式验证并获取用户信息的实例,大家不是很理解,问题很多,现在附上简单的验证获取用户信息的实例!

微信企业号OAuth2验证接口实例(使用SpringMVC)

OAuth2.0验证需要注意:

1.redirect_uri参数是授权回调地址,也就是说这个地址外网是可以访问的,所以如果使用本地映射服务器的猿们请修改为外网可以访问的地址!2.配置可信域名,可信域名是1中redirect_uri的域名部分,不需要http,支持二级域名及IP地址!3.根据微信回调到你地址的URL 中会带有code参数,code参数每次获取的值都不一样并且5分钟有效。

4.在使用code参数获取用户信息的时候,确保每次的code参数都是新的,,之前看到有人每次获取的code是新的但是传如的code还是之前已经使用过的,因此慎重!

此处附上代码:

SimpleOAuth2Controller:

<span style="font-family:FangSong_GB2312;font-size:14px;">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 * */@Controllerpublic class SimpleOAuth2Controller {/** * 拼接网页授权链接 * 此处步骤也可以用页面链接代替 * @return */@RequestMapping(value = { "/oauth2wx.do" })public String Oauth2API(HttpServletRequest request){//获取项目域名String reqUrl =request.getLocalAddr();//拼接微信回调地址String backUrl ="" + reqUrl + "/oauth2me.do";String redirect_uri = "";try {redirect_uri = java.net.URLEncoder.encode(backUrl, "utf-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}String oauth2Url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Constants.CORPID + "&redirect_uri=" + redirect_uri+ "&response_type=code&scope=snsapi_base&state=sunlight#wechat_redirect";return "redirect:" + oauth2Url;}/** * 授权回调请求处理 * @return */@RequestMapping(value = { "/oauth2me.do" })public String oAuth2Url(HttpServletRequest request, @RequestParam String code){AccessToken accessToken = QiYeUtil.getAccessToken(Constants.CORPID, Constants.SECRET);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 "user";}/** * 调用接口获取用户信息 * * @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 "";}}</span>运行截图:

服务器调试图:

手机端结果:

转载请注明出处,以免惨不忍睹!

技术交流请加入QQ群:点击链接加入群【微信企业号开发交流】:?_wv=1027&k=RgbtOX

QQ群:89714226

人生伟业的建立 ,不在能知,乃在能行。

微信企业号简单的OAuth2验证接口实例(使用SpringMVC非注解方式)

相关文章:

你感兴趣的文章:

标签云: