diff --git a/user-service/pom.xml b/user-service/pom.xml
index 6b4935d..cdbff64 100644
--- a/user-service/pom.xml
+++ b/user-service/pom.xml
@@ -16,7 +16,6 @@
1.8
- Hoxton.SR3
@@ -186,18 +185,15 @@
3.2.1
+
+
+ com.github.binarywang
+ weixin-java-mp
+ 4.6.7.B
+
+
+
-
-
-
- org.springframework.cloud
- spring-cloud-dependencies
- ${spring-cloud.version}
- pom
- import
-
-
-
diff --git a/user-service/src/main/java/com/mh/user/config/RestTemplateConfig.java b/user-service/src/main/java/com/mh/user/config/RestTemplateConfig.java
index f6074d8..bda0bd8 100644
--- a/user-service/src/main/java/com/mh/user/config/RestTemplateConfig.java
+++ b/user-service/src/main/java/com/mh/user/config/RestTemplateConfig.java
@@ -1,14 +1,21 @@
package com.mh.user.config;
+import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
/**
* @author ljf
* @title :
- * @description : redis配置
+ * @description : 缓存配置
* @updateTime 2020-08-20
* @throws :
*/
@Configuration
public class RestTemplateConfig {
+
+ @Bean
+ public RestTemplate restTemplate() {
+ return new RestTemplate();
+ }
}
diff --git a/user-service/src/main/java/com/mh/user/config/WebSecurityConfig.java b/user-service/src/main/java/com/mh/user/config/WebSecurityConfig.java
index 6732f53..637a2f6 100644
--- a/user-service/src/main/java/com/mh/user/config/WebSecurityConfig.java
+++ b/user-service/src/main/java/com/mh/user/config/WebSecurityConfig.java
@@ -42,36 +42,36 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 禁用 csrf, 由于使用的是JWT,我们这里不需要csrf
- System.out.println("test0");
- http.cors().and().csrf().disable()
- .authorizeRequests()
- // 跨域预检请求
- .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
- // web jars
- .antMatchers("/webjars/**").permitAll()
- // 查看SQL监控(druid)
- .antMatchers("/druid/**").permitAll()
- // 首页和登录页面
- .antMatchers("/").permitAll()
- .antMatchers("/login").permitAll()
- // swagger
- .antMatchers("/swagger-ui.html").permitAll()
- .antMatchers("/swagger-resources/**").permitAll()
- .antMatchers("/v2/api-docs").permitAll()
- .antMatchers("/webjars/springfox-swagger-ui/**").permitAll()
- // 验证码
- .antMatchers("/captcha.jpg**").permitAll()
- // 服务监控
- .antMatchers("/actuator/**").permitAll()
- // 其他所有请求需要身份认证
- .anyRequest().authenticated();
- // 退出登录处理器
- http.logout().logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler());
- // token验证过滤器
- http.addFilterBefore(new JwtAuthenticationFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class);
- System.out.println("test0_1");
+// System.out.println("test0");
+// http.cors().and().csrf().disable()
+// .authorizeRequests()
+// // 跨域预检请求
+// .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
+// // web jars
+// .antMatchers("/webjars/**").permitAll()
+// // 查看SQL监控(druid)
+// .antMatchers("/druid/**").permitAll()
+// // 首页和登录页面
+// .antMatchers("/").permitAll()
+// .antMatchers("/login").permitAll()
+// // swagger
+// .antMatchers("/swagger-ui.html").permitAll()
+// .antMatchers("/swagger-resources/**").permitAll()
+// .antMatchers("/v2/api-docs").permitAll()
+// .antMatchers("/webjars/springfox-swagger-ui/**").permitAll()
+// // 验证码
+// .antMatchers("/captcha.jpg**").permitAll()
+// // 服务监控
+// .antMatchers("/actuator/**").permitAll()
+// // 其他所有请求需要身份认证
+// .anyRequest().authenticated();
+// // 退出登录处理器
+// http.logout().logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler());
+// // token验证过滤器
+// http.addFilterBefore(new JwtAuthenticationFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class);
+// System.out.println("test0_1");
// 禁用token验证
-// http.csrf().disable().authorizeRequests().anyRequest().permitAll().and().logout().permitAll();
+ http.csrf().disable().authorizeRequests().anyRequest().permitAll().and().logout().permitAll();
}
@Bean
diff --git a/user-service/src/main/java/com/mh/user/config/wechat/WechatMpConfig.java b/user-service/src/main/java/com/mh/user/config/wechat/WechatMpConfig.java
new file mode 100644
index 0000000..2411235
--- /dev/null
+++ b/user-service/src/main/java/com/mh/user/config/wechat/WechatMpConfig.java
@@ -0,0 +1,94 @@
+package com.mh.user.config.wechat;
+
+import me.chanjar.weixin.mp.api.WxMpService;
+import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
+import me.chanjar.weixin.mp.config.WxMpConfigStorage;
+import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.stereotype.Component;
+
+import java.net.URLEncoder;
+
+/**
+ * @author chison
+ * @date 2020-04-03 15:42
+ * @Description 微信网页授权信息配置类
+ */
+@Component
+@Configuration
+public class WechatMpConfig {
+
+ @Value("${wechat.mpAppId}")
+ private String mpAppId;
+ @Value("${wechat.mpAppSecret}")
+ private String mpAppSecret;
+ @Value("${wechat.redirectUri}")
+ private String redirectUri;
+ @Value("${wechat.authorizedUrl}")
+ private String authorizedUrl;
+ @Value("${wechat.access_token}")
+ private String accessToken;
+ @Value("${wechat.userinfo}")
+ private String userinfo;
+ @Value("${wechat.pushUrl}")
+ private String pushUrl;
+ @Value("${wechat.baseUrl}")
+ private String baseUrl;
+
+ public String getBaseUrl() {
+ return baseUrl;
+ }
+
+ public void setBaseUrl(String baseUrl) {
+ this.baseUrl = baseUrl;
+ }
+
+ public String getAuthorizedUrl() {
+ return authorizedUrl.replace("mpAppId", mpAppId).replace("REDIRECT_URI", URLEncoder.encode(redirectUri));
+ }
+
+ public String getAccessTokenUrl() {
+ return accessToken.replace("APPID", mpAppId).replace("SECRET", mpAppSecret);
+ }
+
+ public String getUserInfo(String accessToken, String openId) {
+ return userinfo.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
+ }
+
+ public String getPushMsgUrl(String accessToken) {
+ return pushUrl.replace("ACCESS_TOKEN", accessToken);
+ }
+
+ public String getMpAppId() {
+ return mpAppId;
+ }
+
+ public String getMpAppSecret() {
+ return mpAppSecret;
+ }
+
+ /**
+ * 配置WxMpService所需信息
+ *
+ * @return
+ */
+ @Bean // 此注解指定在Spring容器启动时,就执行该方法并将该方法返回的对象交由Spring容器管理
+ public WxMpService wxMpService() {
+ WxMpService wxMpService = new WxMpServiceImpl();
+ // 设置配置信息的存储位置
+ wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
+
+ return wxMpService;
+ }
+
+ public WxMpConfigStorage wxMpConfigStorage() {
+ // 使用这个实现类则表示将配置信息存储在内存中
+ WxMpDefaultConfigImpl wxMpInMemoryConfigStorage = new WxMpDefaultConfigImpl();
+ wxMpInMemoryConfigStorage.setAppId(getMpAppId());
+ wxMpInMemoryConfigStorage.setSecret(getMpAppSecret());
+
+ return wxMpInMemoryConfigStorage;
+ }
+}
diff --git a/user-service/src/main/java/com/mh/user/config/wechat/WechatSignUtil.java b/user-service/src/main/java/com/mh/user/config/wechat/WechatSignUtil.java
new file mode 100644
index 0000000..ae78415
--- /dev/null
+++ b/user-service/src/main/java/com/mh/user/config/wechat/WechatSignUtil.java
@@ -0,0 +1,62 @@
+package com.mh.user.config.wechat;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Base64;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * @Author : Rainbow
+ * @date : 2024-09-14
+ */
+@Slf4j
+public class WechatSignUtil {
+
+ /**
+ * SHA-1加密
+ * @param params
+ * @return
+ */
+ public static String generateSignature(Map params) {
+ // 使用TreeMap进行自然排序(按照key的ASCII码排序)
+ TreeMap sortedParams = new TreeMap<>(params);
+
+ // 拼接字符串
+ StringBuilder stringBuilder = new StringBuilder();
+ for (Map.Entry entry : sortedParams.entrySet()) {
+ if (stringBuilder.length() > 0) {
+ stringBuilder.append("&");
+ }
+ stringBuilder.append(entry.getKey()).append("=").append(entry.getValue());
+ }
+ String string1 = stringBuilder.toString();
+
+ // SHA-1加密
+ try {
+ // 获取SHA-1实例
+ MessageDigest digest = MessageDigest.getInstance("SHA-1");
+
+ // 将输入字符串转换为字节数组
+ byte[] encodedHash = digest.digest(string1.getBytes());
+
+ // 可选:将字节数组转换为Base64字符串,便于显示和传输
+ String base64Encoded = Base64.getEncoder().encodeToString(encodedHash);
+ log.info("SHA-1 (Base64 Encoded): {}", base64Encoded);
+
+ // 或者,直接以16进制形式输出
+ StringBuilder hexString = new StringBuilder();
+ for (byte b : encodedHash) {
+ String hex = Integer.toHexString(0xff & b);
+ if(hex.length() == 1) hexString.append('0');
+ hexString.append(hex);
+ }
+ log.info("SHA-1 (Hexadecimal): {}", hexString);
+ return hexString.toString();
+ } catch (NoSuchAlgorithmException e) {
+ throw new RuntimeException("SHA-1 algorithm not found", e);
+ }
+ }
+}
diff --git a/user-service/src/main/java/com/mh/user/controller/wechat/WechatController.java b/user-service/src/main/java/com/mh/user/controller/wechat/WechatController.java
new file mode 100644
index 0000000..b55932a
--- /dev/null
+++ b/user-service/src/main/java/com/mh/user/controller/wechat/WechatController.java
@@ -0,0 +1,119 @@
+package com.mh.user.controller.wechat;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.github.benmanes.caffeine.cache.Cache;
+import com.mh.user.config.wechat.WechatMpConfig;
+import com.mh.user.service.wechat.WechatUserInfoService;
+import com.mh.user.utils.CacheUtil;
+import com.mh.user.utils.SignUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author LJF
+ * @version 1.0
+ * @project mh_esi
+ * @description 微信相关接口
+ * @date 2024-11-27 15:37:57
+ */
+@Slf4j
+@Controller
+@RequestMapping("/wechat")
+public class WechatController {
+
+ @Autowired
+ private WechatMpConfig wechatMpConfig;
+
+ @Autowired
+ private RestTemplate restTemplate;
+
+ @Autowired
+ private WechatUserInfoService wechatUserInfoService;
+
+ /**
+ * 微信校验
+ *
+ * @param signature
+ * @param timestamp
+ * @param nonce
+ * @param echostr
+ * @return
+ */
+ @GetMapping("/checkToken")
+ @ResponseBody
+ public String checkSignature(@RequestParam("signature") String signature,
+ @RequestParam("timestamp") String timestamp,
+ @RequestParam("nonce") String nonce,
+ @RequestParam("echostr") String echostr) {
+ if (SignUtil.checkSignature(signature, timestamp, nonce)) {
+ return echostr;
+ } else {
+ return null;
+ }
+ }
+
+ @GetMapping("/authorize")
+ @ResponseBody
+ public String authorize() {
+ log.info("进入authorize,redirect:{}", wechatMpConfig.getAuthorizedUrl());
+ return "redirect:" + wechatMpConfig.getAuthorizedUrl();
+ }
+
+ @GetMapping("/userInfo")
+ @ResponseBody
+ public String userInfo(@RequestParam("code") String code, @RequestParam("state") String returnUrl) {
+ try {
+ // {"access_token":"86_OnLuaNu9da1Xeh3Q4PE77sLaX_CQQFNY5rvnWTK6o_-vhIeb74rydT93HCowiVKkYUAWPG8186mvQaP55Cy47Xtuo4EkqOSyiagyW1q4mVU","expires_in":7200,"refresh_token":"86_6YSDe0P0KkFnJH0KnJzvtodh9z62GdDbdl8aG-VF7Rn46DKsWvQOuES3EluhDUQ38uUboZNr9a5wU9LvoI9HDqhkWm9hoE-1T8vUPfjoLhc","openid":"o0qeN5lw4JquQy_H9Rk5QN-CyMRU","scope":"snsapi_userinfo"}
+ // 判断cache中是否存在access_token
+// CacheUtil instance = CacheUtil.getInstance();
+// Cache