Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
AuthorizeLogin
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
虞诚毅
AuthorizeLogin
Commits
74e7bafe
Commit
74e7bafe
authored
Apr 09, 2019
by
303514581@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
2018/4/9 wap方式微信公众号授权登陆+用户信息获取接口编写完毕
parent
7aef3230
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
207 additions
and
8 deletions
+207
-8
src/main/java/com/zhiwei/Tool/GetWechatOpenUrl.java
+35
-3
src/main/java/com/zhiwei/config/WechatConfig.java
+50
-0
src/main/java/com/zhiwei/controller/WapWechatLoginController.java
+108
-0
src/main/java/com/zhiwei/controller/WechatLoginController.java
+3
-5
src/main/resources/wechatlogin.properties
+11
-0
No files found.
src/main/java/com/zhiwei/Tool/GetWechatOpenUrl.java
View file @
74e7bafe
...
...
@@ -7,7 +7,6 @@ public class GetWechatOpenUrl {
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
GetWechatOpenUrl
.
class
);
/**
* Step1地址 获取code
*
...
...
@@ -18,7 +17,7 @@ public class GetWechatOpenUrl {
* @param scope
* @return
*/
public
String
getCodeUrl
(
String
getCodeUrl
,
String
appid
,
String
redirect_uri
,
String
respone_type
,
String
scope
)
{
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="
);
...
...
@@ -34,7 +33,7 @@ public class GetWechatOpenUrl {
return
url
.
toString
();
}
public
String
getTokenUrl
(
String
getTokenUrl
,
String
appid
,
String
secret
,
String
code
,
String
grant_type
)
{
public
String
getTokenUrl
(
String
getTokenUrl
,
String
appid
,
String
secret
,
String
code
,
String
grant_type
)
{
StringBuilder
url
=
new
StringBuilder
();
url
.
append
(
getTokenUrl
);
url
.
append
(
"?appid="
);
...
...
@@ -51,6 +50,7 @@ public class GetWechatOpenUrl {
/**
* 获取用户信息url
*
* @Title: getUserInfo
* @Description: 获取用户信息url
* @param @param userInfoUrl
...
...
@@ -69,4 +69,36 @@ public class GetWechatOpenUrl {
logger
.
info
(
"获取用户信息url:"
+
url
);
return
url
.
toString
();
}
/**
* wap公众号授权url
*
* @param wapWR
* @param string
* @Title: getWapLoginUrl
* @Description: wap公众号授权url
* @param @param wapCodeUrl
* @param @param wapAppid
* @param @param wapRedirectUri
* @param @param wapScope
* @param @param wapWR
* @param @return 设定文件
* @return String 返回类型
*/
public
String
getWapLoginUrl
(
String
wapCodeUrl
,
String
wapAppid
,
String
wapRedirectUri
,
String
response_type
,
String
wapScope
,
String
wapWR
)
{
StringBuilder
url
=
new
StringBuilder
();
url
.
append
(
wapCodeUrl
);
url
.
append
(
"?appid="
);
url
.
append
(
wapAppid
);
url
.
append
(
"&redirect_uri="
);
url
.
append
(
wapRedirectUri
);
url
.
append
(
"&response_type="
);
url
.
append
(
response_type
);
url
.
append
(
"&scope="
);
url
.
append
(
wapScope
);
url
.
append
(
wapWR
);
logger
.
info
(
"wap公众号授权url:"
+
url
);
return
url
.
toString
();
}
}
src/main/java/com/zhiwei/config/WechatConfig.java
View file @
74e7bafe
...
...
@@ -30,6 +30,56 @@ public class WechatConfig {
private
String
grantType
;
// 获取用户个人信息(UnionID机制)地址
private
String
userInfoUrl
;
// 微信公众号授权登陆地址
private
String
wapCodeUrl
;
// 微信公众号授权appid
private
String
wapAppid
;
// 微信公众号成功授权后的回调地址,域名必须与审核时填写的授权域名一致
private
String
wapRedirectUri
;
// 微信公众号应用授权作用域
private
String
wapScope
;
// #wechat_redirect无论直接打开还是做页面302重定向时候,必须带此参数
private
String
wapWR
;
public
String
getWapCodeUrl
()
{
return
wapCodeUrl
;
}
public
void
setWapCodeUrl
(
String
wapCodeUrl
)
{
this
.
wapCodeUrl
=
wapCodeUrl
;
}
public
String
getWapAppid
()
{
return
wapAppid
;
}
public
void
setWapAppid
(
String
wapAppid
)
{
this
.
wapAppid
=
wapAppid
;
}
public
String
getWapRedirectUri
()
{
return
wapRedirectUri
;
}
public
void
setWapRedirectUri
(
String
wapRedirectUri
)
{
this
.
wapRedirectUri
=
wapRedirectUri
;
}
public
String
getWapScope
()
{
return
wapScope
;
}
public
void
setWapScope
(
String
wapScope
)
{
this
.
wapScope
=
wapScope
;
}
public
String
getWapWR
()
{
return
wapWR
;
}
public
void
setWapWR
(
String
wapWR
)
{
this
.
wapWR
=
wapWR
;
}
public
String
getUserInfoUrl
()
{
return
userInfoUrl
;
...
...
src/main/java/com/zhiwei/controller/WapWechatLoginController.java
0 → 100644
View file @
74e7bafe
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.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.alibaba.fastjson.JSONObject
;
import
com.zhiwei.Tool.GetWechatOpenUrl
;
import
com.zhiwei.Tool.Tools
;
import
com.zhiwei.config.WechatConfig
;
import
com.zhiwei.entity.WeChatTokenEntity
;
@SuppressWarnings
(
"deprecation"
)
@RestController
public
class
WapWechatLoginController
{
@Autowired
private
WechatConfig
config
;
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
WapWechatLoginController
.
class
);
private
GetWechatOpenUrl
getUrl
=
new
GetWechatOpenUrl
();
private
static
String
yoururl
=
null
;
/**
* 回调接口
*
* @param response
* @param request
*/
@SuppressWarnings
(
"resource"
)
@RequestMapping
(
"/wapwechat/recall"
)
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());
logger
.
info
(
entity
.
toString
());
String
sendurl
=
yoururl
+
"?access_token="
+
entity
.
getAccess_token
()
+
"&openid="
+
entity
.
getOpenid
()
+
"&expires_in="
+
entity
.
getExpires_in
()
+
"&unionid="
+
entity
.
getUnionid
();
response
.
sendRedirect
(
sendurl
);
}
catch
(
IOException
e
)
{
logger
.
error
(
"微信公众号授权请求失败"
,
e
);
}
}
@RequestMapping
(
value
=
"/wapwechat/getUserInfo"
,
method
=
RequestMethod
.
GET
)
public
String
getWechatUserInfo
(
@RequestParam
(
value
=
"access_token"
)
String
access_token
,
@RequestParam
(
value
=
"openid"
)
String
openid
,
HttpServletResponse
response
,
HttpServletRequest
request
)
{
// https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
try
{
String
url
=
getUrl
.
getUserInfo
(
config
.
getUserInfoUrl
(),
access_token
,
openid
);
return
Tools
.
httpGet
(
url
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"微信公众号授权请求用户信息失败"
,
e
);
return
Tools
.
returnCommonError
(
"zhiwei授权接口出错"
);
}
}
@RequestMapping
(
"/wapwechat/login"
)
public
void
wapLogin
(
@RequestParam
(
value
=
"redirect_uri"
)
String
redirect_uri
,
HttpServletResponse
response
,
HttpServletRequest
request
)
{
try
{
yoururl
=
redirect_uri
;
String
url
=
getUrl
.
getWapLoginUrl
(
config
.
getWapCodeUrl
(),
config
.
getWapAppid
(),
config
.
getWapRedirectUri
(),
config
.
getResponseType
(),
config
.
getWapScope
(),
config
.
getWapWR
());
response
.
sendRedirect
(
url
);
}
catch
(
IOException
e
)
{
logger
.
error
(
"微信公众号授权登录请求发送失败"
,
e
);
}
}
}
src/main/java/com/zhiwei/controller/WechatLoginController.java
View file @
74e7bafe
...
...
@@ -25,7 +25,7 @@ import com.zhiwei.Tool.Tools;
import
com.zhiwei.config.WechatConfig
;
import
com.zhiwei.entity.WeChatTokenEntity
;
//微博授权登录
@SuppressWarnings
(
"deprecation"
)
@RestController
public
class
WechatLoginController
{
...
...
@@ -85,7 +85,7 @@ public class WechatLoginController {
logger
.
info
(
entity
.
toString
());
String
sendurl
=
yoururl
+
"?access_token="
+
entity
.
getAccess_token
()
+
"&openid="
+
entity
.
getOpenid
()
+
"&expires_in="
+
entity
.
getExpires_in
()+
"&unionid="
+
entity
.
getUnionid
();
+
"&expires_in="
+
entity
.
getExpires_in
()
+
"&unionid="
+
entity
.
getUnionid
();
response
.
sendRedirect
(
sendurl
);
}
catch
(
IOException
e
)
{
logger
.
error
(
"微信回调请求失败,错误信息:{},错误位置:{}"
,
e
.
getMessage
(),
e
.
getStackTrace
());
...
...
@@ -94,8 +94,7 @@ public class WechatLoginController {
@RequestMapping
(
value
=
"/WechatLogin/getUserInfo"
,
method
=
RequestMethod
.
GET
)
public
String
getWechatUserInfo
(
@RequestParam
(
value
=
"access_token"
)
String
access_token
,
@RequestParam
(
value
=
"openid"
)
String
openid
,
HttpServletResponse
response
,
HttpServletRequest
request
)
{
@RequestParam
(
value
=
"openid"
)
String
openid
,
HttpServletResponse
response
,
HttpServletRequest
request
)
{
// https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
try
{
String
url
=
getUrl
.
getUserInfo
(
config
.
getUserInfoUrl
(),
access_token
,
openid
);
...
...
@@ -106,5 +105,4 @@ public class WechatLoginController {
}
}
}
src/main/resources/wechatlogin.properties
View file @
74e7bafe
...
...
@@ -17,3 +17,13 @@ wechat.getTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token
wechat.grantType
=
authorization_code
#获取用户个人信息(UnionID机制)地址
wechat.userInfoUrl
=
https://api.weixin.qq.com/sns/userinfo
#微信公众号授权登陆地址
wechat.wapCodeUrl
=
https://open.weixin.qq.com/connect/oauth2/authorize
#微信公众号授权appid
wechat.wapAppid
=
wxd1f40dde7e15845f
#微信公众号成功授权后的回调地址,域名必须与审核时填写的授权域名一致
wechat.wapRedirectUri
=
http://login.zhiweidata.com/wapwechat/recall
#微信公众号应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
wechat.wapScope
=
snsapi_userinfo
#微信公众号#wechat_redirect无论直接打开还是做页面302重定向时候,必须带此参数
wechat.wapWR
=
#wechat_redirect
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment