|
@@ -12,8 +12,12 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.PrintWriter;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.util.Arrays;
|
|
|
|
|
|
@RestController
|
|
|
@CatchAndLog
|
|
@@ -25,6 +29,8 @@ public class WechatController {
|
|
|
@Autowired
|
|
|
private UserGateway userGateway;
|
|
|
|
|
|
+ private final static String TOKEN = "lnServer";
|
|
|
+
|
|
|
@GetMapping("/authUrl")
|
|
|
@Operation(summary = "自动跳转微信授权")
|
|
|
public ApiResult<Void> authUrl(HttpServletResponse response) throws IOException {
|
|
@@ -44,4 +50,47 @@ public class WechatController {
|
|
|
response.sendRedirect(welcomeUrl);
|
|
|
return ApiResult.success();
|
|
|
}
|
|
|
+
|
|
|
+ @GetMapping("/serverCheck")
|
|
|
+ @Operation(summary = "公众号发信息接口")
|
|
|
+ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
+ String signature = request.getParameter("signature");
|
|
|
+ String timestamp = request.getParameter("timestamp");
|
|
|
+ String nonce = request.getParameter("nonce");
|
|
|
+ String echostr = request.getParameter("echostr");
|
|
|
+
|
|
|
+ String signatureCheck = getSHA1(TOKEN, timestamp, nonce);
|
|
|
+ if (signatureCheck != null && signatureCheck.equals(signature)) {
|
|
|
+ PrintWriter out = response.getWriter();
|
|
|
+ out.print(echostr);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getSHA1(String token, String timestamp, String nonce) {
|
|
|
+ try {
|
|
|
+ String[] array = new String[]{token, timestamp, nonce};
|
|
|
+ Arrays.sort(array);
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (String s : array) {
|
|
|
+ sb.append(s);
|
|
|
+ }
|
|
|
+ MessageDigest md = MessageDigest.getInstance("SHA-1");
|
|
|
+ md.update(sb.toString().getBytes());
|
|
|
+ byte[] digest = md.digest();
|
|
|
+ StringBuilder hexstr = new StringBuilder();
|
|
|
+ for (byte b : digest) {
|
|
|
+ String shaHex = Integer.toHexString(b & 0xFF);
|
|
|
+ if (shaHex.length() < 2) {
|
|
|
+ hexstr.append(0);
|
|
|
+ }
|
|
|
+ hexstr.append(shaHex);
|
|
|
+ }
|
|
|
+ return hexstr.toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|