Commit db25d2a3 authored by hezhen's avatar hezhen

123

parent f70d42c5
package com.xxfc.platform.summit.controller;
import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSONObject;
import com.github.wxiaoqi.security.auth.client.annotation.IgnoreUserToken;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.xxfc.platform.summit.service.WeixinService;
import com.xxfc.platform.summit.vo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* @author Administrator
*/
@Controller
@RequestMapping("/auth")
@Slf4j
public class WeixinController {
public static final String WECHAT_AUTOLOGIN_CALLBACKURL_KEY = "callback";
@Autowired
WeixinService weixinService;
public static final String frontSessionKey = "frontWeixKey";
@Value("${wx.url}")
private String url;
@RequestMapping(value ="/app/unauth/wxLogin",method = RequestMethod.GET)
@IgnoreUserToken
public String index(@RequestParam(value = "redirec_url")String redirec_url){
log.info("-----微信wxLogin---redirec_url=="+redirec_url);
if (StringUtils.isBlank(redirec_url)){
redirec_url="";
}
String encrypt_curr_url = Base64.encode(redirec_url);
redirec_url=url+"?" + WECHAT_AUTOLOGIN_CALLBACKURL_KEY+ "=" + encrypt_curr_url;
String oauth_api=weixinService.getAuthorize(redirec_url);
return String.format("redirect%s",oauth_api);
}
/**
* 微信浏览器获取用户信息
* @param code
* @param callback
* @return
*/
@GetMapping(value = "/userInfo")
public String getUserInformation(String code, String callback, HttpServletRequest request) {
log.info("-----微信回调userInfo---code=="+code+"----redirec_url==="+callback);
if (StringUtils.isNotBlank(code)){
log.info("----code为空---");
return String.format("code为空");
}
JSONObject jsonData = weixinService.getAccessToken(code);
String openid = jsonData.getString("openid");
String access_token = jsonData.getString("access_token");
String refresh_token = jsonData.getString("refresh_token");
log.info("-----微信回调userInfo---openid=="+openid+"----access_token==="+access_token);
HttpSession session = request.getSession();
//验证access_token是否失效
JSONObject validateData = weixinService.getValidateData(access_token, openid);
if (!"0".equals(validateData.getString("errcode"))){
//刷新access_token
JSONObject refreshData= weixinService.getRefreshToken(refresh_token);
access_token = refreshData.getString("access_token");
}
UserInfo userInfo=new UserInfo();
userInfo.setOpenId(openid);
log.error("UserInfo===" + JSONObject.toJSONString(userInfo));
String encode = Base64.encode(JSONObject.toJSONString(userInfo));
session.removeAttribute(frontSessionKey);
session.setAttribute(frontSessionKey, encode);
callback =Base64.encode(callback);
log.error("callback===" + callback);
return String.format("redirect%s",callback);
}
}
...@@ -6,6 +6,7 @@ import javax.servlet.http.HttpServletResponse; ...@@ -6,6 +6,7 @@ import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import cn.hutool.core.codec.Base64; import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.UserAgentUtil; import com.github.wxiaoqi.security.common.util.UserAgentUtil;
import com.xxfc.platform.summit.service.WeixinService; import com.xxfc.platform.summit.service.WeixinService;
import com.xxfc.platform.summit.vo.UserInfo; import com.xxfc.platform.summit.vo.UserInfo;
...@@ -16,6 +17,8 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -16,6 +17,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import java.io.PrintWriter;
/** /**
* 微信登陆拦截器 * 微信登陆拦截器
...@@ -29,13 +32,8 @@ public class WeChatH5LoginInterceoptor extends HandlerInterceptorAdapter { ...@@ -29,13 +32,8 @@ public class WeChatH5LoginInterceoptor extends HandlerInterceptorAdapter {
/** /**
* 微信公众号自动登陆令牌的url参数名 * 微信公众号自动登陆令牌的url参数名
*/ */
public static final String WECHAT_AUTOLOGIN_CALLBACKURL_KEY = "wechat_autologin_callback_accesstoken";
public static final String frontSessionKey = "frontWeixKey"; public static final String frontSessionKey = "frontWeixKey";
@Value("${wx.url}")
private String url;
@Autowired @Autowired
WeixinService weixinService; WeixinService weixinService;
...@@ -53,53 +51,13 @@ public class WeChatH5LoginInterceoptor extends HandlerInterceptorAdapter { ...@@ -53,53 +51,13 @@ public class WeChatH5LoginInterceoptor extends HandlerInterceptorAdapter {
if (isWx) { if (isWx) {
String frontSessionValue1 = (String) session.getAttribute(frontSessionKey); String frontSessionValue1 = (String) session.getAttribute(frontSessionKey);
if (StringUtils.isNotBlank(frontSessionValue1)) { if (StringUtils.isNotBlank(frontSessionValue1)) {
String frontSessionValue = new String(Base64.decode(frontSessionValue1), "utf-8");
return true; return true;
} }
/*String curr_url = request.getRequestURL().toString() JSONObject json = new JSONObject();
+ (StringUtils.isBlank(request.getQueryString()) ? "" : "?" + request.getQueryString());*/ json.put("status",1001);
String curr_url="https://dev.dfangche.com/api/summit/activity/app/unauth/index"; json.put("data","https://dev.dfangche.com/api/summit/auth/app/unauth/wxLogin?redirec_url=https://dev.dfangche.com/h5/appHtml/view/wxh5/index.html");
String encrypt_curr_url = Base64.encode(curr_url); response.getWriter().write(json.toJSONString());
String code = request.getParameter("code");
// 没有code, 则进行网页授权获取code
log.info("curr_url=====" + curr_url + "-----code=" + code);
if (StringUtils.isBlank(code)) {
String redirec_url=curr_url+"?" + WECHAT_AUTOLOGIN_CALLBACKURL_KEY+ "=" + encrypt_curr_url;
String oauth_api=weixinService.getAuthorize(curr_url);
log.info("curr_url=====" + curr_url);
response.sendRedirect("https://www.baidu.com");
return false;
}
// 有code, 换取openid
String openid = null;
String access_token = null;
try {
log.info("调用微信网页授权接口code=" + code);
JSONObject access_token_json=weixinService.getAccessToken(code);
if (access_token_json == null || StringUtils.isNotBlank(access_token_json.getString("errcode"))) {
log.info("err: " + JSON.toJSONString(access_token_json));
log.info("调用微信网页授权接口失败, appid或者appsecret不正确");
return false;
}
openid = access_token_json.getString("openid").trim();
access_token = access_token_json.getString("access_token");
UserInfo userInfo=new UserInfo();
userInfo.setOpenId(openid);
log.error("UserInfo===" + JSONObject.toJSONString(userInfo));
String encode = Base64.encode(JSONObject.toJSONString(userInfo));
session.removeAttribute(frontSessionKey);
session.setAttribute(frontSessionKey, encode);
} catch (Exception e) {
log.info("【" + curr_url + "】获取access_token失败");
return false;
}
// 重定向到原来地址后进行自动登陆
String encrypt_callbackurl = request.getParameter(WECHAT_AUTOLOGIN_CALLBACKURL_KEY);
String decrypt_callbackurl =Base64.encode(encrypt_callbackurl.getBytes("utf-8"));
log.error("decrypt_callbackurl===" + decrypt_callbackurl);
response.sendRedirect(decrypt_callbackurl);
return false; return false;
} }
return true; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment