2024-08-17 12:11:19 +08:00
|
|
|
package com.yanzhu.auth.controller;
|
|
|
|
|
|
|
|
import com.yanzhu.auth.form.LoginBody;
|
|
|
|
import com.yanzhu.auth.form.RegisterBody;
|
|
|
|
import com.yanzhu.auth.service.SysLoginService;
|
2024-09-17 10:16:27 +08:00
|
|
|
import com.yanzhu.common.core.constant.SecurityConstants;
|
2024-08-17 12:11:19 +08:00
|
|
|
import com.yanzhu.common.core.domain.R;
|
2024-09-17 10:16:27 +08:00
|
|
|
import com.yanzhu.common.core.text.Convert;
|
2024-08-17 12:11:19 +08:00
|
|
|
import com.yanzhu.common.core.utils.JwtUtils;
|
|
|
|
import com.yanzhu.common.core.utils.StringUtils;
|
|
|
|
import com.yanzhu.common.security.auth.AuthUtil;
|
|
|
|
import com.yanzhu.common.security.service.TokenService;
|
|
|
|
import com.yanzhu.common.security.utils.SecurityUtils;
|
2024-09-17 10:16:27 +08:00
|
|
|
import com.yanzhu.system.api.RemoteProService;
|
|
|
|
import com.yanzhu.system.api.RemoteUserService;
|
|
|
|
import com.yanzhu.system.api.domain.SysUser;
|
2024-08-17 12:11:19 +08:00
|
|
|
import com.yanzhu.system.api.model.LoginUser;
|
2024-09-17 10:16:27 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Objects;
|
2024-08-17 12:11:19 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* token 控制
|
|
|
|
*
|
|
|
|
* @author ruoyi
|
|
|
|
*/
|
|
|
|
@RestController
|
|
|
|
public class TokenController
|
|
|
|
{
|
|
|
|
@Autowired
|
|
|
|
private TokenService tokenService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private SysLoginService sysLoginService;
|
|
|
|
|
2024-09-17 10:16:27 +08:00
|
|
|
@Autowired
|
|
|
|
private RemoteProService remoteProService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private RemoteUserService remoteUserService;
|
|
|
|
|
2024-08-17 12:11:19 +08:00
|
|
|
@PostMapping("login")
|
|
|
|
public R<?> login(@RequestBody LoginBody form)
|
|
|
|
{
|
|
|
|
// 用户登录
|
|
|
|
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
|
|
|
|
// 获取登录token
|
|
|
|
return R.ok(tokenService.createToken(userInfo));
|
|
|
|
}
|
|
|
|
|
2024-09-17 10:16:27 +08:00
|
|
|
/**
|
|
|
|
* 切换项目登录
|
|
|
|
* @param proId 切换项目登录
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
@GetMapping("/switchProject/{proId}")
|
|
|
|
public R<?> switchProject(@PathVariable("proId") Long proId,HttpServletRequest request)
|
|
|
|
{
|
|
|
|
if(SecurityUtils.isAdmin(SecurityUtils.getUserId()) || SecurityUtils.isGSAdmin()){
|
|
|
|
LoginUser loginUser = tokenService.getLoginUser(request);
|
|
|
|
if (StringUtils.isNotNull(loginUser))
|
|
|
|
{
|
|
|
|
R<Map<String, Object>> dataResult = remoteProService.info(proId,SecurityConstants.INNER);
|
|
|
|
Map<String, Object> proData = dataResult.getData();
|
|
|
|
SysUser user = loginUser.getSysUser();
|
|
|
|
user.setActiveComId(Convert.toLong(proData.get("comId")));
|
|
|
|
user.setActiveProjectId(Convert.toLong(proData.get("id")));
|
|
|
|
user.setActiveProjectName(Convert.toStr(proData.get("projectName")));
|
|
|
|
loginUser.setProjectId(Convert.toLong(proData.get("id")));
|
|
|
|
loginUser.setProjectName(Convert.toStr(proData.get("projectName")));
|
|
|
|
loginUser.setProjectDeptId(Convert.toLong(proData.get("comId")));
|
|
|
|
// 刷新令牌有效期
|
|
|
|
tokenService.refreshToken(loginUser);
|
|
|
|
return R.ok();
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
// 查询用户信息
|
|
|
|
R<LoginUser> userResult = remoteUserService.getUserInfo(SecurityUtils.getUsername(), proId, SecurityConstants.INNER);
|
|
|
|
LoginUser userInfo = userResult.getData();
|
|
|
|
SysUser user = userResult.getData().getSysUser();
|
|
|
|
// 补充用户项目信息
|
|
|
|
if(Objects.nonNull(user.getActiveComId())){
|
|
|
|
userInfo.setProjectDeptId(user.getActiveComId());
|
|
|
|
}
|
|
|
|
if(Objects.nonNull(user.getActiveProjectId())){
|
|
|
|
userInfo.setProjectId(user.getActiveProjectId());
|
|
|
|
}
|
|
|
|
if(Objects.nonNull(user.getActiveProjectName())){
|
|
|
|
userInfo.setProjectName(user.getActiveProjectName());
|
|
|
|
}
|
|
|
|
// 刷新令牌有效期
|
|
|
|
tokenService.refreshToken(userInfo);
|
|
|
|
return R.ok();
|
|
|
|
}
|
|
|
|
return R.fail();
|
|
|
|
}
|
|
|
|
|
2024-08-17 12:11:19 +08:00
|
|
|
@DeleteMapping("logout")
|
|
|
|
public R<?> logout(HttpServletRequest request)
|
|
|
|
{
|
|
|
|
String token = SecurityUtils.getToken(request);
|
|
|
|
if (StringUtils.isNotEmpty(token))
|
|
|
|
{
|
|
|
|
String username = JwtUtils.getUserName(token);
|
|
|
|
// 删除用户缓存记录
|
|
|
|
AuthUtil.logoutByToken(token);
|
|
|
|
// 记录用户退出日志
|
|
|
|
sysLoginService.logout(username);
|
|
|
|
}
|
|
|
|
return R.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("refresh")
|
|
|
|
public R<?> refresh(HttpServletRequest request)
|
|
|
|
{
|
|
|
|
LoginUser loginUser = tokenService.getLoginUser(request);
|
|
|
|
if (StringUtils.isNotNull(loginUser))
|
|
|
|
{
|
|
|
|
// 刷新令牌有效期
|
|
|
|
tokenService.refreshToken(loginUser);
|
|
|
|
return R.ok();
|
|
|
|
}
|
|
|
|
return R.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("register")
|
|
|
|
public R<?> register(@RequestBody RegisterBody registerBody)
|
|
|
|
{
|
|
|
|
// 用户注册
|
|
|
|
sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
|
|
|
|
return R.ok();
|
|
|
|
}
|
|
|
|
}
|