浏览代码

自动获取accesstoken

hsj 1 月之前
父节点
当前提交
e324c062b1

+ 0 - 3
.mvn/wrapper/maven-wrapper.properties

@@ -1,3 +0,0 @@
-wrapperVersion=3.3.4
-distributionType=only-script
-distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.12/apache-maven-3.9.12-bin.zip

+ 23 - 32
pom.xml

@@ -5,52 +5,49 @@
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
-        <version>4.0.2</version>
-        <relativePath/> <!-- lookup parent from repository -->
+        <version>3.2.0</version>
+        <relativePath/>
     </parent>
     <groupId>top.husj</groupId>
     <artifactId>husj-wx</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <name>husj-wx</name>
     <description>husj-wx</description>
-    <url/>
-    <licenses>
-        <license/>
-    </licenses>
-    <developers>
-        <developer/>
-    </developers>
-    <scm>
-        <connection/>
-        <developerConnection/>
-        <tag/>
-        <url/>
-    </scm>
+
     <properties>
         <java.version>21</java.version>
+        <lombok.version>1.18.30</lombok.version>
     </properties>
+
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-webmvc</artifactId>
+            <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <scope>runtime</scope>
-            <optional>true</optional>
-        </dependency>
+        <!-- Lombok 依赖 -->
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
-            <optional>true</optional>
+            <version>${lombok.version}</version> <!-- 添加版本号 -->
+            <scope>provided</scope>
         </dependency>
+
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-webmvc-test</artifactId>
+            <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.83</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-mail</artifactId>
+        </dependency>
     </dependencies>
 
     <build>
@@ -58,11 +55,13 @@
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.11.0</version>
                 <configuration>
                     <annotationProcessorPaths>
                         <path>
                             <groupId>org.projectlombok</groupId>
                             <artifactId>lombok</artifactId>
+                            <version>${lombok.version}</version> <!-- 添加版本号 -->
                         </path>
                     </annotationProcessorPaths>
                 </configuration>
@@ -70,14 +69,6 @@
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
-                <configuration>
-                    <excludes>
-                        <exclude>
-                            <groupId>org.projectlombok</groupId>
-                            <artifactId>lombok</artifactId>
-                        </exclude>
-                    </excludes>
-                </configuration>
             </plugin>
         </plugins>
     </build>

+ 6 - 0
src/main/java/top/husj/husj_wx/Application.java

@@ -2,6 +2,8 @@ package top.husj.husj_wx;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.client.RestTemplate;
 
 @SpringBootApplication
 public class Application {
@@ -10,4 +12,8 @@ public class Application {
         SpringApplication.run(Application.class, args);
     }
 
+    @Bean
+    public RestTemplate restTemplate() {
+        return new RestTemplate();
+    }
 }

+ 51 - 0
src/main/java/top/husj/husj_wx/config/AccessTokenConfig.java

@@ -0,0 +1,51 @@
+package top.husj.husj_wx.config;
+
+import com.alibaba.fastjson.JSONObject;
+import jakarta.annotation.PostConstruct;
+import jakarta.mail.MessagingException;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+import top.husj.husj_wx.utils.AccessTokenUtil;
+import top.husj.husj_wx.utils.EmailUtil;
+
+@Configuration
+@Slf4j
+public class AccessTokenConfig {
+    @Autowired
+    private RestTemplate restTemplate;
+
+    @PostConstruct
+    public void getAccessToken() throws MessagingException {
+        String appId = AccessTokenUtil.AppIp;
+        String appSecret = AccessTokenUtil.AppSecret;
+        try {
+            String url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
+                    appId, appSecret);
+
+            String response = restTemplate.getForObject(url, String.class);
+            JSONObject jsonResponse = JSONObject.parseObject(response);
+
+            if (jsonResponse.containsKey("access_token")) {
+                String accessToken = jsonResponse.getString("access_token");
+                AccessTokenUtil.setAccessToken(accessToken);
+                log.info("成功获取access_token: {}", accessToken.substring(0, 10) + "...");
+            } else {
+                String errorMsg = jsonResponse.getString("errmsg");
+                log.error("获取access_token失败: {}", errorMsg);
+                throw new RuntimeException("获取access_token失败: " + errorMsg);
+            }
+
+        } catch (Exception e) {
+            log.error("获取access_token时发生异常: ", e);
+            String host = "smtp.030208.xyz";
+            String port = "465";
+            String username = "husj@030208.xyz";
+            String password = "15629747218hsjH";
+            EmailUtil emailUtil = new EmailUtil(host, port, username, password);
+            emailUtil.sendTextEmail("807244836@qq.com", "微信公众号token获取失败请检查", "微信公众号token获取失败请检查");
+        }
+    }
+}

+ 29 - 0
src/main/java/top/husj/husj_wx/utils/AccessTokenUtil.java

@@ -0,0 +1,29 @@
+package top.husj.husj_wx.utils;
+
+import java.util.concurrent.locks.ReentrantLock;
+
+public class AccessTokenUtil {
+    private static String AccessToken;
+    public static final String AppIp = "wxb9510ec5ad7b4a5f";
+    public static final String AppSecret = "771278278f2cad8a32615c388a26e215";
+
+    private static ReentrantLock lock = new ReentrantLock();
+
+    public static String getAccessToken() {
+        lock.lock();
+        try {
+            return AccessToken;
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    public static void setAccessToken(String accessToken) {
+        lock.lock();
+        try {
+            AccessToken = accessToken;
+        } finally {
+            lock.unlock();
+        }
+    }
+}

+ 71 - 0
src/main/java/top/husj/husj_wx/utils/EmailUtil.java

@@ -0,0 +1,71 @@
+package top.husj.husj_wx.utils;
+
+
+
+import java.io.UnsupportedEncodingException;
+import java.util.Properties;
+
+import jakarta.mail.Message;
+import jakarta.mail.MessagingException;
+import jakarta.mail.Session;
+import jakarta.mail.Transport;
+import jakarta.mail.Message.RecipientType;
+import jakarta.mail.internet.InternetAddress;
+import jakarta.mail.internet.MimeMessage;
+
+public class EmailUtil {
+    private Session session;
+    private String fromEmail;
+
+    public EmailUtil(String host, String port, String username, String password) {
+        Properties props = new Properties();
+        props.put("mail.smtp.auth", "true");
+        props.put("mail.smtp.starttls.enable", "true");
+        props.put("mail.smtp.host", host);
+        props.put("mail.smtp.port", port);
+        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
+        props.put("mail.smtp.socketFactory.port", port);
+        this.session = Session.getInstance(props, new jakarta.mail.Authenticator() {
+            protected jakarta.mail.PasswordAuthentication getPasswordAuthentication() {
+                return new jakarta.mail.PasswordAuthentication(username, password);
+            }
+        });
+        this.fromEmail = username;
+    }
+
+    public void sendTextEmail(String to, String subject, String text) throws MessagingException {
+        Message message = new MimeMessage(this.session);
+        message.setFrom(new InternetAddress(this.fromEmail));
+        message.setRecipients(RecipientType.TO, InternetAddress.parse(to));
+        message.setSubject(subject);
+        message.setText(text);
+        Transport.send(message);
+    }
+
+    public void sendHtmlEmail(String to, String subject, String htmlContent) throws MessagingException, UnsupportedEncodingException {
+        Message message = new MimeMessage(this.session);
+        message.setFrom(new InternetAddress(this.fromEmail, "发件人名称"));
+        message.setRecipients(RecipientType.TO, InternetAddress.parse(to));
+        message.setSubject(subject);
+        message.setContent(htmlContent, "text/html;charset=UTF-8");
+        Transport.send(message);
+    }
+
+    public static void main(String[] args) {
+        String host = "smtp.030208.xyz";
+        String port = "465";
+        String username = "husj@030208.xyz";
+        String password = "15629747218hsjH";
+        EmailUtil emailUtil = new EmailUtil(host, port, username, password);
+
+        try {
+            emailUtil.sendTextEmail("807244836@qq.com", "测试文本邮件", "这是一封测试邮件。");
+            String html = "<h1>HTML邮件测试</h1><p style='color: red;'>红色文本</p><p><a href='https://example.com'>链接</a></p>";
+            emailUtil.sendHtmlEmail("807244836@qq.com", "测试HTML邮件", html);
+            System.out.println("邮件发送成功!");
+        } catch (Exception var7) {
+            var7.printStackTrace();
+        }
+
+    }
+}