mirror of
https://github.com/dromara/payment-spring-boot.git
synced 2026-03-14 05:43:46 +08:00
微信公众号服务号授权优化
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
<groupId>com.enongm.dianji</groupId>
|
<groupId>com.enongm.dianji</groupId>
|
||||||
<artifactId>payment-spring-boot-autoconfigure</artifactId>
|
<artifactId>payment-spring-boot-autoconfigure</artifactId>
|
||||||
<version>1.0.1.RELEASE</version>
|
<version>1.0.0.RELEASE</version>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
@@ -38,7 +38,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.enongm.dianji</groupId>
|
<groupId>com.enongm.dianji</groupId>
|
||||||
<artifactId>payment-spring-boot</artifactId>
|
<artifactId>payment-spring-boot</artifactId>
|
||||||
<version>1.0.1.RELEASE</version>
|
<version>1.0.0.RELEASE</version>
|
||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.enongm.dianji.payment.wechat.oauth2;
|
|||||||
|
|
||||||
|
|
||||||
import com.enongm.dianji.payment.PayException;
|
import com.enongm.dianji.payment.PayException;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.springframework.http.RequestEntity;
|
import org.springframework.http.RequestEntity;
|
||||||
@@ -29,6 +29,7 @@ public class OAuth2AuthorizationRequestRedirectProvider {
|
|||||||
private static final String AUTHORIZATION_URI = "https://open.weixin.qq.com/connect/oauth2/authorize";
|
private static final String AUTHORIZATION_URI = "https://open.weixin.qq.com/connect/oauth2/authorize";
|
||||||
private static final String TOKEN_URI = "https://api.weixin.qq.com/sns/oauth2/access_token";
|
private static final String TOKEN_URI = "https://api.weixin.qq.com/sns/oauth2/access_token";
|
||||||
private final RestOperations restOperations = new RestTemplate();
|
private final RestOperations restOperations = new RestTemplate();
|
||||||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
private final String appId;
|
private final String appId;
|
||||||
private final String secret;
|
private final String secret;
|
||||||
|
|
||||||
@@ -46,12 +47,12 @@ public class OAuth2AuthorizationRequestRedirectProvider {
|
|||||||
/**
|
/**
|
||||||
* 拼接微信公众号授权的url,用以触发用户点击跳转微信授权.
|
* 拼接微信公众号授权的url,用以触发用户点击跳转微信授权.
|
||||||
*
|
*
|
||||||
* @param phoneNumber the phone number
|
* @param state the state
|
||||||
* @param redirectUri the redirect uri
|
* @param redirectUri the redirect uri
|
||||||
* @return uri components
|
* @return uri components
|
||||||
*/
|
*/
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public String redirect(String phoneNumber, String redirectUri) {
|
public String redirect(String state, String redirectUri) {
|
||||||
Assert.hasText(redirectUri, "redirectUri is required");
|
Assert.hasText(redirectUri, "redirectUri is required");
|
||||||
String encode = URLEncoder.encode(redirectUri, StandardCharsets.UTF_8.name());
|
String encode = URLEncoder.encode(redirectUri, StandardCharsets.UTF_8.name());
|
||||||
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
|
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
|
||||||
@@ -59,7 +60,7 @@ public class OAuth2AuthorizationRequestRedirectProvider {
|
|||||||
queryParams.add("redirect_uri", encode);
|
queryParams.add("redirect_uri", encode);
|
||||||
queryParams.add("response_type", "code");
|
queryParams.add("response_type", "code");
|
||||||
queryParams.add("scope", "snsapi_base");
|
queryParams.add("scope", "snsapi_base");
|
||||||
queryParams.add("state", phoneNumber);
|
queryParams.add("state", state);
|
||||||
return UriComponentsBuilder.fromHttpUrl(AUTHORIZATION_URI).queryParams(queryParams).build().toUriString() + "#wechat_redirect";
|
return UriComponentsBuilder.fromHttpUrl(AUTHORIZATION_URI).queryParams(queryParams).build().toUriString() + "#wechat_redirect";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,12 +71,15 @@ public class OAuth2AuthorizationRequestRedirectProvider {
|
|||||||
* @param code the code
|
* @param code the code
|
||||||
* @return the string
|
* @return the string
|
||||||
*/
|
*/
|
||||||
public String openId(String code) {
|
@SneakyThrows
|
||||||
|
public ObjectNode openId(String code, String state) {
|
||||||
Assert.hasText(code, "wechat pay oauth2 code is required");
|
Assert.hasText(code, "wechat pay oauth2 code is required");
|
||||||
|
Assert.hasText(state, "wechat pay oauth2 state is required");
|
||||||
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
|
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
|
||||||
queryParams.add("appid", appId);
|
queryParams.add("appid", appId);
|
||||||
queryParams.add("secret", secret);
|
queryParams.add("secret", secret);
|
||||||
queryParams.add("code", code);
|
queryParams.add("code", code);
|
||||||
|
queryParams.add("state", state);
|
||||||
queryParams.add("grant_type", "authorization_code");
|
queryParams.add("grant_type", "authorization_code");
|
||||||
URI uri = UriComponentsBuilder.fromHttpUrl(TOKEN_URI)
|
URI uri = UriComponentsBuilder.fromHttpUrl(TOKEN_URI)
|
||||||
.queryParams(queryParams)
|
.queryParams(queryParams)
|
||||||
@@ -83,13 +87,13 @@ public class OAuth2AuthorizationRequestRedirectProvider {
|
|||||||
.toUri();
|
.toUri();
|
||||||
|
|
||||||
RequestEntity<Void> requestEntity = RequestEntity.get(uri).build();
|
RequestEntity<Void> requestEntity = RequestEntity.get(uri).build();
|
||||||
ResponseEntity<ObjectNode> responseEntity = restOperations.exchange(requestEntity, ObjectNode.class);
|
ResponseEntity<String> responseEntity = restOperations.exchange(requestEntity, String.class);
|
||||||
ObjectNode body = responseEntity.getBody();
|
|
||||||
|
String body = responseEntity.getBody();
|
||||||
if (Objects.nonNull(body)) {
|
if (Objects.nonNull(body)) {
|
||||||
JsonNode openid = body.get("openid");
|
return objectMapper.readValue(body, ObjectNode.class);
|
||||||
return openid.asText();
|
|
||||||
}
|
}
|
||||||
throw new PayException("OpenId Not Available");
|
throw new PayException("Wechat OAuth2 Authorization failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -44,8 +44,7 @@ public abstract class AbstractApi {
|
|||||||
return RequestEntity.post(uri)
|
return RequestEntity.post(uri)
|
||||||
.body(mapper.writeValueAsString(params));
|
.body(mapper.writeValueAsString(params));
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
e.printStackTrace();
|
throw new PayException("wechat app pay json failed");
|
||||||
}
|
}
|
||||||
throw new PayException("wechat app pay json failed");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
<groupId>com.enongm.dianji</groupId>
|
<groupId>com.enongm.dianji</groupId>
|
||||||
<artifactId>payment-spring-boot-starter</artifactId>
|
<artifactId>payment-spring-boot-starter</artifactId>
|
||||||
<version>1.0.1.RELEASE</version>
|
<version>1.0.0.RELEASE</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.enongm.dianji</groupId>
|
<groupId>com.enongm.dianji</groupId>
|
||||||
<artifactId>payment-spring-boot</artifactId>
|
<artifactId>payment-spring-boot</artifactId>
|
||||||
<version>1.0.1.RELEASE</version>
|
<version>1.0.0.RELEASE</version>
|
||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<groupId>com.enongm.dianji</groupId>
|
<groupId>com.enongm.dianji</groupId>
|
||||||
<artifactId>payment-spring-boot</artifactId>
|
<artifactId>payment-spring-boot</artifactId>
|
||||||
<version>1.0.1.RELEASE</version>
|
<version>1.0.0.RELEASE</version>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user