新增退出登录接口定义

This commit is contained in:
aoshiguchen
2022-08-02 00:03:15 +08:00
parent 6e74685c9b
commit a5a2ef8a62
4 changed files with 108 additions and 30 deletions
@@ -27,17 +27,14 @@ import fun.asgc.neutrino.core.web.annotation.PostMapping;
import fun.asgc.neutrino.core.web.annotation.RequestBody;
import fun.asgc.neutrino.core.web.annotation.RequestMapping;
import fun.asgc.neutrino.core.web.annotation.RestController;
import fun.asgc.neutrino.proxy.server.base.rest.Authorization;
import fun.asgc.neutrino.proxy.server.controller.req.LoginReq;
import fun.asgc.neutrino.proxy.server.controller.req.LogoutReq;
import fun.asgc.neutrino.proxy.server.controller.res.LoginRes;
import fun.asgc.neutrino.proxy.server.dal.entity.UserDO;
import fun.asgc.neutrino.proxy.server.dal.entity.UserTokenDO;
import fun.asgc.neutrino.proxy.server.controller.res.LogoutRes;
import fun.asgc.neutrino.proxy.server.service.UserService;
import fun.asgc.neutrino.proxy.server.util.Md5Util;
import fun.asgc.neutrino.proxy.server.util.ParamCheckUtil;
import java.util.Date;
import java.util.UUID;
/**
*
* @author: aoshiguchen
@@ -50,31 +47,19 @@ public class IndexController {
@Autowired
private UserService userService;
@Authorization(login = false)
@PostMapping("login")
public LoginRes login(@RequestBody LoginReq req) {
ParamCheckUtil.checkNotEmpty(req.getLoginName(), "loginName");
ParamCheckUtil.checkNotEmpty(req.getLoginPassword(), "loginPassword");
UserDO userDO = userService.findByLoginName(req.getLoginName());
if (null == userDO || !Md5Util.encode(req.getLoginPassword()).equals(userDO.getLoginPassword())) {
// TODO 抛出异常
}
String token = UUID.randomUUID().toString().replaceAll("-", "");
return userService.login(req);
}
Date now = new Date();
// TODO 计算过期时间
userService.addUserToken(new UserTokenDO()
.setToken(token)
.setUserId(userDO.getId())
.setExpirationTime(now)
.setCreateTime(now)
.setUpdateTime(now)
);
return new LoginRes()
.setToken(token)
.setUserId(userDO.getId())
.setUserName(userDO.getName());
@PostMapping("logout")
public LogoutRes logout(@RequestBody LogoutReq req) {
// TODO
return null;
}
}
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.controller.req;
import lombok.Data;
/**
* 退出登录请求参数
* @author: aoshiguchen
* @date: 2022/8/1
*/
@Data
public class LogoutReq {
}
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.controller.res;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 退出登录响应参数
* @author: aoshiguchen
* @date: 2022/8/2
*/
@Accessors(chain = true)
@Data
public class LogoutRes {
}
@@ -23,10 +23,17 @@ package fun.asgc.neutrino.proxy.server.service;
import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.web.annotation.RequestBody;
import fun.asgc.neutrino.proxy.server.controller.req.LoginReq;
import fun.asgc.neutrino.proxy.server.controller.res.LoginRes;
import fun.asgc.neutrino.proxy.server.dal.UserMapper;
import fun.asgc.neutrino.proxy.server.dal.UserTokenMapper;
import fun.asgc.neutrino.proxy.server.dal.entity.UserDO;
import fun.asgc.neutrino.proxy.server.dal.entity.UserTokenDO;
import fun.asgc.neutrino.proxy.server.util.Md5Util;
import java.util.Date;
import java.util.UUID;
/**
*
@@ -40,13 +47,29 @@ public class UserService {
@Autowired
private UserTokenMapper userTokenMapper;
public LoginRes login(LoginReq req) {
UserDO userDO = userMapper.findByLoginName(req.getLoginName());
if (null == userDO || !Md5Util.encode(req.getLoginPassword()).equals(userDO.getLoginPassword())) {
// TODO 抛出异常
}
String token = UUID.randomUUID().toString().replaceAll("-", "");
public UserDO findByLoginName(String loginName) {
return userMapper.findByLoginName(loginName);
}
Date now = new Date();
// TODO 计算过期时间
userTokenMapper.add(new UserTokenDO()
.setToken(token)
.setUserId(userDO.getId())
.setExpirationTime(now)
.setCreateTime(now)
.setUpdateTime(now)
);
public void addUserToken(UserTokenDO userTokenDO) {
userTokenMapper.add(userTokenDO);
// TODO 新增登录日志
return new LoginRes()
.setToken(token)
.setUserId(userDO.getId())
.setUserName(userDO.getName());
}
}