Skip to content

Commit a9d4766

Browse files
committed
project: 增加微信扫码登录
1 parent 1731d9b commit a9d4766

15 files changed

Lines changed: 34 additions & 48 deletions

File tree

springboot/springboot-weixin-qrcode-login/src/main/java/com/wdbyte/weixin/SpringBootApp.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
@ServletComponentScan(basePackages = "com.wdbyte.weixin.config")
1212
public class SpringBootApp {
1313

14-
public static void main(String[] args) {
15-
SpringApplication.run(SpringBootApp.class, args);
16-
}
14+
public static void main(String[] args) {
15+
SpringApplication.run(SpringBootApp.class, args);
16+
}
1717

1818
}

springboot/springboot-weixin-qrcode-login/src/main/java/com/wdbyte/weixin/config/JwtFilter.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,17 @@
2121
import org.apache.commons.lang3.StringUtils;
2222

2323
/**
24-
* JWT过滤器,拦截 /api/* 请求
24+
* JWT过滤器,拦截 /user/* 请求
2525
*/
2626
@Slf4j
27-
@WebFilter(filterName = "JwtFilter", urlPatterns = "/api/*")
27+
@WebFilter(filterName = "JwtFilter", urlPatterns = {"/user/*"})
2828
public class JwtFilter implements Filter {
2929
private static List<String> EXCLUDE_PATH_LIST = new ArrayList<>();
3030

3131
static {
32-
EXCLUDE_PATH_LIST.add("/api/v1/user/qrcode");
33-
EXCLUDE_PATH_LIST.add("/api/v1/user/code");
34-
EXCLUDE_PATH_LIST.add("/api/v1/user/login/code");
35-
EXCLUDE_PATH_LIST.add("/api/v1/user/login/qrcode");
36-
EXCLUDE_PATH_LIST.add("/api/v1/weixin/check");
37-
EXCLUDE_PATH_LIST.add("/api/v1/github/webhooks");
32+
EXCLUDE_PATH_LIST.add("/user/qrcode");
33+
EXCLUDE_PATH_LIST.add("/user/login/qrcode");
34+
EXCLUDE_PATH_LIST.add("/weixin/check");
3835
}
3936

4037
private JwtUtil jwtUtil;

springboot/springboot-weixin-qrcode-login/src/main/java/com/wdbyte/weixin/controller/WeixinServerController.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
/**
1515
* @author https://www.wdbyte.com
16-
* @date 2024/01/16
1716
*/
1817
@Slf4j
1918
@RestController
@@ -22,7 +21,7 @@ public class WeixinServerController {
2221
@Autowired
2322
private WeixinUserService weixinUserService;
2423

25-
@GetMapping(value = "/api/v1/weixin/check")
24+
@GetMapping(value = "/weixin/check")
2625
public String weixinCheck(HttpServletRequest request) {
2726
String signature = request.getParameter("signature");
2827
String timestamp = request.getParameter("timestamp");
@@ -36,9 +35,9 @@ public String weixinCheck(HttpServletRequest request) {
3635
return echostr;
3736
}
3837

39-
@PostMapping(value = "/api/v1/weixin/check")
38+
@PostMapping(value = "/weixin/check")
4039
public String weixinMsg(@RequestBody String requestBody, @RequestParam("signature") String signature,
41-
@RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce) {
40+
@RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce) {
4241

4342
log.debug("requestBody:{}", requestBody);
4443
log.debug("signature:{}", signature);
@@ -49,5 +48,4 @@ public String weixinMsg(@RequestBody String requestBody, @RequestParam("signatur
4948
return weixinUserService.handleWeixinMsg(requestBody);
5049
}
5150

52-
5351
}

springboot/springboot-weixin-qrcode-login/src/main/java/com/wdbyte/weixin/controller/WeixinUserController.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
/**
1616
* @author https://www.wdbyte.com
17-
* @date 2024/03/16
1817
*/
1918
@CrossOrigin(origins = {"https://www.wdbyte.com", "https://bing.wdbyte.com", "http://localhost:8000"})
2019
@Slf4j
@@ -27,7 +26,7 @@ public class WeixinUserController {
2726
@Autowired
2827
private JwtUtil jwtUtil;
2928

30-
@GetMapping(value = "/api/v1/user/qrcode")
29+
@GetMapping(value = "/user/qrcode")
3130
public String getQrCode() {
3231
WeixinQrCode qrCode = weixinApiUtil.getQrCode();
3332
qrCode.setUrl(null);
@@ -43,7 +42,7 @@ public String getQrCode() {
4342
* @param ticket
4443
* @return
4544
*/
46-
@GetMapping(value = "/api/v1/user/login/qrcode")
45+
@GetMapping(value = "/user/login/qrcode")
4746
public String userLogin(String ticket) {
4847
String openId = WeixinQrCodeCacheUtil.get(ticket);
4948
if (StringUtils.isNotEmpty(openId)) {

springboot/springboot-weixin-qrcode-login/src/main/java/com/wdbyte/weixin/model/ApiResult.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
/**
88
* @author https://www.wdbyte.com
9-
* @date 2022/01/04
109
*/
1110
@Slf4j
1211
@Getter

springboot/springboot-weixin-qrcode-login/src/main/java/com/wdbyte/weixin/model/WeixinQrCode.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
/**
66
* @author https://www.wdbyte.com
7-
* @date 2024/03/15
87
*/
98
@Data
109
public class WeixinQrCode {

springboot/springboot-weixin-qrcode-login/src/main/java/com/wdbyte/weixin/service/WeixinUserService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.wdbyte.weixin.service;
22

33
/**
4-
* @author niulang
5-
* @date 2024/03/24
4+
* @Author 公众号:程序猿阿朗
65
*/
76
public interface WeixinUserService {
87

springboot/springboot-weixin-qrcode-login/src/main/java/com/wdbyte/weixin/service/impl/WeixinUserServiceImpl.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,15 @@
1212
import org.springframework.stereotype.Service;
1313

1414
/**
15-
* @author niulang
16-
* @date 2024/03/24
15+
* @Author 公众号:程序猿阿朗
1716
*/
1817
@Slf4j
1918
@Service
2019
public class WeixinUserServiceImpl implements WeixinUserService {
2120

21+
@Value("${weixin.token}")
2222
private String token;
2323

24-
public WeixinUserServiceImpl(@Value("${weixin.token}") String token) {
25-
this.token = token;
26-
}
27-
2824
@Override
2925
public void checkSignature(String signature, String timestamp, String nonce) {
3026
String[] arr = new String[] {token, timestamp, nonce};

springboot/springboot-weixin-qrcode-login/src/main/java/com/wdbyte/weixin/util/AesUtils.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,25 @@
1111

1212
/**
1313
* Java 使用 AES 加密算法进行加密解密
14-
* */
14+
*/
1515
@Component
1616
public class AesUtils {
1717

1818
/**
19-
* 秘钥(需要使用长度为16、24或32的字节数组作为AES算法的密钥,否则就会遇到java.security.InvalidKeyException异常)
19+
* 秘钥(需要使用长度为16、24或32的字节数组作为AES算法的密钥,否则就会遇到java.security.InvalidKeyException异常)
2020
*/
2121
@Value("${ase.util.secret}")
2222
public String key;
2323

2424
/**
2525
* AES算法加密
26+
*
2627
* @Param:text原文
2728
* @Param:key密钥
28-
* */
29+
*/
2930

3031
@SneakyThrows
31-
public String aesEncrypt(String text) {
32+
public String aesEncrypt(String text) {
3233
// 创建AES加密算法实例(根据传入指定的秘钥进行加密)
3334
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
3435
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
@@ -43,9 +44,10 @@ public String aesEncrypt(String text) {
4344

4445
/**
4546
* AES算法解密
47+
*
4648
* @Param:base64Encrypted密文
4749
* @Param:key密钥
48-
* */
50+
*/
4951
public String aesDecrypt(String base64Encrypted) throws Exception {
5052
// 创建AES解密算法实例
5153
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

springboot/springboot-weixin-qrcode-login/src/main/java/com/wdbyte/weixin/util/ApiResultUtil.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
/**
88
* @author https://www.wdbyte.com
9-
* @date 2023/11/22
109
*/
1110
public class ApiResultUtil {
1211

0 commit comments

Comments
 (0)