Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
cloud-platform
Project
Project
Details
Activity
Releases
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
youjj
cloud-platform
Commits
55c2c2d5
Commit
55c2c2d5
authored
Sep 19, 2019
by
hezhen
Committed by
libin
Sep 29, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
123
parent
6582379b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
49 deletions
+101
-49
WeixinController.java
...com/xxfc/platform/summit/controller/WeixinController.java
+94
-0
WeChatH5LoginInterceoptor.java
...latform/summit/interceptor/WeChatH5LoginInterceoptor.java
+7
-49
No files found.
xx-summit/xx-summit-server/src/main/java/com/xxfc/platform/summit/controller/WeixinController.java
0 → 100644
View file @
55c2c2d5
package
com
.
xxfc
.
platform
.
summit
.
controller
;
import
cn.hutool.core.codec.Base64
;
import
com.alibaba.fastjson.JSONObject
;
import
com.github.wxiaoqi.security.auth.client.annotation.IgnoreUserToken
;
import
com.github.wxiaoqi.security.common.msg.ObjectRestResponse
;
import
com.xxfc.platform.summit.service.WeixinService
;
import
com.xxfc.platform.summit.vo.UserInfo
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpSession
;
/**
* @author Administrator
*/
@Controller
@RequestMapping
(
"/auth"
)
@Slf4j
public
class
WeixinController
{
public
static
final
String
WECHAT_AUTOLOGIN_CALLBACKURL_KEY
=
"callback"
;
@Autowired
WeixinService
weixinService
;
public
static
final
String
frontSessionKey
=
"frontWeixKey"
;
@Value
(
"${wx.url}"
)
private
String
url
;
@RequestMapping
(
value
=
"/app/unauth/wxLogin"
,
method
=
RequestMethod
.
GET
)
@IgnoreUserToken
public
String
index
(
@RequestParam
(
value
=
"redirec_url"
)
String
redirec_url
){
log
.
info
(
"-----微信wxLogin---redirec_url=="
+
redirec_url
);
if
(
StringUtils
.
isBlank
(
redirec_url
)){
redirec_url
=
""
;
}
String
encrypt_curr_url
=
Base64
.
encode
(
redirec_url
);
redirec_url
=
url
+
"?"
+
WECHAT_AUTOLOGIN_CALLBACKURL_KEY
+
"="
+
encrypt_curr_url
;
String
oauth_api
=
weixinService
.
getAuthorize
(
redirec_url
);
return
String
.
format
(
"redirect%s"
,
oauth_api
);
}
/**
* 微信浏览器获取用户信息
* @param code
* @param callback
* @return
*/
@GetMapping
(
value
=
"/userInfo"
)
public
String
getUserInformation
(
String
code
,
String
callback
,
HttpServletRequest
request
)
{
log
.
info
(
"-----微信回调userInfo---code=="
+
code
+
"----redirec_url==="
+
callback
);
if
(
StringUtils
.
isNotBlank
(
code
)){
log
.
info
(
"----code为空---"
);
return
String
.
format
(
"code为空"
);
}
JSONObject
jsonData
=
weixinService
.
getAccessToken
(
code
);
String
openid
=
jsonData
.
getString
(
"openid"
);
String
access_token
=
jsonData
.
getString
(
"access_token"
);
String
refresh_token
=
jsonData
.
getString
(
"refresh_token"
);
log
.
info
(
"-----微信回调userInfo---openid=="
+
openid
+
"----access_token==="
+
access_token
);
HttpSession
session
=
request
.
getSession
();
//验证access_token是否失效
JSONObject
validateData
=
weixinService
.
getValidateData
(
access_token
,
openid
);
if
(!
"0"
.
equals
(
validateData
.
getString
(
"errcode"
))){
//刷新access_token
JSONObject
refreshData
=
weixinService
.
getRefreshToken
(
refresh_token
);
access_token
=
refreshData
.
getString
(
"access_token"
);
}
UserInfo
userInfo
=
new
UserInfo
();
userInfo
.
setOpenId
(
openid
);
log
.
error
(
"UserInfo==="
+
JSONObject
.
toJSONString
(
userInfo
));
String
encode
=
Base64
.
encode
(
JSONObject
.
toJSONString
(
userInfo
));
session
.
removeAttribute
(
frontSessionKey
);
session
.
setAttribute
(
frontSessionKey
,
encode
);
callback
=
Base64
.
encode
(
callback
);
log
.
error
(
"callback==="
+
callback
);
return
String
.
format
(
"redirect%s"
,
callback
);
}
}
xx-summit/xx-summit-server/src/main/java/com/xxfc/platform/summit/interceptor/WeChatH5LoginInterceoptor.java
View file @
55c2c2d5
...
...
@@ -6,6 +6,7 @@ import javax.servlet.http.HttpServletResponse;
import
javax.servlet.http.HttpSession
;
import
cn.hutool.core.codec.Base64
;
import
com.alibaba.fastjson.JSON
;
import
com.github.wxiaoqi.security.common.msg.ObjectRestResponse
;
import
com.github.wxiaoqi.security.common.util.UserAgentUtil
;
import
com.xxfc.platform.summit.service.WeixinService
;
import
com.xxfc.platform.summit.vo.UserInfo
;
...
...
@@ -16,6 +17,8 @@ import org.springframework.beans.factory.annotation.Value;
import
org.springframework.web.servlet.handler.HandlerInterceptorAdapter
;
import
com.alibaba.fastjson.JSONObject
;
import
java.io.PrintWriter
;
/**
* 微信登陆拦截器
...
...
@@ -29,13 +32,8 @@ public class WeChatH5LoginInterceoptor extends HandlerInterceptorAdapter {
/**
* 微信公众号自动登陆令牌的url参数名
*/
public
static
final
String
WECHAT_AUTOLOGIN_CALLBACKURL_KEY
=
"wechat_autologin_callback_accesstoken"
;
public
static
final
String
frontSessionKey
=
"frontWeixKey"
;
@Value
(
"${wx.url}"
)
private
String
url
;
@Autowired
WeixinService
weixinService
;
...
...
@@ -53,54 +51,14 @@ public class WeChatH5LoginInterceoptor extends HandlerInterceptorAdapter {
if
(
isWx
)
{
String
frontSessionValue1
=
(
String
)
session
.
getAttribute
(
frontSessionKey
);
if
(
StringUtils
.
isNotBlank
(
frontSessionValue1
))
{
String
frontSessionValue
=
new
String
(
Base64
.
decode
(
frontSessionValue1
),
"utf-8"
);
return
true
;
}
/*String curr_url = request.getRequestURL().toString()
+ (StringUtils.isBlank(request.getQueryString()) ? "" : "?" + request.getQueryString());*/
String
curr_url
=
"https://dev.dfangche.com/api/summit/activity/app/unauth/index"
;
String
encrypt_curr_url
=
Base64
.
encode
(
curr_url
);
String
code
=
request
.
getParameter
(
"code"
);
// 没有code, 则进行网页授权获取code
log
.
info
(
"curr_url====="
+
curr_url
+
"-----code="
+
code
);
if
(
StringUtils
.
isBlank
(
code
))
{
String
redirec_url
=
curr_url
+
"?"
+
WECHAT_AUTOLOGIN_CALLBACKURL_KEY
+
"="
+
encrypt_curr_url
;
String
oauth_api
=
weixinService
.
getAuthorize
(
curr_url
);
log
.
info
(
"curr_url====="
+
curr_url
);
response
.
sendRedirect
(
"https://www.baidu.com"
);
return
false
;
}
// 有code, 换取openid
String
openid
=
null
;
String
access_token
=
null
;
try
{
log
.
info
(
"调用微信网页授权接口code="
+
code
);
JSONObject
access_token_json
=
weixinService
.
getAccessToken
(
code
);
if
(
access_token_json
==
null
||
StringUtils
.
isNotBlank
(
access_token_json
.
getString
(
"errcode"
)))
{
log
.
info
(
"err: "
+
JSON
.
toJSONString
(
access_token_json
));
log
.
info
(
"调用微信网页授权接口失败, appid或者appsecret不正确"
);
JSONObject
json
=
new
JSONObject
();
json
.
put
(
"status"
,
1001
);
json
.
put
(
"data"
,
"https://dev.dfangche.com/api/summit/auth/app/unauth/wxLogin?redirec_url=https://dev.dfangche.com/h5/appHtml/view/wxh5/index.html"
);
response
.
getWriter
().
write
(
json
.
toJSONString
());
return
false
;
}
openid
=
access_token_json
.
getString
(
"openid"
).
trim
();
access_token
=
access_token_json
.
getString
(
"access_token"
);
UserInfo
userInfo
=
new
UserInfo
();
userInfo
.
setOpenId
(
openid
);
log
.
error
(
"UserInfo==="
+
JSONObject
.
toJSONString
(
userInfo
));
String
encode
=
Base64
.
encode
(
JSONObject
.
toJSONString
(
userInfo
));
session
.
removeAttribute
(
frontSessionKey
);
session
.
setAttribute
(
frontSessionKey
,
encode
);
}
catch
(
Exception
e
)
{
log
.
info
(
"【"
+
curr_url
+
"】获取access_token失败"
);
return
false
;
}
// 重定向到原来地址后进行自动登陆
String
encrypt_callbackurl
=
request
.
getParameter
(
WECHAT_AUTOLOGIN_CALLBACKURL_KEY
);
String
decrypt_callbackurl
=
Base64
.
encode
(
encrypt_callbackurl
.
getBytes
(
"utf-8"
));
log
.
error
(
"decrypt_callbackurl==="
+
decrypt_callbackurl
);
response
.
sendRedirect
(
decrypt_callbackurl
);
return
false
;
}
return
true
;
}
...
...
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