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
a2e50818
Commit
a2e50818
authored
Apr 08, 2019
by
303514581@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
2019/4/8 新增获取wechat用户信息接口
parent
837524b6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
219 additions
and
5 deletions
+219
-5
pom.xml
+36
-1
src/main/java/com/zhiwei/Tool/GetWechatOpenUrl.java
+21
-0
src/main/java/com/zhiwei/Tool/Tools.java
+125
-0
src/main/java/com/zhiwei/config/WechatConfig.java
+10
-0
src/main/java/com/zhiwei/controller/WechatLoginController.java
+24
-4
src/main/resources/wechatlogin.properties
+3
-0
No files found.
pom.xml
View file @
a2e50818
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
...
...
@@ -115,6 +116,40 @@
<artifactId>
spring-boot-configuration-processor
</artifactId>
<optional>
true
</optional>
</dependency>
<!-- crawler工具包 -->
<dependency>
<groupId>
com.zhiwei.crawler
</groupId>
<artifactId>
crawler-core
</artifactId>
<version>
0.3.5-SNAPSHOT
</version>
</dependency>
<!-- 日志 -->
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-core
</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-api
</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-1.2-api -->
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-1.2-api
</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
<groupId>
org.apache.logging.log4j
</groupId>
<artifactId>
log4j-slf4j-impl
</artifactId>
<exclusions>
<exclusion>
<groupId>
org.slf4j
</groupId>
<artifactId>
slf4j-api
</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
...
...
src/main/java/com/zhiwei/Tool/GetWechatOpenUrl.java
View file @
a2e50818
...
...
@@ -48,4 +48,25 @@ public class GetWechatOpenUrl {
logger
.
info
(
"请求token的url:"
+
url
);
return
url
.
toString
();
}
/**
* 获取用户信息url
* @Title: getUserInfo
* @Description: 获取用户信息url
* @param @param userInfoUrl
* @param @param access_token
* @param @param openid
* @param @return 设定文件
* @return String 返回类型
*/
public
String
getUserInfo
(
String
userInfoUrl
,
String
access_token
,
String
openid
)
{
StringBuilder
url
=
new
StringBuilder
();
url
.
append
(
userInfoUrl
);
url
.
append
(
"?access_token="
);
url
.
append
(
access_token
);
url
.
append
(
"&openid="
);
url
.
append
(
openid
);
logger
.
info
(
"获取用户信息url:"
+
url
);
return
url
.
toString
();
}
}
src/main/java/com/zhiwei/Tool/Tools.java
0 → 100644
View file @
a2e50818
package
com
.
zhiwei
.
Tool
;
import
java.util.HashMap
;
import
java.util.Map
;
import
com.alibaba.fastjson.JSONObject
;
import
com.zhiwei.crawler.core.HttpBoot
;
import
com.zhiwei.crawler.utils.RequestUtils
;
import
com.zhiwei.crawler.utils.RequestUtils.HttpMethod
;
import
okhttp3.Request
;
import
okhttp3.Response
;
/**
* 自用工具类
* @ClassName: Tools
* @Description: 自用工具类
* @author shentao
* @date 2019年4月8日 上午10:20:19
*/
public
class
Tools
{
/** HttpBoot **/
private
static
HttpBoot
httpBoot
=
new
HttpBoot
.
Builder
().
build
();
/**
* get请求
*
* @Title: httpGet
* @Description: get请求
* @param @param url
* @param @return 设定文件
* @return String 返回类型
*/
public
static
String
httpGet
(
String
url
)
{
Request
request
=
RequestUtils
.
wrapGet
(
url
);
try
(
Response
response
=
httpBoot
.
syncCall
(
request
))
{
return
response
.
body
().
string
();
}
catch
(
Exception
e
)
{
return
null
;
}
}
/**
* post form请求
*
* @Title: httpPostForm
* @Description: post form请求
* @param @param urlNewProject
* @param @param form
* @param @return 设定文件
* @return String 返回类型
*/
public
static
String
httpPostForm
(
String
url
,
Map
<
String
,
Object
>
form
)
{
Request
request
=
RequestUtils
.
wrapPost
(
url
,
new
HashMap
<>(),
form
);
try
(
Response
response
=
httpBoot
.
syncCall
(
request
))
{
return
response
.
body
().
string
();
}
catch
(
Exception
e
)
{
return
null
;
}
}
/**
* post RequestBody请求
*
* @Title: httpPostRequestBody
* @Description: post RequestBody请求
* @param @param url
* @param @param info
* @param @return 设定文件
* @return String 返回类型
*/
public
static
String
httpPostRequestBody
(
String
url
,
String
info
)
{
Request
request
=
RequestUtils
.
wrapPost
(
url
,
"application/json"
,
info
);
try
(
Response
response
=
httpBoot
.
syncCall
(
request
))
{
return
response
.
body
().
string
();
}
catch
(
Exception
e
)
{
return
null
;
}
}
/**
* delete 请求
*
* @Title: httpDelete
* @Description: delete 请求
* @param @param url
* @param @param info
* @param @return 设定文件
* @return String 返回类型
*/
public
static
String
httpDelete
(
String
url
,
String
info
)
{
Request
request
=
RequestUtils
.
wrap
(
HttpMethod
.
DELETE
,
url
,
"application/json"
,
info
);
try
(
Response
response
=
httpBoot
.
syncCall
(
request
))
{
return
response
.
body
().
string
();
}
catch
(
Exception
e
)
{
return
null
;
}
}
/**
* put 请求
*
* @Title: httpPut
* @Description: put 请求
* @param @param url
* @param @param info
* @param @return 设定文件
* @return String 返回类型
*/
public
static
String
httpPut
(
String
url
,
String
info
)
{
Request
request
=
RequestUtils
.
wrap
(
HttpMethod
.
PUT
,
url
,
"application/json"
,
info
);
try
(
Response
response
=
httpBoot
.
syncCall
(
request
))
{
return
response
.
body
().
string
();
}
catch
(
Exception
e
)
{
return
null
;
}
}
public
static
String
returnCommonError
(
String
errormsg
)
{
JSONObject
error
=
new
JSONObject
();
error
.
put
(
"errcode"
,
-
2
);
error
.
put
(
"errmsg"
,
errormsg
);
return
error
.
toJSONString
();
}
}
src/main/java/com/zhiwei/config/WechatConfig.java
View file @
a2e50818
...
...
@@ -28,6 +28,16 @@ public class WechatConfig {
private
String
getTokenUrl
;
// 授权类型
private
String
grantType
;
// 获取用户个人信息(UnionID机制)地址
private
String
userInfoUrl
;
public
String
getUserInfoUrl
()
{
return
userInfoUrl
;
}
public
void
setUserInfoUrl
(
String
userInfoUrl
)
{
this
.
userInfoUrl
=
userInfoUrl
;
}
public
String
getGetCodeUrl
()
{
return
getCodeUrl
;
...
...
src/main/java/com/zhiwei/controller/WechatLoginController.java
View file @
a2e50818
...
...
@@ -15,10 +15,13 @@ 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
;
...
...
@@ -76,15 +79,32 @@ public class WechatLoginController {
if
(
httpresponse
.
getStatusLine
().
getStatusCode
()
==
HttpStatus
.
SC_OK
)
{
resultToken
=
EntityUtils
.
toString
(
httpresponse
.
getEntity
(),
"utf-8"
);
}
System
.
out
.
println
(
resultToken
);
//
System.out.println(resultToken);
WeChatTokenEntity
entity
=
JSONObject
.
parseObject
(
resultToken
,
WeChatTokenEntity
.
class
);
System
.
err
.
println
(
entity
.
toString
());
// System.err.println(entity.toString());
logger
.
info
(
entity
.
toString
());
String
sendurl
=
yoururl
+
"?
token="
+
entity
.
getAccess_token
()
+
"&u
id="
+
entity
.
getOpenid
()
+
"&expires_in="
+
entity
.
getExpires_in
();
String
sendurl
=
yoururl
+
"?
access_token="
+
entity
.
getAccess_token
()
+
"&open
id="
+
entity
.
getOpenid
()
+
"&expires_in="
+
entity
.
getExpires_in
()
+
"&unionid="
+
entity
.
getUnionid
()
;
response
.
sendRedirect
(
sendurl
);
}
catch
(
IOException
e
)
{
logger
.
error
(
"微信回调请求失败,错误信息:{},错误位置:{}"
,
e
.
getMessage
(),
e
.
getStackTrace
());
}
}
@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
)
{
// 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
.
getMessage
(),
e
.
getStackTrace
());
return
Tools
.
returnCommonError
(
"zhiwei授权接口出错"
);
}
}
}
src/main/resources/wechatlogin.properties
View file @
a2e50818
...
...
@@ -15,3 +15,5 @@ wechat.scope=snsapi_login
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
\ 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