Java實(shí)現(xiàn)微信掃碼登入的實(shí)例代碼
微信掃碼登入
首先去通過(guò)微信開放平臺(tái)做好開發(fā)者資質(zhì)認(rèn)證,創(chuàng)建網(wǎng)站應(yīng)用然后等待審核
開發(fā)者資質(zhì)認(rèn)證

網(wǎng)站應(yīng)用

審核通過(guò)的話就是這個(gè)樣子 還有最底下的授權(quán)回調(diào)地址 (www.xxxxx.com) 填寫域名即可

pom
<!-- WeChatQrCode --> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency>
第一步 用戶通過(guò)點(diǎn)擊事件獲取到微信二維碼連接

/**
* 獲取微信登陸二維碼地址
* @return
*/
@RequestMapping(value = "/getQRCodeUrl",method = RequestMethod.POST)
public Message getQRCodeUrl() {
logger.info("獲取二維碼地址");
try {
String codeUrl = weChatService.getQRCodeUrl();
logger.info("codeUrl:"+codeUrl);
return new Message(ResponseEnum.SUCCESS,codeUrl);
}catch (Exception e){
logger.error(e.toString()+e);
return new Message(ResponseEnum.FALL);
}
}
@Override
public String getQRCodeUrl() {
// 生成 state 參數(shù),用于防止 csrf
String date = DateUtil.format(new Date(), "yyyyMMdd");
String state = MD5Utils.generate(CSRF_KEY + date);
return wxMpService.buildQrConnectUrl(wxRedirectUrl,"snsapi_login", state);
}
- https://open.weixin.qq.com/connect/qrconnect?appid=xxxx&redirect_uri=xxxxxx&response_type=code&scope=snsapi_login&state=e97555458779708b99b9d40cb49f54245c7500e536445d32#wechat_redirect
- appid 是你網(wǎng)站應(yīng)用里面的 redirect_uri 你設(shè)置的授權(quán)回調(diào)地址 scope網(wǎng)站掃碼登入為snsapi_login即可 state。微信開放平臺(tái)文檔文檔寫的很詳細(xì),看不懂看文檔
- 用戶掃碼成功之后微信會(huì)回調(diào)你設(shè)置的回調(diào)地址 獲取二維碼連接
/**
* 回調(diào)地址
* @param code
* @param state
* @return
*/
@RequestMapping(value = "/wxCallBack",method = RequestMethod.POST)
public Message wxCallBack(HttpServletRequest request, HttpServletResponse response,
@RequestParam(name = "code",defaultValue = "") String code, @RequestParam(name = "state",defaultValue = "")String state) {
if(StringUtils.isBlank(code)){
return new Message(ResponseEnum.ESSENTIAL_IS_NULL);
}
if(StringUtils.isBlank(state)){
return new Message(ResponseEnum.ESSENTIAL_IS_NULL);
}
logger.info("微信回調(diào)------------");
logger.info(code+"------"+state);
try {
Message message=weChatService.wxCallBack(request,code, state);
return message;
}catch (Exception e){
logger.error(e.toString()+"\n"+e);
return new Message(ResponseEnum.FALL);
}
}
@Override
public Message wxCallBack(HttpServletRequest request, String code, String state) {
String openId = null;
if (code != null) {
// 獲取 openid
try {
WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
if (accessToken == null) {
return new Message(ResponseEnum.DATA_IS_NULL);
}
openId = accessToken.getOpenId();
log.info("openId:" + openId);
/*token = accessToken.getAccessToken();*/
WxMpUser wxUser = wxMpService.oauth2getUserInfo(accessToken, null);
log.info(wxUser.toString());
return new Message(ResponseEnum.WECHAT_NOT_BINGDING, wxUser);
} catch (WxErrorException e) {
log.error(e.getMessage(), e);
return new Message(ResponseEnum.FALL);
}
}
return new Message(ResponseEnum.FALL);
}
前端請(qǐng)求
$("#weiLog").click(function () {
$.ajax({
type: "POST",
url: "/api/wx/getQRCodeUrl",
dataType: 'json',
success: function (result) {
//console.log(result);
if(result.code==0){
this.itop = (window.screen.availHeight - 500) / 2;
//獲得窗口的水平位置
this.ileft = (window.screen.availWidth - 400) / 2;
this.w = window.open(
result.data,
"newwindow",
"height=500, width=600, top=" +
this.itop +
", left = " +
this.ileft +
", toolbar=no, menubar=no,scrollbars=no, resizable=no,location=no, status=no"
);
}
}
});
});
總結(jié)
到此這篇關(guān)于Java實(shí)現(xiàn)微信掃碼登入的實(shí)例代碼的文章就介紹到這了,更多相關(guān)java微信掃碼登入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JPA之多對(duì)多查詢死循環(huán)嵌套問(wèn)題及解決方案
這篇文章主要介紹了JPA之多對(duì)多查詢死循環(huán)嵌套問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
IDEA斷點(diǎn)調(diào)試,斷點(diǎn)不起作用的解決
這篇文章主要介紹了IDEA斷點(diǎn)調(diào)試,斷點(diǎn)不起作用的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Springboot使用RabbitMQ實(shí)現(xiàn)關(guān)閉超時(shí)訂單(示例詳解)
介紹了如何在Spring Boot項(xiàng)目中使用RabbitMQ實(shí)現(xiàn)訂單的延時(shí)處理和超時(shí)關(guān)閉,通過(guò)配置RabbitMQ的交換機(jī)、隊(duì)列和綁定關(guān)系,以及編寫監(jiān)聽方法,實(shí)現(xiàn)了訂單數(shù)據(jù)的發(fā)送和延時(shí)消費(fèi),感興趣的朋友一起看看吧2025-01-01
java final 和instanceof 關(guān)鍵字的區(qū)別
這篇文章介紹了java final 和instanceof 關(guān)鍵字的區(qū)別,有需要的朋友可以參考一下2013-09-09
關(guān)于如何搭建CAS服務(wù)并將CAS項(xiàng)目導(dǎo)入IDEA
這篇文章主要介紹了關(guān)于如何搭建CAS服務(wù)并將CAS項(xiàng)目導(dǎo)入IDEA的問(wèn)題,文中提供了詳細(xì)的圖文講解,需要的朋友可以參考下,如果有錯(cuò)誤的地方還請(qǐng)指正2023-03-03
SpringBoot多環(huán)境打包與配置文件排除實(shí)踐記錄
本文介紹了SpringBoot項(xiàng)目多環(huán)境打包與配置文件排除實(shí)踐,包括多環(huán)境配置的實(shí)現(xiàn)方法、打包時(shí)排除配置文件的方法以及動(dòng)態(tài)加載外部配置文件的最佳實(shí)踐,感興趣的朋友跟隨小編一起看看吧2024-11-11
解決IDEA target文件夾越來(lái)越大的問(wèn)題
這篇文章主要介紹了解決IDEA target文件夾越來(lái)越大的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
java如何通過(guò)Kerberos認(rèn)證方式連接hive
該文主要介紹了如何在數(shù)據(jù)源管理功能中適配不同數(shù)據(jù)源(如MySQL、PostgreSQL和Hive),特別是如何在SpringBoot3框架下通過(guò)Kerberos認(rèn)證與Hive進(jìn)行安全交互,文章詳細(xì)描述了Kerberos認(rèn)證過(guò)程,包括配置krb5.conf和keytab文件、處理Hadoop和Hive版本兼容性問(wèn)題2025-02-02
Java設(shè)計(jì)模式之適配器模式簡(jiǎn)介
這篇文章主要介紹了Java設(shè)計(jì)模式之適配器模式,需要的朋友可以參考下2014-07-07

