单点登录亚微退出问题

This commit is contained in:
wangliwen 2022-06-10 09:39:06 +08:00
parent 8b295bcbcb
commit 4308dcec36
1 changed files with 112 additions and 97 deletions

View File

@ -25,9 +25,15 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletRequest;
@ -38,130 +44,139 @@ import java.util.Map;
/**
* 登录
*
*/
@RestController
@Api(tags="登录管理")
@Api(tags = "登录管理")
public class LoginController {
@Autowired
private SysUserService sysUserService;
@Autowired
private SysUserTokenService sysUserTokenService;
@Autowired
private CaptchaService captchaService;
@Autowired
private SysLogLoginService sysLogLoginService;
private static Logger logger = LoggerFactory.getLogger(LoginController.class);
@Autowired
private SysUserService sysUserService;
@Autowired
private SysUserTokenService sysUserTokenService;
@Autowired
private CaptchaService captchaService;
@Autowired
private SysLogLoginService sysLogLoginService;
@GetMapping("captcha")
@ApiOperation(value = "验证码", produces="application/octet-stream")
@ApiImplicitParam(paramType = "query", dataType="string", name = "uuid", required = true)
public void captcha(HttpServletResponse response, String uuid)throws IOException {
//uuid不能为空
AssertUtils.isBlank(uuid, ErrorCode.IDENTIFIER_NOT_NULL);
@Value("${yawei.enable}")
private Boolean yaweiEnable; // 亚微登录
//生成验证码
captchaService.create(response, uuid);
}
@GetMapping("captcha")
@ApiOperation(value = "验证码", produces = "application/octet-stream")
@ApiImplicitParam(paramType = "query", dataType = "string", name = "uuid", required = true)
public void captcha(HttpServletResponse response, String uuid) throws IOException {
//uuid不能为空
AssertUtils.isBlank(uuid, ErrorCode.IDENTIFIER_NOT_NULL);
@GetMapping("login")
@ApiOperation(value = "登录")
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "登录名称", paramType = "query",required = true, dataType="String"),
@ApiImplicitParam(name = "password", value = "密码", paramType = "query",required = true, dataType="String"),
//生成验证码
captchaService.create(response, uuid);
}
@GetMapping("login")
@ApiOperation(value = "登录")
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "登录名称", paramType = "query", required = true, dataType = "String"),
@ApiImplicitParam(name = "password", value = "密码", paramType = "query", required = true, dataType = "String"),
// @ApiImplicitParam(name = "captcha", value = "验证码", paramType = "query",required = true, dataType="String"),
@ApiImplicitParam(name = "uuid", value = "UUID", paramType = "query",required = true, dataType="String"),
})
@ApiImplicitParam(name = "uuid", value = "UUID", paramType = "query", required = true, dataType = "String"),
})
// public Result login(HttpServletRequest request, @RequestBody LoginDTO login) {
public Result login(HttpServletRequest request, HttpServletResponse response, @ApiIgnore @RequestParam Map<String, Object> params) throws Exception {
public Result login(HttpServletRequest request, HttpServletResponse response, @ApiIgnore @RequestParam Map<String, Object> params) throws Exception {
LoginDTO login = new LoginDTO();
login.setUsername(String.valueOf(params.get("username")));
login.setPassword(String.valueOf(params.get("password")));
LoginDTO login = new LoginDTO();
login.setUsername(String.valueOf(params.get("username")));
login.setPassword(String.valueOf(params.get("password")));
// login.setCaptcha(String.valueOf(params.get("captcha")));
login.setUuid(String.valueOf(params.get("uuid")));
//效验数据
login.setUuid(String.valueOf(params.get("uuid")));
//效验数据
// ValidatorUtils.validateEntity(login);
//验证码是否正确
//验证码是否正确
// boolean flag = captchaService.validate(login.getUuid(), login.getCaptcha());
// if(!flag){
// return new Result().error(ErrorCode.CAPTCHA_ERROR);
// }
//用户信息
SysUserDTO user = sysUserService.getByUsername(login.getUsername());
//用户信息
SysUserDTO user = sysUserService.getByUsername(login.getUsername());
SysLogLoginEntity log = new SysLogLoginEntity();
log.setOperation(LoginOperationEnum.LOGIN.value());
log.setCreateDate(new Date());
log.setIp(IpUtils.getIpAddr(request));
log.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
log.setIp(IpUtils.getIpAddr(request));
SysLogLoginEntity log = new SysLogLoginEntity();
log.setOperation(LoginOperationEnum.LOGIN.value());
log.setCreateDate(new Date());
log.setIp(IpUtils.getIpAddr(request));
log.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
log.setIp(IpUtils.getIpAddr(request));
//用户不存在
if(user == null){
log.setStatus(LoginStatusEnum.FAIL.value());
log.setCreatorName(login.getUsername());
sysLogLoginService.save(log);
//用户不存在
if (user == null) {
log.setStatus(LoginStatusEnum.FAIL.value());
log.setCreatorName(login.getUsername());
sysLogLoginService.save(log);
throw new RenException(ErrorCode.ACCOUNT_PASSWORD_ERROR);
}
throw new RenException(ErrorCode.ACCOUNT_PASSWORD_ERROR);
}
//密码错误
if(!PasswordUtils.matches(login.getPassword(), user.getPassword())){
log.setStatus(LoginStatusEnum.FAIL.value());
log.setCreator(user.getId());
log.setCreatorName(user.getUsername());
sysLogLoginService.save(log);
//密码错误
if (!PasswordUtils.matches(login.getPassword(), user.getPassword())) {
log.setStatus(LoginStatusEnum.FAIL.value());
log.setCreator(user.getId());
log.setCreatorName(user.getUsername());
sysLogLoginService.save(log);
throw new RenException(ErrorCode.ACCOUNT_PASSWORD_ERROR);
}
throw new RenException(ErrorCode.ACCOUNT_PASSWORD_ERROR);
}
//账号停用
if(user.getStatus() == UserStatusEnum.DISABLE.value()){
log.setStatus(LoginStatusEnum.LOCK.value());
log.setCreator(user.getId());
log.setCreatorName(user.getUsername());
sysLogLoginService.save(log);
//账号停用
if (user.getStatus() == UserStatusEnum.DISABLE.value()) {
log.setStatus(LoginStatusEnum.LOCK.value());
log.setCreator(user.getId());
log.setCreatorName(user.getUsername());
sysLogLoginService.save(log);
throw new RenException(ErrorCode.ACCOUNT_DISABLE);
}
throw new RenException(ErrorCode.ACCOUNT_DISABLE);
}
//登录成功
log.setStatus(LoginStatusEnum.SUCCESS.value());
log.setCreator(user.getId());
log.setCreatorName(user.getUsername());
sysLogLoginService.save(log);
//登录成功
log.setStatus(LoginStatusEnum.SUCCESS.value());
log.setCreator(user.getId());
log.setCreatorName(user.getUsername());
sysLogLoginService.save(log);
Result<Map> token = sysUserTokenService.createToken(user.getId());
request.setAttribute(Constant.TOKEN_HEADER, token.getData().get(Constant.TOKEN_HEADER));
new Oauth2Filter().login(request, response);
return token;
}
Result<Map> token = sysUserTokenService.createToken(user.getId());
request.setAttribute(Constant.TOKEN_HEADER, token.getData().get(Constant.TOKEN_HEADER));
new Oauth2Filter().login(request, response);
return token;
}
@PostMapping("logout")
@ApiOperation(value = "退出")
public Result logout(HttpServletRequest request, HttpServletResponse response) {
UserDetail user = SecurityUser.getUser();
@PostMapping("logout")
@ApiOperation(value = "退出")
public Result logout(HttpServletRequest request, HttpServletResponse response) {
UserDetail user = SecurityUser.getUser();
//退出
sysUserTokenService.logout(user.getId());
//清理亚威登录状态
YaWeiCookieManage.clearnTicket(request, response);
//退出
sysUserTokenService.logout(user.getId());
//用户信息
SysLogLoginEntity log = new SysLogLoginEntity();
log.setOperation(LoginOperationEnum.LOGOUT.value());
log.setIp(IpUtils.getIpAddr(request));
log.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
log.setIp(IpUtils.getIpAddr(request));
log.setStatus(LoginStatusEnum.SUCCESS.value());
log.setCreator(user.getId());
log.setCreatorName(user.getUsername());
log.setCreateDate(new Date());
sysLogLoginService.save(log);
try {
if (yaweiEnable) {
//清理亚威登录状态
YaWeiCookieManage.clearnTicket(request, response);
}
} catch (Exception e) {
logger.error("清理亚威登录状态失败", e);
}
//用户信息
SysLogLoginEntity log = new SysLogLoginEntity();
log.setOperation(LoginOperationEnum.LOGOUT.value());
log.setIp(IpUtils.getIpAddr(request));
log.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
log.setIp(IpUtils.getIpAddr(request));
log.setStatus(LoginStatusEnum.SUCCESS.value());
log.setCreator(user.getId());
log.setCreatorName(user.getUsername());
log.setCreateDate(new Date());
sysLogLoginService.save(log);
return new Result();
}
return new Result();
}
}