Commit 0f2e9070 authored by 万成波's avatar 万成波

移动端登录

parent 2a9ae49c
package com.tangguo.web.controller.system;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.tangguo.common.constant.Constants;
import com.tangguo.common.core.domain.AjaxResult;
import com.tangguo.common.core.domain.entity.SysMenu;
import com.tangguo.common.core.domain.entity.SysUser;
import com.tangguo.common.core.domain.model.LoginBody;
import com.tangguo.common.core.domain.model.LoginUser;
import com.tangguo.common.exception.ServiceException;
import com.tangguo.common.utils.SecurityUtils;
import com.tangguo.common.core.domain.model.WxcpCodeLogin;
import com.tangguo.framework.config.ServerConfig;
import com.tangguo.framework.web.service.SysLoginService;
import com.tangguo.framework.web.service.SysPermissionService;
import com.tangguo.framework.web.service.TokenService;
import com.tangguo.framework.web.service.UserDetailsServiceImpl;
import com.tangguo.system.service.ISysMenuService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* 登录验证
*
* @author ruoyi
*/
@Slf4j
@RestController
public class SysLoginController {
@Autowired
......@@ -36,6 +47,19 @@ public class SysLoginController {
@Autowired
private SysPermissionService permissionService;
@Autowired
private UserDetailsServiceImpl userDetailsServiceImpl;
@Autowired
private TokenService tokenService;
@Autowired
private WxCpService wxCpService;
@Autowired
private ServerConfig serverConfig;
/**
* 登录方法
*
......@@ -94,8 +118,32 @@ public class SysLoginController {
*/
@PostMapping("/bbs/mobile/user/code/login")
public AjaxResult codeLogin(@RequestBody WxcpCodeLogin bo) {
Map<String, Object> tokenMap = this.loginService.codeLogin(bo);
return AjaxResult.success(tokenMap);
LoginUser loginUser = SecurityUtils.getLoginUserNotEx();
// 企微登录认证链接
String authCode = bo.getCode();
String baseUrl = this.serverConfig.getUrl();
String redirectUrl = "http://test.tangguo.ren" + "/bbs/h5/pages/login/login/";
WxCpOAuth2Service oauth2Service = this.wxCpService.getOauth2Service();
if (Objects.isNull(loginUser) && StrUtil.isBlank(authCode)) {
String oauth2Url = oauth2Service.buildAuthorizationUrl(redirectUrl, null);
return AjaxResult.error(401, "身份认证失败", oauth2Url);
}
// 查询企微用户信息
String token = null;
if (Objects.isNull(loginUser) && StrUtil.isNotBlank(authCode)) {
try {
WxCpOauth2UserInfo userInfo = oauth2Service.getUserInfo(authCode);
log.info("=> 查询企微用户信息,返回结果:{},{}", authCode, JSON.toJSONString(userInfo));
UserDetails userDetails = this.userDetailsServiceImpl.loadUserByUsername2(userInfo.getUserId());
token = this.tokenService.createToken((LoginUser) userDetails);
} catch (Exception e) {
log.error("=> 查询用户信息失败:", e);
throw new ServiceException("登录失败,查询用户信息失败。");
}
}
return AjaxResult.success("登录成功", token);
}
......
......@@ -97,5 +97,5 @@ wx:
cp:
corp-id: ww63ca87d5f8647514
app-config:
agent-id: 1000044
secret: GqApjJ2aDuntiU5iQ9yqx8JKQwYDMrg1tTHdeeF0BWA
agent-id: 1000072
secret: O2KXf2b9oGG2GBrpzDgf4EFdhGwl2KaS9BWtJQT1I64
# 项目相关配置
ruoyi:
profile: /home/application/safe-campus-bbs/server/resources
profile: /home/applications/safe-campus-bbs/server/resources
# 数据源配置
spring:
......
......@@ -36,7 +36,7 @@ public class ApplicationTest {
public void test() {
WxCpOAuth2Service oauth2Service = this.wxCpService.getOauth2Service();
System.out.println(oauth2Service.buildAuthorizationUrl("https://wecom.jift.edu.cn/bbs/", ""));
System.out.println(oauth2Service.buildAuthorizationUrl("https://test.tangguo.ren/bbs/h5/pages/login/login/", ""));
}
......
......@@ -158,6 +158,17 @@ public class AjaxResult extends HashMap<String, Object> {
return new AjaxResult(code, msg, null);
}
/**
* 返回错误消息
*
* @param code 状态码
* @param msg 返回内容
* @return 错误消息
*/
public static AjaxResult error(int code, String msg, Object data) {
return new AjaxResult(code, msg, data);
}
/**
* 方便链式调用
*
......
......@@ -57,6 +57,18 @@ public class SecurityUtils {
}
}
/**
* 获取用户
* 不会抛出异常 用于可登录可不登录时使用获取信息
**/
public static LoginUser getLoginUserNotEx() {
try {
return (LoginUser) getAuthentication().getPrincipal();
} catch (Exception e) {
return null;
}
}
/**
* 获取Authentication
*/
......
package com.tangguo.framework.web.service;
import cn.hutool.core.util.StrUtil;
import com.tangguo.common.constant.CacheConstants;
import com.tangguo.common.constant.Constants;
import com.tangguo.common.constant.UserConstants;
......@@ -11,6 +12,7 @@ import com.tangguo.common.exception.ServiceException;
import com.tangguo.common.exception.user.*;
import com.tangguo.common.utils.DateUtils;
import com.tangguo.common.utils.MessageUtils;
import com.tangguo.common.utils.SecurityUtils;
import com.tangguo.common.utils.StringUtils;
import com.tangguo.common.utils.ip.IpUtils;
import com.tangguo.framework.manager.AsyncManager;
......@@ -20,6 +22,7 @@ import com.tangguo.system.service.ISysConfigService;
import com.tangguo.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -174,34 +177,4 @@ public class SysLoginService {
userService.updateUserProfile(sysUser);
}
/**
* 企微用户Code登录
*
* @param bo 登录参数
* @return 登录结果
*/
public Map<String, Object> codeLogin(WxcpCodeLogin bo) {
String userName;
try {
log.info("=> 移动端用户登录:{}", bo);
WxCpOauth2UserInfo authUserInfo = this.wxCpService.getOauth2Service().getAuthUserInfo(bo.getCode());
userName = authUserInfo.getUserId();
} catch (WxErrorException e) {
log.error("=> 移动端用户登录失败,查询当前企微用户数据失败:", e);
throw new ServiceException("登录失败,查询当前企微用户数据失败。", 401);
}
try {
UserDetails userDetails = this.userDetailsServiceImpl.loadUserByUsername2(userName);
String token = this.tokenService.createToken((LoginUser) userDetails);
Map<String, Object> resultMap = new LinkedHashMap<>(2);
resultMap.put("token", token);
return resultMap;
} catch (Exception e) {
log.error("=> 移动端用户登录失败,生成用户Token失败:", e);
throw new ServiceException("登录失败:" + e.getMessage(), 401);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment