初始化 c 端的登录逻辑
parent
53bda604b0
commit
28fdc8e42e
|
@ -1,6 +1,7 @@
|
||||||
package cn.iocoder.yudao.adminserver.modules.system.controller.logger.vo.loginlog;
|
package cn.iocoder.yudao.adminserver.modules.system.controller.logger.vo.loginlog;
|
||||||
|
|
||||||
import io.swagger.annotations.ApiModel;
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
@ -11,4 +12,8 @@ import lombok.ToString;
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@ToString(callSuper = true)
|
@ToString(callSuper = true)
|
||||||
public class SysLoginLogCreateReqVO extends SysLoginLogBaseVO {
|
public class SysLoginLogCreateReqVO extends SysLoginLogBaseVO {
|
||||||
}
|
|
||||||
|
@ApiModelProperty(value = "用户编号", example = "1")
|
||||||
|
private Long userId;
|
||||||
|
|
||||||
|
}
|
|
@ -6,7 +6,7 @@ import cn.iocoder.yudao.framework.security.core.service.SecurityAuthFrameworkSer
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 认证 Service 接口
|
* 管理后台的认证 Service 接口
|
||||||
*
|
*
|
||||||
* 提供用户的账号密码登录、token 的校验等认证相关的功能
|
* 提供用户的账号密码登录、token 的校验等认证相关的功能
|
||||||
*
|
*
|
||||||
|
|
|
@ -38,6 +38,7 @@ import org.springframework.util.Assert;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.*;
|
import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.*;
|
||||||
|
@ -154,14 +155,24 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createLoginLog(String username, SysLoginLogTypeEnum logTypeEnum, SysLoginResultEnum loginResult) {
|
private void createLoginLog(String username, SysLoginLogTypeEnum logTypeEnum, SysLoginResultEnum loginResult) {
|
||||||
|
// 获得用户
|
||||||
|
SysUserDO user = userService.getUserByUsername(username);
|
||||||
|
// 插入登录日志
|
||||||
SysLoginLogCreateReqVO reqVO = new SysLoginLogCreateReqVO();
|
SysLoginLogCreateReqVO reqVO = new SysLoginLogCreateReqVO();
|
||||||
reqVO.setLogType(logTypeEnum.getType());
|
reqVO.setLogType(logTypeEnum.getType());
|
||||||
reqVO.setTraceId(TracerUtils.getTraceId());
|
reqVO.setTraceId(TracerUtils.getTraceId());
|
||||||
|
if (user != null) {
|
||||||
|
reqVO.setUserId(user.getId());
|
||||||
|
}
|
||||||
reqVO.setUsername(username);
|
reqVO.setUsername(username);
|
||||||
reqVO.setUserAgent(ServletUtils.getUserAgent());
|
reqVO.setUserAgent(ServletUtils.getUserAgent());
|
||||||
reqVO.setUserIp(ServletUtils.getClientIP());
|
reqVO.setUserIp(ServletUtils.getClientIP());
|
||||||
reqVO.setResult(loginResult.getResult());
|
reqVO.setResult(loginResult.getResult());
|
||||||
loginLogService.createLoginLog(reqVO);
|
loginLogService.createLoginLog(reqVO);
|
||||||
|
// 更新最后登录时间
|
||||||
|
if (user != null && Objects.equals(SysLoginResultEnum.SUCCESS.getResult(), loginResult.getResult())) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -24,17 +24,10 @@ public class SysLoginLogServiceImpl implements SysLoginLogService {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private SysLoginLogMapper loginLogMapper;
|
private SysLoginLogMapper loginLogMapper;
|
||||||
@Resource
|
|
||||||
private SysUserService userService;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createLoginLog(SysLoginLogCreateReqVO reqVO) {
|
public void createLoginLog(SysLoginLogCreateReqVO reqVO) {
|
||||||
SysLoginLogDO loginLog = SysLoginLogConvert.INSTANCE.convert(reqVO);
|
SysLoginLogDO loginLog = SysLoginLogConvert.INSTANCE.convert(reqVO);
|
||||||
// 获得用户
|
|
||||||
SysUserDO user = userService.getUserByUsername(reqVO.getUsername());
|
|
||||||
if (user != null) {
|
|
||||||
loginLog.setUserId(user.getId());
|
|
||||||
}
|
|
||||||
loginLog.setUserType(UserTypeEnum.ADMIN.getValue());
|
loginLog.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||||
// 插入
|
// 插入
|
||||||
loginLogMapper.insert(loginLog);
|
loginLogMapper.insert(loginLog);
|
||||||
|
|
|
@ -20,7 +20,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户 Service 接口
|
* 后台用户 Service 接口
|
||||||
*
|
*
|
||||||
* @author 芋道源码
|
* @author 芋道源码
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class LoginUser implements UserDetails {
|
||||||
*/
|
*/
|
||||||
private Long id;
|
private Long id;
|
||||||
/**
|
/**
|
||||||
* 科室编号
|
* 部门编号
|
||||||
*/
|
*/
|
||||||
private Long deptId;
|
private Long deptId;
|
||||||
/**
|
/**
|
||||||
|
@ -49,6 +49,8 @@ public class LoginUser implements UserDetails {
|
||||||
*/
|
*/
|
||||||
private Integer status;
|
private Integer status;
|
||||||
|
|
||||||
|
// TODO @芋艿:怎么去掉 deptId
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@JsonIgnore// 避免序列化
|
@JsonIgnore// 避免序列化
|
||||||
public String getPassword() {
|
public String getPassword() {
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
package cn.iocoder.yudao.userserver.modules.infra.service.auth;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.security.core.service.SecurityAuthFrameworkService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 认证 Service 接口
|
|
||||||
*
|
|
||||||
* 提供用户的账号密码登录、token 的校验等认证相关的功能
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
public interface SysAuthService extends SecurityAuthFrameworkService {
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,38 +0,0 @@
|
||||||
package cn.iocoder.yudao.userserver.modules.infra.service.auth.impl;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.security.core.LoginUser;
|
|
||||||
import cn.iocoder.yudao.userserver.modules.infra.service.auth.SysAuthService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
|
||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Auth Service 实现类
|
|
||||||
*
|
|
||||||
* @author 芋道源码
|
|
||||||
*/
|
|
||||||
@Service
|
|
||||||
@Slf4j
|
|
||||||
public class SysAuthServiceImpl implements SysAuthService {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public LoginUser verifyTokenAndRefresh(String token) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public LoginUser mockLogin(Long userId) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void logout(String token) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1 @@
|
||||||
|
package cn.iocoder.yudao.userserver.modules.infra.service;
|
|
@ -2,6 +2,7 @@ package cn.iocoder.yudao.userserver.modules.member.controller.auth;
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||||
import cn.iocoder.yudao.userserver.modules.member.controller.auth.vo.*;
|
import cn.iocoder.yudao.userserver.modules.member.controller.auth.vo.*;
|
||||||
|
import cn.iocoder.yudao.userserver.modules.member.service.auth.MbrAuthService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiImplicitParam;
|
||||||
import io.swagger.annotations.ApiImplicitParams;
|
import io.swagger.annotations.ApiImplicitParams;
|
||||||
|
@ -10,8 +11,13 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getUserAgent;
|
||||||
|
|
||||||
@Api(tags = "认证")
|
@Api(tags = "认证")
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/")
|
@RequestMapping("/")
|
||||||
|
@ -19,13 +25,15 @@ import javax.validation.Valid;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class MbrAuthController {
|
public class MbrAuthController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private MbrAuthService authService;
|
||||||
|
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
@ApiOperation("使用手机 + 密码登录")
|
@ApiOperation("使用手机 + 密码登录")
|
||||||
public CommonResult<MbrAuthLoginRespVO> login(@RequestBody @Valid MbrAuthLoginReqVO reqVO) {
|
public CommonResult<MbrAuthLoginRespVO> login(@RequestBody @Valid MbrAuthLoginReqVO reqVO) {
|
||||||
// String token = authService.login(reqVO, getClientIP(), getUserAgent());
|
String token = authService.login(reqVO, getClientIP(), getUserAgent());
|
||||||
// // 返回结果
|
// 返回结果
|
||||||
// return success(MbrAuthLoginRespVO.builder().token(token).build());
|
return success(MbrAuthLoginRespVO.builder().token(token).build());
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/sms-login")
|
@PostMapping("/sms-login")
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
package cn.iocoder.yudao.userserver.modules.member.convert;
|
|
@ -0,0 +1,15 @@
|
||||||
|
package cn.iocoder.yudao.userserver.modules.member.convert.user;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.security.core.LoginUser;
|
||||||
|
import cn.iocoder.yudao.userserver.modules.member.dal.dataobject.user.MbrUserDO;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface MbrAuthConvert {
|
||||||
|
|
||||||
|
MbrAuthConvert INSTANCE = Mappers.getMapper(MbrAuthConvert.class);
|
||||||
|
|
||||||
|
LoginUser convert(MbrUserDO bean);
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package cn.iocoder.yudao.userserver.modules.member.dal.mysql.user;
|
package cn.iocoder.yudao.userserver.modules.member.dal.dataobject.user;
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
|
@ -0,0 +1,19 @@
|
||||||
|
package cn.iocoder.yudao.userserver.modules.member.dal.mysql.user;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||||
|
import cn.iocoder.yudao.userserver.modules.member.dal.dataobject.user.MbrUserDO;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MbrUserDO Mapper
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface MbrUserMapper extends BaseMapperX<MbrUserDO> {
|
||||||
|
|
||||||
|
default MbrUserDO selectByMobile(String mobile) {
|
||||||
|
return selectOne("mobile", mobile);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package cn.iocoder.yudao.userserver.modules.member.service.auth;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.security.core.service.SecurityAuthFrameworkService;
|
||||||
|
import cn.iocoder.yudao.userserver.modules.member.controller.auth.vo.MbrAuthLoginReqVO;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户前台的认证 Service 接口
|
||||||
|
*
|
||||||
|
* 提供用户的账号密码登录、token 的校验等认证相关的功能
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
public interface MbrAuthService extends SecurityAuthFrameworkService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 手机 + 密码登录
|
||||||
|
*
|
||||||
|
* @param reqVO 登录信息
|
||||||
|
* @param userIp 用户 IP
|
||||||
|
* @param userAgent 用户 UA
|
||||||
|
* @return 身份令牌,使用 JWT 方式
|
||||||
|
*/
|
||||||
|
String login(@Valid MbrAuthLoginReqVO reqVO, String userIp, String userAgent);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,65 @@
|
||||||
|
package cn.iocoder.yudao.userserver.modules.member.service.auth.impl;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.security.core.LoginUser;
|
||||||
|
import cn.iocoder.yudao.userserver.modules.member.controller.auth.vo.MbrAuthLoginReqVO;
|
||||||
|
import cn.iocoder.yudao.userserver.modules.member.convert.user.MbrAuthConvert;
|
||||||
|
import cn.iocoder.yudao.userserver.modules.member.dal.dataobject.user.MbrUserDO;
|
||||||
|
import cn.iocoder.yudao.userserver.modules.member.service.auth.MbrAuthService;
|
||||||
|
import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auth Service 实现类
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class MbrAuthServiceImpl implements MbrAuthService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
@Lazy // 延迟加载,因为存在相互依赖的问题
|
||||||
|
private AuthenticationManager authenticationManager;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private MbrUserService userService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserDetails loadUserByUsername(String mobile) throws UsernameNotFoundException {
|
||||||
|
// 获取 username 对应的 SysUserDO
|
||||||
|
MbrUserDO user = userService.getUserByMobile(mobile);
|
||||||
|
if (user == null) {
|
||||||
|
throw new UsernameNotFoundException(mobile);
|
||||||
|
}
|
||||||
|
// 创建 LoginUser 对象
|
||||||
|
return MbrAuthConvert.INSTANCE.convert(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String login(MbrAuthLoginReqVO reqVO, String userIp, String userAgent) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LoginUser verifyTokenAndRefresh(String token) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LoginUser mockLogin(Long userId) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void logout(String token) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package cn.iocoder.yudao.userserver.modules.member.service.user;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.userserver.modules.member.dal.dataobject.user.MbrUserDO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 前台用户 Service 接口
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
public interface MbrUserService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过手机查询用户
|
||||||
|
*
|
||||||
|
* @param mobile 手机
|
||||||
|
* @return 用户对象
|
||||||
|
*/
|
||||||
|
MbrUserDO getUserByMobile(String mobile);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package cn.iocoder.yudao.userserver.modules.member.service.user.impl;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.userserver.modules.member.dal.dataobject.user.MbrUserDO;
|
||||||
|
import cn.iocoder.yudao.userserver.modules.member.dal.mysql.user.MbrUserMapper;
|
||||||
|
import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User Service 实现类
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Valid
|
||||||
|
@Slf4j
|
||||||
|
public class MbrUserServiceImpl implements MbrUserService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private MbrUserMapper userMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MbrUserDO getUserByMobile(String mobile) {
|
||||||
|
return userMapper.selectByMobile(mobile);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue