combinations-wx-login-starter
1.功能介绍
1.微信登录封装
- 根据微信开放平台返回的
code
来获取access_token
和unionId
,完成微信登录。
2.响应封装
- 通过
AccessTokenResponse
封装微信返回的结果,包括access_token
、expires_in
、unionId
等关键信息。
2.配置示例
yaml
sun-rays:
wx:
login:
app-id: ${WX_LOGIN_APP_ID} # 微信开放平台应用的 AppID
app-secret: ${WX_LOGIN_APP_SECRET} # 微信开放平台应用的 AppSecret
access-token-url-prefix: https://api.weixin.qq.com/sns/oauth2/access_token # 微信开放平台的 access_token_url 前缀
3.案例演示
1.创建模块
2.目录结构
3.pom.xml
xml
<dependencies>
<!-- combinations-wx-login-starter -->
<dependency>
<groupId>cn.sunxiansheng</groupId>
<artifactId>combinations-wx-login-starter</artifactId>
</dependency>
<!-- 必须引入 -->
<dependency>
<groupId>cn.sunxiansheng</groupId>
<artifactId>common-log4j2-starter</artifactId>
</dependency>
<!-- 测试用的Web模块 -->
<dependency>
<groupId>cn.sunxiansheng</groupId>
<artifactId>common-web-starter</artifactId>
</dependency>
<!-- env模块确保数据安全 -->
<dependency>
<groupId>cn.sunxiansheng</groupId>
<artifactId>common-env-starter</artifactId>
</dependency>
</dependencies>
4.application.yml 配置日志存储根目录以及.env文件的绝对路径还有微信登录
yaml
sun-rays:
log4j2:
home: /Users/sunxiansheng/IdeaProjects/sunrays-framework-demo/combinations-wx-login-starter-demo/logs # 日志存储根目录
env:
path: /Users/sunxiansheng/IdeaProjects/sunrays-framework-demo/combinations-wx-login-starter-demo # .env文件的绝对路径
wx:
login:
app-id: ${WX_LOGIN_APP_ID} # 微信开放平台应用的AppID
app-secret: ${WX_LOGIN_APP_SECRET} # 微信开放平台应用的AppSecret
5..env
properties
WX_LOGIN_APP_ID=微信开放平台应用的AppID
WX_LOGIN_APP_SECRET=微信开放平台应用的AppSecret
6.WxLoginController.java
java
package cn.sunxiansheng.wx.login.controller;
import cn.sunxiansheng.wx.login.entity.AccessTokenResponse;
import cn.sunxiansheng.wx.login.utils.WxLoginUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* Description: 微信登录Controller
*
* @Author sun
* @Create 2025/1/13 16:26
* @Version 1.0
*/
@Slf4j
@RestController
@RequestMapping("/wx")
public class WxLoginController {
@Resource
private WxLoginUtil wxLoginUtil;
@RequestMapping("/test")
public String test() {
return "test";
}
@Data
public static class CodeAndState {
/**
* 微信的code
*/
private String code;
/**
* 微信的state
*/
private String state;
}
/**
* 微信登录
*
* @param codeAndState 前端传过来的code和state
* @return 返回unionId
*/
@RequestMapping("/login")
public String login(@RequestBody CodeAndState codeAndState) {
// 使用code来完成微信登录
AccessTokenResponse accessTokenResponse = wxLoginUtil.wxLogin(codeAndState.getCode());
if (accessTokenResponse == null) {
log.error("accessToken is null");
return "null";
}
// 获取unionId
String unionId = accessTokenResponse.getUnionId();
if (unionId == null) {
log.error("unionId is null");
return "null";
}
// 获取unionId
return accessTokenResponse.getUnionId();
}
}
7.WxLoginApplication.java 启动类
java
package cn.sunxiansheng.wx.login;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Description: 微信启动类
*
* @Author sun
* @Create 2025/1/13 17:54
* @Version 1.0
*/
@SpringBootApplication
public class WxLoginApplication {
public static void main(String[] args) {
SpringApplication.run(WxLoginApplication.class, args);
}
}
8.测试说明
这里封装的是后端根据前端传过来的code和state来换取access_token的过程,并且需要在应用中配置回调域并且将前端部署到那个域上,前端才能够接受到code。