Commit 162461e8 authored by 张大伟's avatar 张大伟

增加微信授权

parent 60eaa8ce
package org.rcisoft.wechat;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.rcisoft.wechat.service.WxPortalService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
/**
* 网页授权微信回调接口
* @author David
*/
@Controller
@RequestMapping("/wechat")
public class WechatRedirectController {
@Resource
private WxPortalService service;
@RequestMapping(value = {"/index"})
public String index(String code) {
try {
WxMpUser wxUser = this.service.oauth2UserInfo(code);
System.out.println("微信返回的code: " + code);
System.out.println("WxPortalController授权完毕后获取到的OpenId是:" + wxUser.getOpenId());
return "redirect:http://weega.cn.tunnel.qydev.com/login.html?openId=" + wxUser.getOpenId();
} catch (Exception exception) {
exception.printStackTrace();
return "redirect:http://weega.cn.tunnel.qydev.com/error.html";
}
}
}
package org.rcisoft.wechat.service;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 获取微信用户信息
* @author david
*/
@Service
public class WxPortalService extends WxMpServiceImpl {
@Value("${wechat.mp.appId}")
String appId;
@Value("${wechat.mp.secret}")
String secret;
@Value("${wechat.mp.token}")
String token;
@Value("${wechat.mp.aesKey}")
String aesKey;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public WxPortalService() {
}
@PostConstruct
public void init() {
WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();
config.setAppId(appId);
config.setSecret(secret);
config.setToken(token);
config.setAesKey(aesKey);
super.setWxMpConfigStorage(config);
}
public WxMpOAuth2AccessToken oauth2AccessToken(String code) throws WxErrorException {
WxMpOAuth2AccessToken token = this.oauth2getAccessToken(code);
System.out.println("token:" + token.getAccessToken() + "; code: " + code);
return token;
}
public WxMpUser oauth2UserInfo(String code) throws WxErrorException {
WxMpOAuth2AccessToken token = this.oauth2AccessToken(code);
return token != null?this.oauth2getUserInfo(token, (String)null):null;
}
public WxJsapiSignature getJsApiTicket(String url) throws WxErrorException {
return this.createJsapiSignature(url);
}
}
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