Преглед изворни кода

[微信第三方登录][修改]:微信第三方登录

release_toaliyun_real
Stormeye Wu пре 7 година
родитељ
комит
4ed23c8019
2 измењених фајлова са 54 додато и 50 уклоњено
  1. +2
    -1
      mallinkAdmin/src/main/java/com/iformall/config/ShiroConfig.java
  2. +52
    -49
      mallinkAdmin/src/main/java/com/iformall/controller/WechatLoginController.java

+ 2
- 1
mallinkAdmin/src/main/java/com/iformall/config/ShiroConfig.java Прегледај датотеку

@@ -99,7 +99,8 @@ public class ShiroConfig {
// login // login
filterChainDefinitionMap.put("/doLogin/**", "anon"); filterChainDefinitionMap.put("/doLogin/**", "anon");
filterChainDefinitionMap.put("/bHidLogin/**", "anon"); filterChainDefinitionMap.put("/bHidLogin/**", "anon");
filterChainDefinitionMap.put("/wechat/callback", "anon"); // 微信第三方登录
filterChainDefinitionMap.put("/wechat/callback", "anon"); // 微信第三方登录
filterChainDefinitionMap.put("/wechat/weChatUserLogin", "anon"); // 微信第三方登录
// callback // callback
filterChainDefinitionMap.put("/wxPay/notify/**", "anon"); // 支付回调 filterChainDefinitionMap.put("/wxPay/notify/**", "anon"); // 支付回调
filterChainDefinitionMap.put("/wxPayBill/notify/**", "anon"); filterChainDefinitionMap.put("/wxPayBill/notify/**", "anon");


+ 52
- 49
mallinkAdmin/src/main/java/com/iformall/controller/WechatLoginController.java Прегледај датотеку

@@ -16,6 +16,7 @@ import com.iformall.utils.TOTP;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
@@ -32,6 +33,7 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;




@@ -50,6 +52,13 @@ public class WechatLoginController extends BaseController {
@Autowired @Autowired
private MallUserRoleService mallUserRoleService; private MallUserRoleService mallUserRoleService;


@Data
private class DisplayUserInfo {
String mallName;
String userName;
String userRole;
}

@ApiOperation(value = "微信网页登录回调", notes = "请配置此callback到网页redirect_uri") @ApiOperation(value = "微信网页登录回调", notes = "请配置此callback到网页redirect_uri")
@GetMapping("callback") @GetMapping("callback")
@ResponseBody @ResponseBody
@@ -103,9 +112,18 @@ public class WechatLoginController extends BaseController {
// 跳转登录选择页面 // 跳转登录选择页面
String key = TOTP.generateWechatOpen(accessToken.getOpenId()); String key = TOTP.generateWechatOpen(accessToken.getOpenId());
RedisUtils redisUtils = new RedisUtils(); RedisUtils redisUtils = new RedisUtils();
redisUtils.set(WECHAT_PREV + key, JSON.toJSONString(userList));
redisUtils.set(WECHAT_PREV + key, accessToken.getOpenId());
List<DisplayUserInfo> users = new ArrayList<DisplayUserInfo>();
for(MallUserInfoVo user: userList) {
DisplayUserInfo disUser = new DisplayUserInfo();
disUser.setMallName(user.getMallName());
disUser.setUserName(user.getUsername());
disUser.setUserRole(user.getRoleName());
users.add(disUser);
}
String usersStr = JSON.toJSONString(users);
try { try {
response.sendRedirect("https://" + host + "/#/loginselect?key=" + key);
response.sendRedirect("https://" + host + "/#/loginselect?key=" + key + "&list=" + usersStr);
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage()); log.error(e.getMessage());
} }
@@ -124,70 +142,55 @@ public class WechatLoginController extends BaseController {
} }
} }


@ApiOperation(value = "微信登录用户信息列表")
@GetMapping("getMallUserList")
@ApiImplicitParams({
@ApiImplicitParam(name = "key", value = "key", dataType = "String", paramType = "query", required = true)})
public ResultData getMallUserList(String key) {
log.debug("[" + getIpAddr() + "] MallUserInfoController::getMallUserList");
String openKey = WECHAT_PREV + key;
RedisUtils redisUtils = new RedisUtils();
if(redisUtils.hasKey(openKey)) {
String userList = redisUtils.get(openKey);
return new ResultData(userList);
}
return new ResultData(ErrorCode.WECHAT_LOGIN_KEY_OVERTIME);
}

@ApiOperation(value = "微信用户登录") @ApiOperation(value = "微信用户登录")
@GetMapping("weChatUserLogin") @GetMapping("weChatUserLogin")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "key", value = "key", dataType = "String", paramType = "query", required = true), @ApiImplicitParam(name = "key", value = "key", dataType = "String", paramType = "query", required = true),
@ApiImplicitParam(name = "username", value = "username", dataType = "String", paramType = "query", required = true)})
public void weChatUserLogin(String username, String key, HttpServletRequest request, HttpServletResponse response) {
@ApiImplicitParam(name = "userName", value = "userName", dataType = "String", paramType = "query", required = true)})
public void weChatUserLogin(String userName, String key, HttpServletRequest request, HttpServletResponse response) {
log.debug("[" + getIpAddr() + "] MallUserInfoController::weChatUserLogin"); log.debug("[" + getIpAddr() + "] MallUserInfoController::weChatUserLogin");
String host = request.getHeader("host"); String host = request.getHeader("host");


String openKey = WECHAT_PREV + key; String openKey = WECHAT_PREV + key;
RedisUtils redisUtils = new RedisUtils(); RedisUtils redisUtils = new RedisUtils();
// 限时时间内查找到此用户openId
if(redisUtils.hasKey(openKey)) { if(redisUtils.hasKey(openKey)) {
String userList = redisUtils.get(openKey);
String openId = redisUtils.get(openKey);
// 删除缓存 // 删除缓存
redisUtils.delete(openKey); redisUtils.delete(openKey);
JSONArray userArr = JSON.parseArray(userList);
for(int i=0;i<userArr.size();i++) {
JSONObject userObj = userArr.getJSONObject(i);
if(userObj != null) {
String userName = userObj.getString("userName");
// 限时时间内查找到此用户
MallUserInfo userInfo = mallUserInfoService.getByUsername(userName);
try {
Subject subject = SecurityUtils.getSubject();
UseriFormallToken token = new UseriFormallToken(userInfo.getUsername());
subject.login(token);
MallUserInfo info = (MallUserInfo) SecurityUtils.getSubject().getSession().getAttribute(UserSession.userInfo);
info.setPassword("保密");
info.setBopenId("保密");
info.setWebOpenId("保密");
info.setWithWechat(true);
String menus = mallUserRoleService.getPermissionsByUser(info);
if(menus != null) {
info.setMenus(menus);
}
Cookie unameCookie = new Cookie("uname", info.getUsername());
unameCookie.setMaxAge(60);
response.addCookie(unameCookie);
log.debug("https://" + host + "/#/");
response.sendRedirect("https://" + host + "/#/layout");
} catch (Exception e) {
log.error(e.getMessage());
MallUserInfo userInfo = mallUserInfoService.getByUsername(userName);
if(userInfo.getWebOpenId().equals(openId)) {
try {
Subject subject = SecurityUtils.getSubject();
UseriFormallToken token = new UseriFormallToken(userInfo.getUsername());
subject.login(token);
MallUserInfo info = (MallUserInfo) SecurityUtils.getSubject().getSession().getAttribute(UserSession.userInfo);
info.setPassword("保密");
info.setBopenId("保密");
info.setWebOpenId("保密");
info.setWithWechat(true);
String menus = mallUserRoleService.getPermissionsByUser(info);
if(menus != null) {
info.setMenus(menus);
} }
// 登录cookie
Cookie unameCookie = new Cookie("uname", info.getUsername());
unameCookie.setMaxAge(60);
response.addCookie(unameCookie);

log.debug("https://" + host + "/#/");
response.sendRedirect("https://" + host + "/#/layout");
} catch (Exception e) {
log.error(e.getMessage());
} }
} else {
// 登录失败
log.error("微信登录失败,未找到对应的openId");
} }
} }
// 未找到,跳转登录页
// 未找到/登录失败,跳转登录页
try { try {
response.sendRedirect("https://" + host + "/#/login");
response.sendRedirect("https://" + host + "/#/login?type=WECHAT");
}catch (Exception e) { }catch (Exception e) {
log.error(e.getMessage()); log.error(e.getMessage());
} }


Loading…
Откажи
Сачувај