前提:有些需求需要用到微信用戶的openID的時候,而又不想用戶進行確認授權的時候,就可以進行默認授權
一、微信默認授權
1.1設置網頁授權域名--(公衆號設置-功能設置-網頁授權域名)
1.2.獲取APPID和secret
1.3 默認授權頁面
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
</head>
<body>
<script th:inline="javascript">
window.onload = function () {
window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + [[${appId}]] + "&redirect_uri=" + [[${redirectUri}]] + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
}
</script>
</body>
</html>
1.4 java代碼
private String wxAppId = "wxAppId";
private String secret = "secret";
/**
* 跳轉授權頁
**/
@RequestMapping("wxAuth")
public String wxAuth(Model model) {
model.addAttribute("appId", wxAppId);
model.addAttribute("redirectUri", "http://hjy.77lemon.top/getWxOpenId?param=xxx");
return "auth/wx-auth";
}
/**
* 微信授權完成後地址
**/
@ResponseBody
@RequestMapping(value = "getWxOpenId")
public String getWxOpenId(HttpServletRequest request, String param) throws Exception {
String code = request.getParameter("code");
HttpClient client = new DefaultHttpClient();
String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wxAppId + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
HttpGet httpget = new HttpGet(requestUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = client.execute(httpget, responseHandler);
String wxOpenId = (String) JSON.parseObject(response).get("openid");
System.out.println("wxOpenId:" + wxOpenId);
System.out.println("自定義傳回的參數:" + param);
return wxOpenId;
}
二、支付寶授權
1.在應用裏添加獲取會員功能
2.設置公私鑰
3.授權頁面
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
</head>
<body>
<script th:inline="javascript">
window.onload = function () {
window.location.href = "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=" + [[${appId}]] + "&scope=auth_base&redirect_uri=" + [[${redirectUri}]];
}
</script>
</body>
</html>
4.java代碼
private static String aliAppId = "aliAppId";
@RequestMapping("aliAuth")
public String aliAuth(Model model) {
model.addAttribute("appId", aliAppId);
model.addAttribute("redirectUri", "http://hjy.77lemon.top/getAliUserId?param=xxx");
return "auth/ali-auth";
}
@ResponseBody
@RequestMapping(value = "getAliUserId")
public String getAliUserId(HttpServletRequest request, String param) throws Exception {
String authCode = request.getParameter("auth_code");
AlipaySystemOauthTokenRequest oauthTokenRequest = new AlipaySystemOauthTokenRequest();
oauthTokenRequest.setCode(authCode);
oauthTokenRequest.setGrantType("authorization_code");
AlipayClient alipayClient = getClient();
AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(oauthTokenRequest);
String userId = oauthTokenResponse.getUserId();
System.out.println("userId:" + userId);
System.out.println("自定義傳回的參數:" + param);
return userId;
}
private static AlipayClient alipayClient;
//私鑰
public static String PRIVATE_KEY = "PRIVATE_KEY";
public static AlipayClient getClient() {
if (null == alipayClient) {
alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppId,
PRIVATE_KEY, "json", "GBK", "", "RSA2");
}
return alipayClient;
}
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.0.0</version>
</dependency>