Commit c0cb1a80 by win7

添加微信授权登录组件,更新ReadMe.md

parent 1d6ae3f7
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
--- ---
* **项目地址**>http://192.168.0.36:9090/ycy/AuthorizeLoginplugin.git * **项目地址**>http://192.168.0.36:9090/ycy/AuthorizeLogin.git
--- ---
...@@ -34,9 +34,23 @@ ...@@ -34,9 +34,23 @@
:----|:----|:---- :----|:----|:----
token|string|(QQAPI中的access_token)用户授权的唯一票据,授权令牌 token|string|(QQAPI中的access_token)用户授权的唯一票据,授权令牌
uid|string|(QQAPI中的openid)是此网站上唯一对应用户身份的标识 uid|string|(QQAPI中的openid)是此网站上唯一对应用户身份的标识
expires_in|int|token有效时间(fen单位:分)
- 微信(未实现,缺少开发者权限)
--- - 微信>http://login.zhiweidata.com/WechatLogin
请求参数|必须|类型|说明
:----|:----|:----|:----
yoururl|true|string|授权登录后携带token及uid 或 授权登录取消后携带error_code返回的地址
返回数据|类型|说明
:----|:----|:----
token|string|(网站应用微信登录开发指南中的access_token)用户授权的唯一票据,授权令牌
uid|string|网站应用微信登录开发指南中的openid)是此网站上唯一对应用户身份的标识
expires_in|int|token有效时间(fen单位:分)
---
+ **yoururl如何获取到登录信息?** + **yoururl如何获取到登录信息?**
- 登录完毕以后会跳转到yoururl?token=xxxx&uid=xxxxx - 微博
\ No newline at end of file - 登录完毕以后会跳转到yoururl?token=xxxx&uid=xxxx
- QQ及微信
- 登录完成后跳转至yoururl?token=xxxx&uid=xxxx&expires_in=xxxx
\ No newline at end of file
package com.zhiwei.Tool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GetWechatOpenUrl {
private static final Logger logger = LoggerFactory.getLogger(GetWechatOpenUrl.class);
/**
* Step1地址 获取code
*
* @param getCodeUrl
* @param appid
* @param redirect_uri
* @param respone_type
* @param scope
* @return
*/
public String getCodeUrl(String getCodeUrl,String appid,String redirect_uri,String respone_type,String scope) {
StringBuilder url = new StringBuilder();
url.append(getCodeUrl);
url.append("?appid=");
url.append(appid);
url.append("&redirect_uri=");
url.append(redirect_uri);
url.append("&response_type=");
url.append(respone_type);
url.append("&scope=");
url.append(scope);
url.append("#wechat_redirect");
logger.info("请求授权登录url:" + url);
return url.toString();
}
public String getTokenUrl(String getTokenUrl,String appid,String secret,String code,String grant_type) {
StringBuilder url = new StringBuilder();
url.append(getTokenUrl);
url.append("?appid=");
url.append(appid);
url.append("&secret=");
url.append(secret);
url.append("&code=");
url.append(code);
url.append("&grant_type=");
url.append(grant_type);
logger.info("请求token的url:" + url);
return url.toString();
}
}
package com.zhiwei.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
//微信配置类
@Component
@Configuration
@ConfigurationProperties(prefix = "wechat")
@PropertySource(value = "wechatlogin.properties")
public class WechatConfig {
// 登录地址
private String getCodeUrl;
// 授权类型,值固定为"code"
private String responseType;
// 应用唯一标识
private String appid;
// 应用密钥AppSecret,在微信开放平台提交应用审核通过后获得
private String secret;
// 成功授权后的回调地址,域名必须与审核时填写的授权域名一致
private String redirectUri;
// 应用授权作用域
private String scope;
// 获取token地址
private String getTokenUrl;
// 授权类型
private String grantType;
public String getGetCodeUrl() {
return getCodeUrl;
}
public void setGetCodeUrl(String getCodeUrl) {
this.getCodeUrl = getCodeUrl;
}
public String getResponseType() {
return responseType;
}
public void setResponseType(String responseType) {
this.responseType = responseType;
}
public String getRedirectUri() {
return redirectUri;
}
public void setRedirectUri(String redirectUri) {
this.redirectUri = redirectUri;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
public String getGetTokenUrl() {
return getTokenUrl;
}
public void setGetTokenUrl(String getTokenUrl) {
this.getTokenUrl = getTokenUrl;
}
public String getGrantType() {
return grantType;
}
public void setGrantType(String grantType) {
this.grantType = grantType;
}
}
...@@ -120,7 +120,8 @@ public class QQLoginController { ...@@ -120,7 +120,8 @@ public class QQLoginController {
openid = resultOpenID.substring(start + 9, end - 1); openid = resultOpenID.substring(start + 9, end - 1);
} }
String sendurl = yoururl + "?token=" + token.getAccess_token() + "&uid=" + openid; String sendurl = yoururl + "?token=" + token.getAccess_token() + "&uid=" + openid + "&expires_in"
+ token.getExpires_in();
response.sendRedirect(sendurl); response.sendRedirect(sendurl);
} catch (Exception e) { } catch (Exception e) {
......
package com.zhiwei.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.Tool.GetWechatOpenUrl;
import com.zhiwei.config.WechatConfig;
import com.zhiwei.entity.WeChatTokenEntity;
//微博授权登录
@SuppressWarnings("deprecation")
@RestController
public class WechatLoginController {
@Autowired
private WechatConfig config;
private static final Logger logger = LoggerFactory.getLogger(WechatLoginController.class);
private GetWechatOpenUrl getUrl = new GetWechatOpenUrl();
private static String yoururl = null;
@RequestMapping("/WechatLogin")
public void Login(HttpServletResponse response, HttpServletRequest request) {
yoururl = request.getParameter("yoururl");
String url = getUrl.getCodeUrl(config.getGetCodeUrl(), config.getAppid(), config.getRedirectUri(),
config.getResponseType(), config.getScope());
try {
response.sendRedirect(url);
} catch (IOException e) {
logger.error("微信授权登录请求发送失败,错误信息:{},错误位置:{}", e.getMessage(), e.getStackTrace());
}
}
/**
* 回调接口
*
* @param response
* @param request
*/
@SuppressWarnings("resource")
@RequestMapping("/WechatRecall")
public void Recall(HttpServletResponse response, HttpServletRequest request) {
// 取得code
String code = request.getParameter("code");
// 获取token的地址
String url = null;
try {
url = getUrl.getTokenUrl(config.getGetTokenUrl(), config.getAppid(), config.getSecret(), code,
config.getGrantType());
String resultToken = "";
// 根据地址获取请求
HttpGet httpGet = new HttpGet(url);// 这里发送get请求
// 获取当前客户端对象
HttpClient httpClient = new DefaultHttpClient();
// 通过请求对象获取响应对象
HttpResponse httpresponse;
httpresponse = httpClient.execute(httpGet);
// 判断网络连接状态码是否正常(0--200都数正常)
if (httpresponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
resultToken = EntityUtils.toString(httpresponse.getEntity(), "utf-8");
}
System.out.println(resultToken);
WeChatTokenEntity entity = JSONObject.parseObject(resultToken, WeChatTokenEntity.class);
System.err.println(entity.toString());
String sendurl = yoururl + "?token=" + entity.getAccess_token() + "&uid=" + entity.getOpenid()
+ "&expires_in" + entity.getExpires_in();
response.sendRedirect(sendurl);
} catch (IOException e) {
logger.error("微信回调请求失败,错误信息:{},错误位置:{}", e.getMessage(), e.getStackTrace());
}
}
}
package com.zhiwei.entity;
public class WeChatTokenEntity {
private String access_token;
private int expires_in;
private String refresh_token;
private String openid;
private String scope;
private String unionid;
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
public int getExpires_in() {
return expires_in;
}
public void setExpires_in(int expires_in) {
this.expires_in = expires_in;
}
public String getRefresh_token() {
return refresh_token;
}
public void setRefresh_token(String refresh_token) {
this.refresh_token = refresh_token;
}
public String getOpenid() {
return openid;
}
public void setOpenid(String openid) {
this.openid = openid;
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getUnionid() {
return unionid;
}
public void setUnionid(String unionid) {
this.unionid = unionid;
}
@Override
public String toString() {
return "WeChatTokenEntity [access_token=" + access_token + ", expires_in=" + expires_in + ", refresh_token="
+ refresh_token + ", openid=" + openid + ", scope=" + scope + ", unionid=" + unionid + "]";
}
}
#"_"连接改为首字母大写
#登录地址
wechat.getCodeUrl=https://open.weixin.qq.com/connect/qrconnect
#微信应用唯一标识
wechat.appid=wxb8da90b098af725f
#微信应用密钥AppSecret,在微信开放平台提交应用审核通过后获得
wechat.secret=579a98b20f381300026d007f198d5c18
#微信成功授权后的回调地址,域名必须与审核时填写的授权域名一致
wechat.redirectUri =http://login.zhiweidata.com/WechatRecall
#返回类型
wechat.responseType=code
#微信应用授权作用域,网页应用目前仅填写snsapi_login
wechat.scope=snsapi_login
#获取token地址
wechat.getTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token
#授权类型
wechat.grantType=authorization_code
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment