修改登录API逻辑
This commit is contained in:
@@ -4,26 +4,40 @@ import com.guo.learningprogresstracker.domain.entity.CommonResult;
|
||||
import com.guo.learningprogresstracker.domain.model.LoginBody;
|
||||
import com.guo.learningprogresstracker.exception.AppException;
|
||||
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.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @author guo
|
||||
*/
|
||||
@CrossOrigin
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class LoginController {
|
||||
|
||||
@Resource
|
||||
private LoginService loginService;
|
||||
private final LoginService loginService;
|
||||
private final UserService userServiceImpl;
|
||||
|
||||
@Operation(summary = "登录API")
|
||||
@PostMapping("/login")
|
||||
public CommonResult<Object> login(@RequestBody LoginBody loginBody) throws AppException {
|
||||
String token = loginService.login(loginBody.getUsername(),loginBody.getPassword());
|
||||
return CommonResult.success("登录成功!",token);
|
||||
String userId = userServiceImpl.authenticate(loginBody.getUsername(), loginBody.getPassword());
|
||||
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){
|
||||
return new CommonResult(200,"请求成功,"+message,data);
|
||||
return new CommonResult(200,message,data);
|
||||
}
|
||||
|
||||
public static CommonResult success(Object data){
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package com.guo.learningprogresstracker.service;
|
||||
|
||||
import com.guo.learningprogresstracker.domain.entity.CommonResult;
|
||||
import com.guo.learningprogresstracker.exception.AppException;
|
||||
|
||||
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.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.guo.learningprogresstracker.exception.AppException;
|
||||
|
||||
/**
|
||||
* @author guo
|
||||
@@ -10,4 +11,11 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||
*/
|
||||
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;
|
||||
|
||||
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.service.LoginService;
|
||||
import com.guo.learningprogresstracker.service.UserService;
|
||||
@@ -14,12 +11,10 @@ import org.springframework.stereotype.Service;
|
||||
public class LoginServiceImpl implements LoginService {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Override
|
||||
public String login(String username, String password) throws AppException {
|
||||
UserEntity userEntity = userService.getOneOpt(Wrappers.<UserEntity>lambdaQuery()
|
||||
.eq(UserEntity::getUserName, username)
|
||||
.eq(UserEntity::getUserPassword, password)).orElseThrow(() -> new AppException("账号或密码错误!"));
|
||||
StpUtil.login(userEntity.getId());
|
||||
public String login(String userId) throws AppException {
|
||||
StpUtil.login(userId);
|
||||
//todo Token只在此处使用了,属于后续优化点
|
||||
return StpUtil.getTokenInfo().getTokenValue();
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package com.guo.learningprogresstracker.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.guo.learningprogresstracker.domain.entity.UserEntity;
|
||||
import com.guo.learningprogresstracker.exception.AppException;
|
||||
import com.guo.learningprogresstracker.service.UserService;
|
||||
import com.guo.learningprogresstracker.mapper.UserMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
@@ -12,9 +15,18 @@ import org.springframework.stereotype.Service;
|
||||
* @createDate 2024-06-09 15:28:48
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user