在写一个网页,需要微信授权的登录,前几次还会弹出微信授权,后面就直接没反应了
这是网页的首页
@RequestMapping("/add")
public String puclicIndex(HttpServletRequest request, HttpServletResponse response) throws IOException {
weiXinController.getOpenid(request,response);
return "forward:/news/index.html";
}
package com.videoSys.controller;
import com.videoSys.weixin.AuthAccessToken;
import com.videoSys.weixin.WXUserInfo;
import com.videoSys.weixin.WeiXinService;
import com.videoSys.weixin.WeiXinTool;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Controller
public class WeiXinController extends BaseController {
Logger logger = LoggerFactory.getLogger(WeiXinController.class);
@Autowired
private WeiXinService weiXinService;
private WeiXinTool weiXinTool = new WeiXinTool();
/**
* 微信获取openid回调接口
* @param code
* @param state
* @param request
* @param session
* @return
*/
@RequestMapping("/getOpenid.htm")
public String getOpenid(String code, String state, String refUrl, HttpServletRequest request, HttpSession session) {
System.out.println("code="+code+"state:"+state+"refUrl:"+refUrl);
System.out.println("request:"+request.toString());
System.out.println("session:"+session.toString());
if(WeiXinTool.isWX(request)){
logger.info("微信浏览器");
//微信浏览器
if(StringUtils.isNotBlank(code) && StringUtils.isNotBlank(state)){
logger.info("code,state不为空");
//获取openid跳转回来的
//有跳转链接和Code,获取openid
if (getOpenid(request)==null) {
AuthAccessToken authAccessToken = this.weiXinService.getAuthAccessToken(code);
System.out.println("AuthAccessToken=="+authAccessToken.getAccessToken()+","+authAccessToken.getOpenid()+","+authAccessToken.getScope()+","+authAccessToken.getRefreshToken() );
if(authAccessToken != null){
String openid = authAccessToken.getOpenid();
session.setAttribute("openid", openid);
System.out.println("获取到openid并设置到session中。完成获取openid流程!\topenid:"+openid);
//获取到微信用户信息
WXUserInfo wxUserInfo = this.weiXinTool.getWXUserInfo(authAccessToken);
System.out.println("wxUserInfo=="+wxUserInfo.toString());
//将微信用户信息根据openid为key存到servletContext
System.out.println("openid="+openid+"wxUserInfo=="+wxUserInfo.toString());
ServletContext servletContext = request.getServletContext();
servletContext.setAttribute(openid, wxUserInfo);
System.out.println("servletContent:"+servletContext.getAttribute(openid));
logger.info("获取微信用户信息成功.将其根据openid为key,保存进application中。");
//获取跳转之前的url
if(refUrl != null) {
logger.info("跳转回之前链接。url:"+refUrl);
return "redirect:"+refUrl;
}
}
}
}
}
return "";
}
/**
* 检查是否微信浏览器,并且获取openid
* @param request
* @throws IOException
*/
public String getOpenid(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
if(getOpenid(request) != null) {
//已经存在openid,直接退出
return null;
}
if(!WeiXinTool.isWX(request)){
//不是微信浏览器 直接退出
return null;
}
//将当前的url存到session中
StringBuffer currentUrl = request.getRequestURL();
System.out.println("currentUrl:"+currentUrl.toString());
String uri = request.getRequestURI();
System.out.println("uri:"+uri.toString());
String domain = currentUrl.substring(0,currentUrl.length()-uri.length());
System.out.println("domain:"+domain);
//调用微信获取openid
String redirectUrl = weiXinService.getAuthRedirectUrl(domain+"/getOpenid.htm?refUrl="+currentUrl,WeiXinTool.SCOPE_SNS_API_USERINFO,"STATE");
System.out.println("redirectUrl:"+redirectUrl);
response.sendRedirect(redirectUrl);
return "";
}
}
如图,我在进入该网页时就会自动调用获取微信用户信息的授权方法,前几次进入这个网页还会调用,后面就不再调用了,而且我换个工程去集成它它又会调用了,几次后又没反应了
暂时没有遇到这个问题,请提供代码