Commit 4daac078 authored by hezhen's avatar hezhen

123

parent ad63ee67
...@@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.GetMapping; ...@@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
...@@ -45,10 +44,16 @@ public class WeixinController { ...@@ -45,10 +44,16 @@ public class WeixinController {
if (StringUtils.isBlank(redirec_url)){ if (StringUtils.isBlank(redirec_url)){
redirec_url=""; redirec_url="";
} }
String encrypt_curr_url = Base64.encode(redirec_url); try {
String encrypt_curr_url = Base64.encode(redirec_url.getBytes("utf-8"));
redirec_url=url+"?" + WECHAT_AUTOLOGIN_CALLBACKURL_KEY+ "=" + encrypt_curr_url; redirec_url=url+"?" + WECHAT_AUTOLOGIN_CALLBACKURL_KEY+ "=" + encrypt_curr_url;
String oauth_api=weixinService.getAuthorize(redirec_url); String oauth_api=weixinService.getAuthorize(redirec_url);
return String.format("redirect:"+oauth_api); return String.format("redirect:"+oauth_api);
}catch (Exception e){
e.printStackTrace();
log.info("网络异常===" + e.getMessage());
return String.format("网络异常");
}
} }
/** /**
...@@ -64,30 +69,35 @@ public class WeixinController { ...@@ -64,30 +69,35 @@ public class WeixinController {
log.info("----code为空---"); log.info("----code为空---");
return String.format("code为空"); return String.format("code为空");
} }
JSONObject jsonData = weixinService.getAccessToken(code); try {
String openid = jsonData.getString("openid"); JSONObject jsonData = weixinService.getAccessToken(code);
String access_token = jsonData.getString("access_token"); String openid = jsonData.getString("openid");
String refresh_token = jsonData.getString("refresh_token"); String access_token = jsonData.getString("access_token");
log.info("-----微信回调userInfo---openid=="+openid+"----access_token==="+access_token); String refresh_token = jsonData.getString("refresh_token");
HttpSession session = request.getSession(); log.info("-----微信回调userInfo---openid=="+openid+"----access_token==="+access_token);
//验证access_token是否失效 HttpSession session = request.getSession();
JSONObject validateData = weixinService.getValidateData(access_token, openid); //验证access_token是否失效
if (!"0".equals(validateData.getString("errcode"))){ JSONObject validateData = weixinService.getValidateData(access_token, openid);
//刷新access_token if (!"0".equals(validateData.getString("errcode"))){
JSONObject refreshData= weixinService.getRefreshToken(refresh_token); //刷新access_token
access_token = refreshData.getString("access_token"); JSONObject refreshData= weixinService.getRefreshToken(refresh_token);
access_token = refreshData.getString("access_token");
}
UserInfo userInfo=new UserInfo();
userInfo.setOpenId(openid);
log.info("UserInfo===" + JSONObject.toJSONString(userInfo));
String encode = Base64.encode(JSONObject.toJSONString(userInfo));
session.removeAttribute(frontSessionKey);
session.setAttribute(frontSessionKey, encode);
callback =new String(Base64.decode(callback), "utf-8");
log.info("callback===" + callback);
return String.format("redirect:"+callback);
}catch (Exception e){
e.printStackTrace();
log.info("网络异常===" + e.getMessage());
return String.format("网络异常");
} }
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:"+callback);
}
}
} }
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