修改登录API逻辑

This commit is contained in:
guo
2024-07-09 22:32:12 +08:00
parent f1dc42fce5
commit e3f27cf975
7 changed files with 75 additions and 16 deletions
@@ -4,26 +4,40 @@ import com.guo.learningprogresstracker.domain.entity.CommonResult;
import com.guo.learningprogresstracker.domain.model.LoginBody; import com.guo.learningprogresstracker.domain.model.LoginBody;
import com.guo.learningprogresstracker.exception.AppException; import com.guo.learningprogresstracker.exception.AppException;
import com.guo.learningprogresstracker.service.LoginService; import com.guo.learningprogresstracker.service.LoginService;
import com.guo.learningprogresstracker.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/** /**
* @author guo * @author guo
*/ */
@CrossOrigin @CrossOrigin
@RestController @RestController
@RequiredArgsConstructor
public class LoginController { public class LoginController {
@Resource private final LoginService loginService;
private LoginService loginService; private final UserService userServiceImpl;
@Operation(summary = "登录API")
@PostMapping("/login") @PostMapping("/login")
public CommonResult<Object> login(@RequestBody LoginBody loginBody) throws AppException { public CommonResult<Object> login(@RequestBody LoginBody loginBody) throws AppException {
String token = loginService.login(loginBody.getUsername(),loginBody.getPassword()); String userId = userServiceImpl.authenticate(loginBody.getUsername(), loginBody.getPassword());
return CommonResult.success("登录成功!",token); String token = loginService.login(userId);
//todo 参数传递未加密
return CommonResult.success("登录成功!", token);
}
@Operation(summary = "退出登录API-功能未完成")
@PostMapping("/loingOut")
public CommonResult loingOut(@RequestBody LoginBody loginBody) throws AppException {
//todo loginOut 退出登录API
// loginService.loginOut();
return CommonResult.success("已登出");
} }
} }
@@ -29,7 +29,7 @@ public class CommonResult<T> {
} }
public static CommonResult<Object> success(String message,Object data){ public static CommonResult<Object> success(String message,Object data){
return new CommonResult(200,"请求成功,"+message,data); return new CommonResult(200,message,data);
} }
public static CommonResult success(Object data){ public static CommonResult success(Object data){
@@ -1,9 +1,8 @@
package com.guo.learningprogresstracker.service; package com.guo.learningprogresstracker.service;
import com.guo.learningprogresstracker.domain.entity.CommonResult;
import com.guo.learningprogresstracker.exception.AppException; import com.guo.learningprogresstracker.exception.AppException;
public interface LoginService { public interface LoginService {
String login(String username, String password) throws AppException; String login(String userId) throws AppException;
} }
@@ -2,6 +2,7 @@ package com.guo.learningprogresstracker.service;
import com.guo.learningprogresstracker.domain.entity.UserEntity; import com.guo.learningprogresstracker.domain.entity.UserEntity;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.guo.learningprogresstracker.exception.AppException;
/** /**
* @author guo * @author guo
@@ -10,4 +11,11 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/ */
public interface UserService extends IService<UserEntity> { public interface UserService extends IService<UserEntity> {
/**
* 验证账号密码是否匹配
* @param username
* @param password
* @return
*/
String authenticate(String username, String password) throws AppException;
} }
@@ -1,9 +1,6 @@
package com.guo.learningprogresstracker.service.impl; package com.guo.learningprogresstracker.service.impl;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.guo.learningprogresstracker.domain.entity.CommonResult;
import com.guo.learningprogresstracker.domain.entity.UserEntity;
import com.guo.learningprogresstracker.exception.AppException; import com.guo.learningprogresstracker.exception.AppException;
import com.guo.learningprogresstracker.service.LoginService; import com.guo.learningprogresstracker.service.LoginService;
import com.guo.learningprogresstracker.service.UserService; import com.guo.learningprogresstracker.service.UserService;
@@ -14,12 +11,10 @@ import org.springframework.stereotype.Service;
public class LoginServiceImpl implements LoginService { public class LoginServiceImpl implements LoginService {
@Autowired @Autowired
private UserService userService; private UserService userService;
@Override @Override
public String login(String username, String password) throws AppException { public String login(String userId) throws AppException {
UserEntity userEntity = userService.getOneOpt(Wrappers.<UserEntity>lambdaQuery() StpUtil.login(userId);
.eq(UserEntity::getUserName, username)
.eq(UserEntity::getUserPassword, password)).orElseThrow(() -> new AppException("账号或密码错误!"));
StpUtil.login(userEntity.getId());
//todo Token只在此处使用了,属于后续优化点 //todo Token只在此处使用了,属于后续优化点
return StpUtil.getTokenInfo().getTokenValue(); return StpUtil.getTokenInfo().getTokenValue();
} }
@@ -1,9 +1,12 @@
package com.guo.learningprogresstracker.service.impl; package com.guo.learningprogresstracker.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guo.learningprogresstracker.domain.entity.UserEntity; import com.guo.learningprogresstracker.domain.entity.UserEntity;
import com.guo.learningprogresstracker.exception.AppException;
import com.guo.learningprogresstracker.service.UserService; import com.guo.learningprogresstracker.service.UserService;
import com.guo.learningprogresstracker.mapper.UserMapper; import com.guo.learningprogresstracker.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
@@ -12,9 +15,18 @@ import org.springframework.stereotype.Service;
* @createDate 2024-06-09 15:28:48 * @createDate 2024-06-09 15:28:48
*/ */
@Service @Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity>
implements UserService{ implements UserService{
@Override
public String authenticate(String username, String password) throws AppException {
UserEntity userEntity = this.getOneOpt(Wrappers.<UserEntity>lambdaQuery()
.eq(UserEntity::getUserName, username)
.eq(UserEntity::getUserPassword, password)).orElseThrow(() -> new AppException("账号或密码错误!"));
return userEntity.getId();
}
} }
@@ -0,0 +1,31 @@
package com.guo.learningprogresstracker.controller;
import com.guo.learningprogresstracker.domain.entity.CommonResult;
import com.guo.learningprogresstracker.domain.model.LoginBody;
import com.guo.learningprogresstracker.exception.AppException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
class LoginControllerTest {
@Autowired
private LoginController loginController;
@Test
void login() throws AppException {
LoginBody loginBody = new LoginBody();
loginBody.setUsername("admin");
loginBody.setPassword("admin");
CommonResult<Object> login = loginController.login(loginBody);
assertEquals(login.getMessage(), "登录成功!");
}
@Test
void loingOut() {
}
}