Commit 28fdc8e42e17c48b9b214b8ab5c388a0f5527c6e
1 parent
53bda604
初始化 c 端的登录逻辑
Showing
17 changed files
with
179 additions
and
28 deletions
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/logger/vo/loginlog/SysLoginLogCreateReqVO.java
| 1 | 1 | package cn.iocoder.yudao.adminserver.modules.system.controller.logger.vo.loginlog; |
| 2 | 2 | |
| 3 | 3 | import io.swagger.annotations.ApiModel; |
| 4 | +import io.swagger.annotations.ApiModelProperty; | |
| 4 | 5 | import lombok.Data; |
| 5 | 6 | import lombok.EqualsAndHashCode; |
| 6 | 7 | import lombok.ToString; |
| ... | ... | @@ -11,4 +12,8 @@ import lombok.ToString; |
| 11 | 12 | @EqualsAndHashCode(callSuper = true) |
| 12 | 13 | @ToString(callSuper = true) |
| 13 | 14 | public class SysLoginLogCreateReqVO extends SysLoginLogBaseVO { |
| 14 | -} | |
| 15 | + | |
| 16 | + @ApiModelProperty(value = "用户编号", example = "1") | |
| 17 | + private Long userId; | |
| 18 | + | |
| 19 | +} | |
| 15 | 20 | \ No newline at end of file | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthService.java
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java
| ... | ... | @@ -38,6 +38,7 @@ import org.springframework.util.Assert; |
| 38 | 38 | |
| 39 | 39 | import javax.annotation.Resource; |
| 40 | 40 | import java.util.List; |
| 41 | +import java.util.Objects; | |
| 41 | 42 | import java.util.Set; |
| 42 | 43 | |
| 43 | 44 | import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.*; |
| ... | ... | @@ -154,14 +155,24 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 154 | 155 | } |
| 155 | 156 | |
| 156 | 157 | private void createLoginLog(String username, SysLoginLogTypeEnum logTypeEnum, SysLoginResultEnum loginResult) { |
| 158 | + // 获得用户 | |
| 159 | + SysUserDO user = userService.getUserByUsername(username); | |
| 160 | + // 插入登录日志 | |
| 157 | 161 | SysLoginLogCreateReqVO reqVO = new SysLoginLogCreateReqVO(); |
| 158 | 162 | reqVO.setLogType(logTypeEnum.getType()); |
| 159 | 163 | reqVO.setTraceId(TracerUtils.getTraceId()); |
| 164 | + if (user != null) { | |
| 165 | + reqVO.setUserId(user.getId()); | |
| 166 | + } | |
| 160 | 167 | reqVO.setUsername(username); |
| 161 | 168 | reqVO.setUserAgent(ServletUtils.getUserAgent()); |
| 162 | 169 | reqVO.setUserIp(ServletUtils.getClientIP()); |
| 163 | 170 | reqVO.setResult(loginResult.getResult()); |
| 164 | 171 | loginLogService.createLoginLog(reqVO); |
| 172 | + // 更新最后登录时间 | |
| 173 | + if (user != null && Objects.equals(SysLoginResultEnum.SUCCESS.getResult(), loginResult.getResult())) { | |
| 174 | + | |
| 175 | + } | |
| 165 | 176 | } |
| 166 | 177 | |
| 167 | 178 | /** | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/logger/impl/SysLoginLogServiceImpl.java
| ... | ... | @@ -24,17 +24,10 @@ public class SysLoginLogServiceImpl implements SysLoginLogService { |
| 24 | 24 | |
| 25 | 25 | @Resource |
| 26 | 26 | private SysLoginLogMapper loginLogMapper; |
| 27 | - @Resource | |
| 28 | - private SysUserService userService; | |
| 29 | 27 | |
| 30 | 28 | @Override |
| 31 | 29 | public void createLoginLog(SysLoginLogCreateReqVO reqVO) { |
| 32 | 30 | SysLoginLogDO loginLog = SysLoginLogConvert.INSTANCE.convert(reqVO); |
| 33 | - // 获得用户 | |
| 34 | - SysUserDO user = userService.getUserByUsername(reqVO.getUsername()); | |
| 35 | - if (user != null) { | |
| 36 | - loginLog.setUserId(user.getId()); | |
| 37 | - } | |
| 38 | 31 | loginLog.setUserType(UserTypeEnum.ADMIN.getValue()); |
| 39 | 32 | // 插入 |
| 40 | 33 | loginLogMapper.insert(loginLog); | ... | ... |
yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserService.java
yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/LoginUser.java
| ... | ... | @@ -24,7 +24,7 @@ public class LoginUser implements UserDetails { |
| 24 | 24 | */ |
| 25 | 25 | private Long id; |
| 26 | 26 | /** |
| 27 | - * 科室编号 | |
| 27 | + * 部门编号 | |
| 28 | 28 | */ |
| 29 | 29 | private Long deptId; |
| 30 | 30 | /** |
| ... | ... | @@ -49,6 +49,8 @@ public class LoginUser implements UserDetails { |
| 49 | 49 | */ |
| 50 | 50 | private Integer status; |
| 51 | 51 | |
| 52 | + // TODO @芋艿:怎么去掉 deptId | |
| 53 | + | |
| 52 | 54 | @Override |
| 53 | 55 | @JsonIgnore// 避免序列化 |
| 54 | 56 | public String getPassword() { | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/service/package-info.java
0 → 100644
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/controller/auth/MbrAuthController.java
| ... | ... | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.userserver.modules.member.controller.auth; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | 4 | import cn.iocoder.yudao.userserver.modules.member.controller.auth.vo.*; |
| 5 | +import cn.iocoder.yudao.userserver.modules.member.service.auth.MbrAuthService; | |
| 5 | 6 | import io.swagger.annotations.Api; |
| 6 | 7 | import io.swagger.annotations.ApiImplicitParam; |
| 7 | 8 | import io.swagger.annotations.ApiImplicitParams; |
| ... | ... | @@ -10,8 +11,13 @@ import lombok.extern.slf4j.Slf4j; |
| 10 | 11 | import org.springframework.validation.annotation.Validated; |
| 11 | 12 | import org.springframework.web.bind.annotation.*; |
| 12 | 13 | |
| 14 | +import javax.annotation.Resource; | |
| 13 | 15 | import javax.validation.Valid; |
| 14 | 16 | |
| 17 | +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | |
| 18 | +import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP; | |
| 19 | +import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getUserAgent; | |
| 20 | + | |
| 15 | 21 | @Api(tags = "认证") |
| 16 | 22 | @RestController |
| 17 | 23 | @RequestMapping("/") |
| ... | ... | @@ -19,13 +25,15 @@ import javax.validation.Valid; |
| 19 | 25 | @Slf4j |
| 20 | 26 | public class MbrAuthController { |
| 21 | 27 | |
| 28 | + @Resource | |
| 29 | + private MbrAuthService authService; | |
| 30 | + | |
| 22 | 31 | @PostMapping("/login") |
| 23 | 32 | @ApiOperation("使用手机 + 密码登录") |
| 24 | 33 | public CommonResult<MbrAuthLoginRespVO> login(@RequestBody @Valid MbrAuthLoginReqVO reqVO) { |
| 25 | -// String token = authService.login(reqVO, getClientIP(), getUserAgent()); | |
| 26 | -// // 返回结果 | |
| 27 | -// return success(MbrAuthLoginRespVO.builder().token(token).build()); | |
| 28 | - return null; | |
| 34 | + String token = authService.login(reqVO, getClientIP(), getUserAgent()); | |
| 35 | + // 返回结果 | |
| 36 | + return success(MbrAuthLoginRespVO.builder().token(token).build()); | |
| 29 | 37 | } |
| 30 | 38 | |
| 31 | 39 | @PostMapping("/sms-login") | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/convert/package-info.java
0 → 100644
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/convert/user/MbrAuthConvert.java
0 → 100644
| 1 | +package cn.iocoder.yudao.userserver.modules.member.convert.user; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.security.core.LoginUser; | |
| 4 | +import cn.iocoder.yudao.userserver.modules.member.dal.dataobject.user.MbrUserDO; | |
| 5 | +import org.mapstruct.Mapper; | |
| 6 | +import org.mapstruct.factory.Mappers; | |
| 7 | + | |
| 8 | +@Mapper | |
| 9 | +public interface MbrAuthConvert { | |
| 10 | + | |
| 11 | + MbrAuthConvert INSTANCE = Mappers.getMapper(MbrAuthConvert.class); | |
| 12 | + | |
| 13 | + LoginUser convert(MbrUserDO bean); | |
| 14 | + | |
| 15 | +} | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/dal/mysql/user/MbrUserDO.java renamed to yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/dal/dataobject/user/MbrUserDO.java
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/dal/mysql/user/MbrUserMapper.java
0 → 100644
| 1 | +package cn.iocoder.yudao.userserver.modules.member.dal.mysql.user; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | |
| 4 | +import cn.iocoder.yudao.userserver.modules.member.dal.dataobject.user.MbrUserDO; | |
| 5 | +import org.apache.ibatis.annotations.Mapper; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * MbrUserDO Mapper | |
| 9 | + * | |
| 10 | + * @author 芋道源码 | |
| 11 | + */ | |
| 12 | +@Mapper | |
| 13 | +public interface MbrUserMapper extends BaseMapperX<MbrUserDO> { | |
| 14 | + | |
| 15 | + default MbrUserDO selectByMobile(String mobile) { | |
| 16 | + return selectOne("mobile", mobile); | |
| 17 | + } | |
| 18 | + | |
| 19 | +} | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/service/auth/SysAuthService.java renamed to yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/service/auth/MbrAuthService.java
| 1 | -package cn.iocoder.yudao.userserver.modules.infra.service.auth; | |
| 1 | +package cn.iocoder.yudao.userserver.modules.member.service.auth; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.security.core.service.SecurityAuthFrameworkService; |
| 4 | +import cn.iocoder.yudao.userserver.modules.member.controller.auth.vo.MbrAuthLoginReqVO; | |
| 5 | + | |
| 6 | +import javax.validation.Valid; | |
| 4 | 7 | |
| 5 | 8 | /** |
| 6 | - * 认证 Service 接口 | |
| 9 | + * 用户前台的认证 Service 接口 | |
| 7 | 10 | * |
| 8 | 11 | * 提供用户的账号密码登录、token 的校验等认证相关的功能 |
| 9 | 12 | * |
| 10 | 13 | * @author 芋道源码 |
| 11 | 14 | */ |
| 12 | -public interface SysAuthService extends SecurityAuthFrameworkService { | |
| 15 | +public interface MbrAuthService extends SecurityAuthFrameworkService { | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * 手机 + 密码登录 | |
| 19 | + * | |
| 20 | + * @param reqVO 登录信息 | |
| 21 | + * @param userIp 用户 IP | |
| 22 | + * @param userAgent 用户 UA | |
| 23 | + * @return 身份令牌,使用 JWT 方式 | |
| 24 | + */ | |
| 25 | + String login(@Valid MbrAuthLoginReqVO reqVO, String userIp, String userAgent); | |
| 13 | 26 | |
| 14 | 27 | } | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/infra/service/auth/impl/SysAuthServiceImpl.java renamed to yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/service/auth/impl/MbrAuthServiceImpl.java
| 1 | -package cn.iocoder.yudao.userserver.modules.infra.service.auth.impl; | |
| 1 | +package cn.iocoder.yudao.userserver.modules.member.service.auth.impl; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.security.core.LoginUser; |
| 4 | -import cn.iocoder.yudao.userserver.modules.infra.service.auth.SysAuthService; | |
| 4 | +import cn.iocoder.yudao.userserver.modules.member.controller.auth.vo.MbrAuthLoginReqVO; | |
| 5 | +import cn.iocoder.yudao.userserver.modules.member.convert.user.MbrAuthConvert; | |
| 6 | +import cn.iocoder.yudao.userserver.modules.member.dal.dataobject.user.MbrUserDO; | |
| 7 | +import cn.iocoder.yudao.userserver.modules.member.service.auth.MbrAuthService; | |
| 8 | +import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService; | |
| 5 | 9 | import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.springframework.context.annotation.Lazy; | |
| 11 | +import org.springframework.security.authentication.AuthenticationManager; | |
| 6 | 12 | import org.springframework.security.core.userdetails.UserDetails; |
| 7 | 13 | import org.springframework.security.core.userdetails.UsernameNotFoundException; |
| 8 | 14 | import org.springframework.stereotype.Service; |
| 9 | 15 | |
| 16 | +import javax.annotation.Resource; | |
| 17 | + | |
| 10 | 18 | /** |
| 11 | 19 | * Auth Service 实现类 |
| 12 | 20 | * |
| ... | ... | @@ -14,7 +22,30 @@ import org.springframework.stereotype.Service; |
| 14 | 22 | */ |
| 15 | 23 | @Service |
| 16 | 24 | @Slf4j |
| 17 | -public class SysAuthServiceImpl implements SysAuthService { | |
| 25 | +public class MbrAuthServiceImpl implements MbrAuthService { | |
| 26 | + | |
| 27 | + @Resource | |
| 28 | + @Lazy // 延迟加载,因为存在相互依赖的问题 | |
| 29 | + private AuthenticationManager authenticationManager; | |
| 30 | + | |
| 31 | + @Resource | |
| 32 | + private MbrUserService userService; | |
| 33 | + | |
| 34 | + @Override | |
| 35 | + public UserDetails loadUserByUsername(String mobile) throws UsernameNotFoundException { | |
| 36 | + // 获取 username 对应的 SysUserDO | |
| 37 | + MbrUserDO user = userService.getUserByMobile(mobile); | |
| 38 | + if (user == null) { | |
| 39 | + throw new UsernameNotFoundException(mobile); | |
| 40 | + } | |
| 41 | + // 创建 LoginUser 对象 | |
| 42 | + return MbrAuthConvert.INSTANCE.convert(user); | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Override | |
| 46 | + public String login(MbrAuthLoginReqVO reqVO, String userIp, String userAgent) { | |
| 47 | + return null; | |
| 48 | + } | |
| 18 | 49 | |
| 19 | 50 | @Override |
| 20 | 51 | public LoginUser verifyTokenAndRefresh(String token) { |
| ... | ... | @@ -31,8 +62,4 @@ public class SysAuthServiceImpl implements SysAuthService { |
| 31 | 62 | |
| 32 | 63 | } |
| 33 | 64 | |
| 34 | - @Override | |
| 35 | - public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | |
| 36 | - return null; | |
| 37 | - } | |
| 38 | 65 | } | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/service/user/MbrUserService.java
0 → 100644
| 1 | +package cn.iocoder.yudao.userserver.modules.member.service.user; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.userserver.modules.member.dal.dataobject.user.MbrUserDO; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * 前台用户 Service 接口 | |
| 7 | + * | |
| 8 | + * @author 芋道源码 | |
| 9 | + */ | |
| 10 | +public interface MbrUserService { | |
| 11 | + | |
| 12 | + /** | |
| 13 | + * 通过手机查询用户 | |
| 14 | + * | |
| 15 | + * @param mobile 手机 | |
| 16 | + * @return 用户对象 | |
| 17 | + */ | |
| 18 | + MbrUserDO getUserByMobile(String mobile); | |
| 19 | + | |
| 20 | +} | ... | ... |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/member/service/user/impl/MbrUserServiceImpl.java
0 → 100644
| 1 | +package cn.iocoder.yudao.userserver.modules.member.service.user.impl; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.userserver.modules.member.dal.dataobject.user.MbrUserDO; | |
| 4 | +import cn.iocoder.yudao.userserver.modules.member.dal.mysql.user.MbrUserMapper; | |
| 5 | +import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService; | |
| 6 | +import lombok.extern.slf4j.Slf4j; | |
| 7 | +import org.springframework.stereotype.Service; | |
| 8 | + | |
| 9 | +import javax.annotation.Resource; | |
| 10 | +import javax.validation.Valid; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * User Service 实现类 | |
| 14 | + * | |
| 15 | + * @author 芋道源码 | |
| 16 | + */ | |
| 17 | +@Service | |
| 18 | +@Valid | |
| 19 | +@Slf4j | |
| 20 | +public class MbrUserServiceImpl implements MbrUserService { | |
| 21 | + | |
| 22 | + @Resource | |
| 23 | + private MbrUserMapper userMapper; | |
| 24 | + | |
| 25 | + @Override | |
| 26 | + public MbrUserDO getUserByMobile(String mobile) { | |
| 27 | + return userMapper.selectByMobile(mobile); | |
| 28 | + } | |
| 29 | + | |
| 30 | +} | ... | ... |
更新日志.md
| ... | ... | @@ -7,10 +7,16 @@ |
| 7 | 7 | |
| 8 | 8 | * 工作流 |
| 9 | 9 | |
| 10 | -## [v1.1.0] 待定 | |
| 10 | +## [v1.1.1] 待定 | |
| 11 | 11 | |
| 12 | 12 | * 支付 |
| 13 | -* 三方平台接入 | |
| 13 | + | |
| 14 | +## [v1.1.0] 待定 | |
| 15 | + | |
| 16 | +* 新增管理后台的企业微信、钉钉等社交登录 | |
| 17 | +* 新增用户前台(例如说,用户使用的小程序)的后端项目 `yudao-user-server` | |
| 18 | +* 新增用户前台的手机登录、验证码登录、TODO | |
| 19 | +* 修复管理后台的用户头像上传 404 的问题 | |
| 14 | 20 | |
| 15 | 21 | ## [v1.0.0] 2021.05.03 |
| 16 | 22 | ... | ... |