Просмотр исходного кода

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

release_toaliyun_real
Stormeye Wu 7 лет назад
Родитель
Сommit
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
filterChainDefinitionMap.put("/doLogin/**", "anon");
filterChainDefinitionMap.put("/bHidLogin/**", "anon");
filterChainDefinitionMap.put("/wechat/callback", "anon"); // 微信第三方登录
filterChainDefinitionMap.put("/wechat/callback", "anon"); // 微信第三方登录
filterChainDefinitionMap.put("/wechat/weChatUserLogin", "anon"); // 微信第三方登录
// callback
filterChainDefinitionMap.put("/wxPay/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.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
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.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


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

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

@ApiOperation(value = "微信网页登录回调", notes = "请配置此callback到网页redirect_uri")
@GetMapping("callback")
@ResponseBody
@@ -103,9 +112,18 @@ public class WechatLoginController extends BaseController {
// 跳转登录选择页面
String key = TOTP.generateWechatOpen(accessToken.getOpenId());
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 {
response.sendRedirect("https://" + host + "/#/loginselect?key=" + key);
response.sendRedirect("https://" + host + "/#/loginselect?key=" + key + "&list=" + usersStr);
} catch (Exception e) {
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 = "微信用户登录")
@GetMapping("weChatUserLogin")
@ApiImplicitParams({
@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");
String host = request.getHeader("host");

String openKey = WECHAT_PREV + key;
RedisUtils redisUtils = new RedisUtils();
// 限时时间内查找到此用户openId
if(redisUtils.hasKey(openKey)) {
String userList = redisUtils.get(openKey);
String openId = redisUtils.get(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 {
response.sendRedirect("https://" + host + "/#/login");
response.sendRedirect("https://" + host + "/#/login?type=WECHAT");
}catch (Exception e) {
log.error(e.getMessage());
}


Загрузка…
Отмена
Сохранить