Skip to content

Commit f5273ba

Browse files
committed
添加接口 用于构造第三方使用网站应用授权登录的url
1 parent 4732f21 commit f5273ba

File tree

7 files changed

+210
-1
lines changed

7 files changed

+210
-1
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ public class WxConsts {
164164
* 弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息
165165
*/
166166
public static final String OAUTH2_SCOPE_USER_INFO = "snsapi_userinfo";
167+
168+
/**
169+
* 网页应用登录授权作用域 snsapi_login
170+
*/
171+
public static final String QRCONNECT_SCOPE_SNSAPI_LOGIN = "snsapi_login";
172+
167173
///////////////////////
168174
// 永久素材类型
169175
///////////////////////

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ public interface WxMpService {
152152
*/
153153
WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) throws WxErrorException;
154154

155+
/**
156+
* <pre>
157+
* 构造第三方使用网站应用授权登录的url
158+
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN">网站应用微信登录开发指南</a>
159+
* URL格式为:https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
160+
* </pre>
161+
*
162+
* @param redirectURI 用户授权完成后的重定向链接,无需urlencode, 方法内会进行encode
163+
* @param scope 应用授权作用域,拥有多个作用域用逗号(,)分隔,网页应用目前仅填写snsapi_login即可
164+
* @param state 非必填,用于保持请求和回调的状态,授权请求后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验
165+
* @return url
166+
*/
167+
String buildQrConnectUrl(String redirectURI, String scope, String state);
168+
155169
/**
156170
* <pre>
157171
* 构造oauth2授权的url连接

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,23 @@ public String oauth2buildAuthorizationUrl(String redirectURI, String scope, Stri
282282
return url.toString();
283283
}
284284

285+
@Override
286+
public String buildQrConnectUrl(String redirectURI, String scope,
287+
String state) {
288+
StringBuilder url = new StringBuilder();
289+
url.append("https://open.weixin.qq.com/connect/qrconnect?");
290+
url.append("appid=").append(this.wxMpConfigStorage.getAppId());
291+
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI));
292+
url.append("&response_type=code");
293+
url.append("&scope=").append(scope);
294+
if (state != null) {
295+
url.append("&state=").append(state);
296+
}
297+
298+
url.append("#wechat_redirect");
299+
return url.toString();
300+
}
301+
285302
private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxErrorException {
286303
try {
287304
RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/ApiTestModule.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static class WxXmlMpInMemoryConfigStorage
4444

4545
private String openId;
4646
private String kfAccount;
47+
private String qrconnectRedirectUrl;
4748

4849
public String getOpenId() {
4950
return this.openId;
@@ -66,6 +67,14 @@ public void setKfAccount(String kfAccount) {
6667
this.kfAccount = kfAccount;
6768
}
6869

70+
public String getQrconnectRedirectUrl() {
71+
return this.qrconnectRedirectUrl;
72+
}
73+
74+
public void setQrconnectRedirectUrl(String qrconnectRedirectUrl) {
75+
this.qrconnectRedirectUrl = qrconnectRedirectUrl;
76+
}
77+
6978
}
7079

7180
}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMiscAPITest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class WxMpMiscAPITest {
2323

2424
@Test
2525
public void testGetCallbackIP() throws WxErrorException {
26-
String[] ipArray = wxService.getCallbackIP();
26+
String[] ipArray = this.wxService.getCallbackIP();
2727
System.out.println(Arrays.toString(ipArray));
2828
Assert.assertNotNull(ipArray);
2929
Assert.assertNotEquals(ipArray.length, 0);
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package me.chanjar.weixin.mp.api.impl;
2+
3+
import org.testng.annotations.Guice;
4+
import org.testng.annotations.Test;
5+
6+
import com.google.inject.Inject;
7+
8+
import me.chanjar.weixin.common.api.WxConsts;
9+
import me.chanjar.weixin.mp.api.ApiTestModule;
10+
import me.chanjar.weixin.mp.api.ApiTestModule.WxXmlMpInMemoryConfigStorage;
11+
12+
import org.testng.Assert;
13+
14+
@Test
15+
@Guice(modules = ApiTestModule.class)
16+
public class WxMpServiceImplTest {
17+
18+
@Inject
19+
private WxMpServiceImpl wxService;
20+
21+
@Test
22+
public void testCheckSignature() {
23+
Assert.fail("Not yet implemented");
24+
}
25+
26+
@Test
27+
public void testGetAccessToken() {
28+
Assert.fail("Not yet implemented");
29+
}
30+
31+
@Test
32+
public void testGetAccessTokenBoolean() {
33+
Assert.fail("Not yet implemented");
34+
}
35+
36+
@Test
37+
public void testGetJsapiTicket() {
38+
Assert.fail("Not yet implemented");
39+
}
40+
41+
@Test
42+
public void testGetJsapiTicketBoolean() {
43+
Assert.fail("Not yet implemented");
44+
}
45+
46+
@Test
47+
public void testCreateJsapiSignature() {
48+
Assert.fail("Not yet implemented");
49+
}
50+
51+
@Test
52+
public void testCustomMessageSend() {
53+
Assert.fail("Not yet implemented");
54+
}
55+
56+
@Test
57+
public void testMassNewsUpload() {
58+
Assert.fail("Not yet implemented");
59+
}
60+
61+
@Test
62+
public void testMassVideoUpload() {
63+
Assert.fail("Not yet implemented");
64+
}
65+
66+
@Test
67+
public void testMassGroupMessageSend() {
68+
Assert.fail("Not yet implemented");
69+
}
70+
71+
@Test
72+
public void testMassOpenIdsMessageSend() {
73+
Assert.fail("Not yet implemented");
74+
}
75+
76+
@Test
77+
public void testMassMessagePreview() {
78+
Assert.fail("Not yet implemented");
79+
}
80+
81+
@Test
82+
public void testShortUrl() {
83+
Assert.fail("Not yet implemented");
84+
}
85+
86+
@Test
87+
public void testTemplateSend() {
88+
Assert.fail("Not yet implemented");
89+
}
90+
91+
@Test
92+
public void testSetIndustry() {
93+
Assert.fail("Not yet implemented");
94+
}
95+
96+
@Test
97+
public void testGetIndustry() {
98+
Assert.fail("Not yet implemented");
99+
}
100+
101+
@Test
102+
public void testSemanticQuery() {
103+
Assert.fail("Not yet implemented");
104+
}
105+
106+
@Test
107+
public void testOauth2buildAuthorizationUrl() {
108+
Assert.fail("Not yet implemented");
109+
}
110+
111+
@Test
112+
public void testBuildQrConnectUrl() {
113+
String qrConnectUrl = this.wxService
114+
.buildQrConnectUrl(
115+
((WxXmlMpInMemoryConfigStorage) this.wxService
116+
.getWxMpConfigStorage()).getOauth2redirectUri(),
117+
WxConsts.QRCONNECT_SCOPE_SNSAPI_LOGIN, null);
118+
Assert.assertNotNull(qrConnectUrl);
119+
System.out.println(qrConnectUrl);
120+
}
121+
122+
@Test
123+
public void testOauth2getAccessToken() {
124+
Assert.fail("Not yet implemented");
125+
}
126+
127+
@Test
128+
public void testOauth2refreshAccessToken() {
129+
Assert.fail("Not yet implemented");
130+
}
131+
132+
@Test
133+
public void testOauth2getUserInfo() {
134+
Assert.fail("Not yet implemented");
135+
}
136+
137+
@Test
138+
public void testOauth2validateAccessToken() {
139+
Assert.fail("Not yet implemented");
140+
}
141+
142+
@Test
143+
public void testGetCallbackIP() {
144+
Assert.fail("Not yet implemented");
145+
}
146+
147+
@Test
148+
public void testGet() {
149+
Assert.fail("Not yet implemented");
150+
}
151+
152+
@Test
153+
public void testPost() {
154+
Assert.fail("Not yet implemented");
155+
}
156+
157+
@Test
158+
public void testExecute() {
159+
Assert.fail("Not yet implemented");
160+
}
161+
162+
}

weixin-java-mp/src/test/resources/test-config.sample.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
<expiresTime>可以不填写</expiresTime>
88
<openId>某个加你公众号的用户的openId</openId>
99
<oauth2redirectUri>网页授权获取用户信息回调地址</oauth2redirectUri>
10+
<qrconnectRedirectUrl>网页应用授权登陆回调地址</qrconnectRedirectUrl>
1011
<kfAccount>完整客服账号,格式为:账号前缀@公众号微信号</kfAccount>
1112
</xml>

0 commit comments

Comments
 (0)