Commit 86e6c04e078721a77f60413000d4d99f402aeedb
1 parent
5ea9cc3c
登录后,返回 OAuth2 的 access token + refresh token
Showing
29 changed files
with
134 additions
and
680 deletions
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/auth/OAuth2ClientIdEnum.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.system.enums.auth; | ||
| 2 | + | ||
| 3 | +import lombok.AllArgsConstructor; | ||
| 4 | +import lombok.Getter; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * OAuth2.0 客户端的编号枚举 | ||
| 8 | + */ | ||
| 9 | +@AllArgsConstructor | ||
| 10 | +@Getter | ||
| 11 | +public enum OAuth2ClientIdEnum { | ||
| 12 | + | ||
| 13 | + DEFAULT(1L); // 系统默认 | ||
| 14 | + | ||
| 15 | + private final Long id; | ||
| 16 | + | ||
| 17 | +} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/auth/OAuth2TokenApiImpl.java
| @@ -3,7 +3,7 @@ package cn.iocoder.yudao.module.system.api.auth; | @@ -3,7 +3,7 @@ package cn.iocoder.yudao.module.system.api.auth; | ||
| 3 | import cn.iocoder.yudao.module.system.api.auth.dto.OAuth2AccessTokenCheckRespDTO; | 3 | import cn.iocoder.yudao.module.system.api.auth.dto.OAuth2AccessTokenCheckRespDTO; |
| 4 | import cn.iocoder.yudao.module.system.api.auth.dto.OAuth2AccessTokenCreateReqDTO; | 4 | import cn.iocoder.yudao.module.system.api.auth.dto.OAuth2AccessTokenCreateReqDTO; |
| 5 | import cn.iocoder.yudao.module.system.api.auth.dto.OAuth2AccessTokenRespDTO; | 5 | import cn.iocoder.yudao.module.system.api.auth.dto.OAuth2AccessTokenRespDTO; |
| 6 | -import cn.iocoder.yudao.module.system.convert.auth.UserSessionConvert; | 6 | +import cn.iocoder.yudao.module.system.convert.auth.OAuth2TokenConvert; |
| 7 | import cn.iocoder.yudao.module.system.service.auth.OAuth2TokenService; | 7 | import cn.iocoder.yudao.module.system.service.auth.OAuth2TokenService; |
| 8 | import org.springframework.stereotype.Service; | 8 | import org.springframework.stereotype.Service; |
| 9 | 9 | ||
| @@ -27,7 +27,7 @@ public class OAuth2TokenApiImpl implements OAuth2TokenApi { | @@ -27,7 +27,7 @@ public class OAuth2TokenApiImpl implements OAuth2TokenApi { | ||
| 27 | 27 | ||
| 28 | @Override | 28 | @Override |
| 29 | public OAuth2AccessTokenCheckRespDTO checkAccessToken(String accessToken) { | 29 | public OAuth2AccessTokenCheckRespDTO checkAccessToken(String accessToken) { |
| 30 | - return UserSessionConvert.INSTANCE.convert(oauth2TokenService.checkAccessToken(accessToken)); | 30 | + return OAuth2TokenConvert.INSTANCE.convert(oauth2TokenService.checkAccessToken(accessToken)); |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | } | 33 | } |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java
| @@ -61,8 +61,7 @@ public class AuthController { | @@ -61,8 +61,7 @@ public class AuthController { | ||
| 61 | @ApiOperation("使用账号密码登录") | 61 | @ApiOperation("使用账号密码登录") |
| 62 | @OperateLog(enable = false) // 避免 Post 请求被记录操作日志 | 62 | @OperateLog(enable = false) // 避免 Post 请求被记录操作日志 |
| 63 | public CommonResult<AuthLoginRespVO> login(@RequestBody @Valid AuthLoginReqVO reqVO) { | 63 | public CommonResult<AuthLoginRespVO> login(@RequestBody @Valid AuthLoginReqVO reqVO) { |
| 64 | - String token = authService.login(reqVO); | ||
| 65 | - return success(AuthLoginRespVO.builder().token(token).build()); | 64 | + return success(authService.login(reqVO)); |
| 66 | } | 65 | } |
| 67 | 66 | ||
| 68 | @PostMapping("/logout") | 67 | @PostMapping("/logout") |
| @@ -114,9 +113,7 @@ public class AuthController { | @@ -114,9 +113,7 @@ public class AuthController { | ||
| 114 | @ApiOperation("使用短信验证码登录") | 113 | @ApiOperation("使用短信验证码登录") |
| 115 | @OperateLog(enable = false) // 避免 Post 请求被记录操作日志 | 114 | @OperateLog(enable = false) // 避免 Post 请求被记录操作日志 |
| 116 | public CommonResult<AuthLoginRespVO> smsLogin(@RequestBody @Valid AuthSmsLoginReqVO reqVO) { | 115 | public CommonResult<AuthLoginRespVO> smsLogin(@RequestBody @Valid AuthSmsLoginReqVO reqVO) { |
| 117 | - String token = authService.smsLogin(reqVO); | ||
| 118 | - // 返回结果 | ||
| 119 | - return success(AuthLoginRespVO.builder().token(token).build()); | 116 | + return success(authService.smsLogin(reqVO)); |
| 120 | } | 117 | } |
| 121 | 118 | ||
| 122 | @PostMapping("/send-sms-code") | 119 | @PostMapping("/send-sms-code") |
| @@ -144,16 +141,14 @@ public class AuthController { | @@ -144,16 +141,14 @@ public class AuthController { | ||
| 144 | @ApiOperation("社交快捷登录,使用 code 授权码") | 141 | @ApiOperation("社交快捷登录,使用 code 授权码") |
| 145 | @OperateLog(enable = false) // 避免 Post 请求被记录操作日志 | 142 | @OperateLog(enable = false) // 避免 Post 请求被记录操作日志 |
| 146 | public CommonResult<AuthLoginRespVO> socialQuickLogin(@RequestBody @Valid AuthSocialQuickLoginReqVO reqVO) { | 143 | public CommonResult<AuthLoginRespVO> socialQuickLogin(@RequestBody @Valid AuthSocialQuickLoginReqVO reqVO) { |
| 147 | - String token = authService.socialQuickLogin(reqVO); | ||
| 148 | - return success(AuthLoginRespVO.builder().token(token).build()); | 144 | + return success(authService.socialQuickLogin(reqVO)); |
| 149 | } | 145 | } |
| 150 | 146 | ||
| 151 | @PostMapping("/social-bind-login") | 147 | @PostMapping("/social-bind-login") |
| 152 | @ApiOperation("社交绑定登录,使用 code 授权码 + 账号密码") | 148 | @ApiOperation("社交绑定登录,使用 code 授权码 + 账号密码") |
| 153 | @OperateLog(enable = false) // 避免 Post 请求被记录操作日志 | 149 | @OperateLog(enable = false) // 避免 Post 请求被记录操作日志 |
| 154 | public CommonResult<AuthLoginRespVO> socialBindLogin(@RequestBody @Valid AuthSocialBindLoginReqVO reqVO) { | 150 | public CommonResult<AuthLoginRespVO> socialBindLogin(@RequestBody @Valid AuthSocialBindLoginReqVO reqVO) { |
| 155 | - String token = authService.socialBindLogin(reqVO); | ||
| 156 | - return success(AuthLoginRespVO.builder().token(token).build()); | 151 | + return success(authService.socialBindLogin(reqVO)); |
| 157 | } | 152 | } |
| 158 | 153 | ||
| 159 | } | 154 | } |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/UserSessionController.java deleted
| 1 | -package cn.iocoder.yudao.module.system.controller.admin.auth; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | ||
| 4 | -import cn.iocoder.yudao.framework.common.pojo.CommonResult; | ||
| 5 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 6 | -import cn.iocoder.yudao.framework.common.util.collection.MapUtils; | ||
| 7 | -import cn.iocoder.yudao.module.system.controller.admin.auth.vo.session.UserSessionPageItemRespVO; | ||
| 8 | -import cn.iocoder.yudao.module.system.controller.admin.auth.vo.session.UserSessionPageReqVO; | ||
| 9 | -import cn.iocoder.yudao.module.system.convert.auth.UserSessionConvert; | ||
| 10 | -import cn.iocoder.yudao.module.system.dal.dataobject.auth.UserSessionDO; | ||
| 11 | -import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; | ||
| 12 | -import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | ||
| 13 | -import cn.iocoder.yudao.module.system.service.auth.UserSessionService; | ||
| 14 | -import cn.iocoder.yudao.module.system.service.dept.DeptService; | ||
| 15 | -import cn.iocoder.yudao.module.system.service.user.AdminUserService; | ||
| 16 | -import io.swagger.annotations.Api; | ||
| 17 | -import io.swagger.annotations.ApiImplicitParam; | ||
| 18 | -import io.swagger.annotations.ApiOperation; | ||
| 19 | -import org.springframework.security.access.prepost.PreAuthorize; | ||
| 20 | -import org.springframework.validation.annotation.Validated; | ||
| 21 | -import org.springframework.web.bind.annotation.*; | ||
| 22 | - | ||
| 23 | -import javax.annotation.Resource; | ||
| 24 | -import java.util.ArrayList; | ||
| 25 | -import java.util.List; | ||
| 26 | -import java.util.Map; | ||
| 27 | - | ||
| 28 | -import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | ||
| 29 | -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | ||
| 30 | - | ||
| 31 | -@Api(tags = "管理后台 - 用户 Session") | ||
| 32 | -@RestController | ||
| 33 | -@RequestMapping("/system/user-session") | ||
| 34 | -public class UserSessionController { | ||
| 35 | - | ||
| 36 | - @Resource | ||
| 37 | - private UserSessionService userSessionService; | ||
| 38 | - @Resource | ||
| 39 | - private AdminUserService userService; | ||
| 40 | - | ||
| 41 | - @Resource | ||
| 42 | - private DeptService deptService; | ||
| 43 | - | ||
| 44 | - @GetMapping("/page") | ||
| 45 | - @ApiOperation("获得 Session 分页列表") | ||
| 46 | - @PreAuthorize("@ss.hasPermission('system:user-session:page')") | ||
| 47 | - public CommonResult<PageResult<UserSessionPageItemRespVO>> getUserSessionPage(@Validated UserSessionPageReqVO reqVO) { | ||
| 48 | - // 获得 Session 分页 | ||
| 49 | - PageResult<UserSessionDO> pageResult = userSessionService.getUserSessionPage(reqVO); | ||
| 50 | - | ||
| 51 | - // 获得拼接需要的数据 | ||
| 52 | - Map<Long, AdminUserDO> userMap = userService.getUserMap( | ||
| 53 | - convertList(pageResult.getList(), UserSessionDO::getUserId, | ||
| 54 | - session -> session.getUserType().equals(UserTypeEnum.ADMIN.getValue()))); | ||
| 55 | - Map<Long, DeptDO> deptMap = deptService.getDeptMap( | ||
| 56 | - convertList(userMap.values(), AdminUserDO::getDeptId)); | ||
| 57 | - // 拼接结果返回 | ||
| 58 | - List<UserSessionPageItemRespVO> sessionList = new ArrayList<>(pageResult.getList().size()); | ||
| 59 | - pageResult.getList().forEach(session -> { | ||
| 60 | - UserSessionPageItemRespVO respVO = UserSessionConvert.INSTANCE.convert(session); | ||
| 61 | - sessionList.add(respVO); | ||
| 62 | - // 设置用户账号 | ||
| 63 | - MapUtils.findAndThen(userMap, session.getUserId(), user -> { | ||
| 64 | - respVO.setUsername(user.getUsername()); | ||
| 65 | - // 设置用户部门 | ||
| 66 | - MapUtils.findAndThen(deptMap, user.getDeptId(), dept -> respVO.setDeptName(dept.getName())); | ||
| 67 | - }); | ||
| 68 | - }); | ||
| 69 | - return success(new PageResult<>(sessionList, pageResult.getTotal())); | ||
| 70 | - } | ||
| 71 | - | ||
| 72 | - @DeleteMapping("/delete") | ||
| 73 | - @ApiOperation("删除 Session") | ||
| 74 | - @ApiImplicitParam(name = "id", value = "Session 编号", required = true, dataTypeClass = Long.class, example = "1024") | ||
| 75 | - @PreAuthorize("@ss.hasPermission('system:user-session:delete')") | ||
| 76 | - public CommonResult<Boolean> deleteUserSession(@RequestParam("id") Long id) { | ||
| 77 | - userSessionService.deleteUserSession(id); | ||
| 78 | - return success(true); | ||
| 79 | - } | ||
| 80 | - | ||
| 81 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/vo/auth/AuthLoginRespVO.java
| @@ -7,14 +7,25 @@ import lombok.Builder; | @@ -7,14 +7,25 @@ import lombok.Builder; | ||
| 7 | import lombok.Data; | 7 | import lombok.Data; |
| 8 | import lombok.NoArgsConstructor; | 8 | import lombok.NoArgsConstructor; |
| 9 | 9 | ||
| 10 | -@ApiModel("管理后台 - 账号密码登录 Response VO") | 10 | +import java.util.Date; |
| 11 | + | ||
| 12 | +@ApiModel("管理后台 - 登录 Response VO") | ||
| 11 | @Data | 13 | @Data |
| 12 | @NoArgsConstructor | 14 | @NoArgsConstructor |
| 13 | @AllArgsConstructor | 15 | @AllArgsConstructor |
| 14 | @Builder | 16 | @Builder |
| 15 | public class AuthLoginRespVO { | 17 | public class AuthLoginRespVO { |
| 16 | 18 | ||
| 17 | - @ApiModelProperty(value = "token", required = true, example = "yudaoyuanma") | ||
| 18 | - private String token; | 19 | + @ApiModelProperty(value = "用户编号", required = true, example = "1024") |
| 20 | + private Long userId; | ||
| 21 | + | ||
| 22 | + @ApiModelProperty(value = "访问令牌", required = true, example = "happy") | ||
| 23 | + private String accessToken; | ||
| 24 | + | ||
| 25 | + @ApiModelProperty(value = "刷新令牌", required = true, example = "nice") | ||
| 26 | + private String refreshToken; | ||
| 27 | + | ||
| 28 | + @ApiModelProperty(value = "过期时间", required = true) | ||
| 29 | + private Date expiresTime; | ||
| 19 | 30 | ||
| 20 | } | 31 | } |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/vo/session/UserSessionPageItemRespVO.java deleted
| 1 | -package cn.iocoder.yudao.module.system.controller.admin.auth.vo.session; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.common.pojo.PageParam; | ||
| 4 | -import io.swagger.annotations.ApiModel; | ||
| 5 | -import io.swagger.annotations.ApiModelProperty; | ||
| 6 | -import lombok.AllArgsConstructor; | ||
| 7 | -import lombok.Data; | ||
| 8 | -import lombok.EqualsAndHashCode; | ||
| 9 | -import lombok.NoArgsConstructor; | ||
| 10 | - | ||
| 11 | -import java.util.Date; | ||
| 12 | - | ||
| 13 | -@ApiModel(value = "管理后台 - 用户在线 Session Response VO", description = "相比用户基本信息来说,会多部门、用户账号等信息") | ||
| 14 | -@Data | ||
| 15 | -@NoArgsConstructor | ||
| 16 | -@AllArgsConstructor | ||
| 17 | -@EqualsAndHashCode(callSuper = true) | ||
| 18 | -public class UserSessionPageItemRespVO extends PageParam { | ||
| 19 | - | ||
| 20 | - @ApiModelProperty(value = "Session 编号", required = true, example = "fe50b9f6-d177-44b1-8da9-72ea34f63db7") | ||
| 21 | - private String id; | ||
| 22 | - | ||
| 23 | - @ApiModelProperty(value = "用户 IP", required = true, example = "127.0.0.1") | ||
| 24 | - private String userIp; | ||
| 25 | - | ||
| 26 | - @ApiModelProperty(value = "浏览器 UserAgent", required = true, example = "Mozilla/5.0") | ||
| 27 | - private String userAgent; | ||
| 28 | - | ||
| 29 | - @ApiModelProperty(value = "登录时间", required = true) | ||
| 30 | - private Date createTime; | ||
| 31 | - | ||
| 32 | - @ApiModelProperty(value = "用户账号", required = true, example = "yudao") | ||
| 33 | - private String username; | ||
| 34 | - | ||
| 35 | - @ApiModelProperty(value = "部门名称", example = "研发部") | ||
| 36 | - private String deptName; | ||
| 37 | - | ||
| 38 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/vo/session/UserSessionPageReqVO.java deleted
| 1 | -package cn.iocoder.yudao.module.system.controller.admin.auth.vo.session; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.common.pojo.PageParam; | ||
| 4 | -import io.swagger.annotations.ApiModel; | ||
| 5 | -import io.swagger.annotations.ApiModelProperty; | ||
| 6 | -import lombok.Data; | ||
| 7 | -import lombok.EqualsAndHashCode; | ||
| 8 | - | ||
| 9 | -@ApiModel("管理后台 - 在线用户 Session 分页 Request VO") | ||
| 10 | -@Data | ||
| 11 | -@EqualsAndHashCode(callSuper = true) | ||
| 12 | -public class UserSessionPageReqVO extends PageParam { | ||
| 13 | - | ||
| 14 | - @ApiModelProperty(value = "用户 IP", example = "127.0.0.1", notes = "模糊匹配") | ||
| 15 | - private String userIp; | ||
| 16 | - | ||
| 17 | - @ApiModelProperty(value = "用户账号", example = "yudao", notes = "模糊匹配") | ||
| 18 | - private String username; | ||
| 19 | - | ||
| 20 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/convert/auth/AuthConvert.java
| 1 | package cn.iocoder.yudao.module.system.convert.auth; | 1 | package cn.iocoder.yudao.module.system.convert.auth; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | 3 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 4 | -import cn.iocoder.yudao.framework.security.core.LoginUser; | ||
| 5 | import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO; | 4 | import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO; |
| 6 | import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO; | 5 | import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO; |
| 7 | import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; | 6 | import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; |
| 8 | import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.*; | 7 | import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.*; |
| 8 | +import cn.iocoder.yudao.module.system.dal.dataobject.auth.OAuth2AccessTokenDO; | ||
| 9 | import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; | 9 | import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; |
| 10 | import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; | 10 | import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; |
| 11 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | 11 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| @@ -21,7 +21,7 @@ public interface AuthConvert { | @@ -21,7 +21,7 @@ public interface AuthConvert { | ||
| 21 | 21 | ||
| 22 | AuthConvert INSTANCE = Mappers.getMapper(AuthConvert.class); | 22 | AuthConvert INSTANCE = Mappers.getMapper(AuthConvert.class); |
| 23 | 23 | ||
| 24 | - LoginUser convert(AdminUserDO bean); | 24 | + AuthLoginRespVO convert(OAuth2AccessTokenDO bean); |
| 25 | 25 | ||
| 26 | default AuthPermissionInfoRespVO convert(AdminUserDO user, List<RoleDO> roleList, List<MenuDO> menuList) { | 26 | default AuthPermissionInfoRespVO convert(AdminUserDO user, List<RoleDO> roleList, List<MenuDO> menuList) { |
| 27 | return AuthPermissionInfoRespVO.builder() | 27 | return AuthPermissionInfoRespVO.builder() |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/convert/auth/UserSessionConvert.java renamed to yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/convert/auth/OAuth2TokenConvert.java
| 1 | package cn.iocoder.yudao.module.system.convert.auth; | 1 | package cn.iocoder.yudao.module.system.convert.auth; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.module.system.api.auth.dto.OAuth2AccessTokenCheckRespDTO; | 3 | import cn.iocoder.yudao.module.system.api.auth.dto.OAuth2AccessTokenCheckRespDTO; |
| 4 | -import cn.iocoder.yudao.module.system.controller.admin.auth.vo.session.UserSessionPageItemRespVO; | ||
| 5 | import cn.iocoder.yudao.module.system.dal.dataobject.auth.OAuth2AccessTokenDO; | 4 | import cn.iocoder.yudao.module.system.dal.dataobject.auth.OAuth2AccessTokenDO; |
| 6 | -import cn.iocoder.yudao.module.system.dal.dataobject.auth.UserSessionDO; | ||
| 7 | import org.mapstruct.Mapper; | 5 | import org.mapstruct.Mapper; |
| 8 | import org.mapstruct.factory.Mappers; | 6 | import org.mapstruct.factory.Mappers; |
| 9 | 7 | ||
| 10 | @Mapper | 8 | @Mapper |
| 11 | -public interface UserSessionConvert { | 9 | +public interface OAuth2TokenConvert { |
| 12 | 10 | ||
| 13 | - UserSessionConvert INSTANCE = Mappers.getMapper(UserSessionConvert.class); | ||
| 14 | - | ||
| 15 | - UserSessionPageItemRespVO convert(UserSessionDO session); | 11 | + OAuth2TokenConvert INSTANCE = Mappers.getMapper(OAuth2TokenConvert.class); |
| 16 | 12 | ||
| 17 | OAuth2AccessTokenCheckRespDTO convert(OAuth2AccessTokenDO bean); | 13 | OAuth2AccessTokenCheckRespDTO convert(OAuth2AccessTokenDO bean); |
| 18 | 14 |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/auth/UserSessionDO.java deleted
| 1 | -package cn.iocoder.yudao.module.system.dal.dataobject.auth; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | ||
| 4 | -import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; | ||
| 5 | -import cn.iocoder.yudao.framework.security.core.LoginUser; | ||
| 6 | -import com.baomidou.mybatisplus.annotation.*; | ||
| 7 | -import lombok.Builder; | ||
| 8 | -import lombok.Data; | ||
| 9 | -import lombok.EqualsAndHashCode; | ||
| 10 | - | ||
| 11 | -import java.util.Date; | ||
| 12 | - | ||
| 13 | -/** | ||
| 14 | - * 在线用户表 | ||
| 15 | - * | ||
| 16 | - * 我们已经将 {@link LoginUser} 缓存在 Redis 当中。 | ||
| 17 | - * 这里额外存储在线用户到 MySQL 中,目的是为了方便管理界面可以灵活查询。 | ||
| 18 | - * 同时,通过定时轮询 UserSessionDO 表,可以主动删除 Redis 的缓存,因为 Redis 的过期删除是延迟的。 | ||
| 19 | - * | ||
| 20 | - * @author 芋道源码 | ||
| 21 | - */ | ||
| 22 | -@TableName(value = "system_user_session") | ||
| 23 | -@KeySequence(value = "system_user_session_seq") | ||
| 24 | -@Data | ||
| 25 | -@Builder | ||
| 26 | -@EqualsAndHashCode(callSuper = true) | ||
| 27 | -@Deprecated | ||
| 28 | -public class UserSessionDO extends BaseDO { | ||
| 29 | - | ||
| 30 | - /** | ||
| 31 | - * 会话编号 | ||
| 32 | - */ | ||
| 33 | - private Long id; | ||
| 34 | - /** | ||
| 35 | - * 令牌 | ||
| 36 | - */ | ||
| 37 | - private String token; | ||
| 38 | - | ||
| 39 | - /** | ||
| 40 | - * 用户编号 | ||
| 41 | - * | ||
| 42 | - * 关联 AdminUserDO.id 或者 MemberUserDO.id | ||
| 43 | - */ | ||
| 44 | - private Long userId; | ||
| 45 | - /** | ||
| 46 | - * 用户类型 | ||
| 47 | - * | ||
| 48 | - * 枚举 {@link UserTypeEnum} | ||
| 49 | - */ | ||
| 50 | - private Integer userType; | ||
| 51 | - | ||
| 52 | - /** | ||
| 53 | - * 用户账号 | ||
| 54 | - * | ||
| 55 | - * 冗余,因为账号可以变更 | ||
| 56 | - */ | ||
| 57 | - private String username; | ||
| 58 | - | ||
| 59 | - /** | ||
| 60 | - * 用户 IP | ||
| 61 | - */ | ||
| 62 | - private String userIp; | ||
| 63 | - /** | ||
| 64 | - * 浏览器 UA | ||
| 65 | - */ | ||
| 66 | - private String userAgent; | ||
| 67 | - /** | ||
| 68 | - * 会话超时时间 | ||
| 69 | - */ | ||
| 70 | - private Date sessionTimeout; | ||
| 71 | - | ||
| 72 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/auth/UserSessionMapper.java deleted
| 1 | -package cn.iocoder.yudao.module.system.dal.mysql.auth; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 4 | -import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | ||
| 5 | -import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | ||
| 6 | -import cn.iocoder.yudao.module.system.controller.admin.auth.vo.session.UserSessionPageReqVO; | ||
| 7 | -import cn.iocoder.yudao.module.system.dal.dataobject.auth.UserSessionDO; | ||
| 8 | -import org.apache.ibatis.annotations.Mapper; | ||
| 9 | - | ||
| 10 | -import java.util.Collection; | ||
| 11 | -import java.util.Date; | ||
| 12 | -import java.util.List; | ||
| 13 | - | ||
| 14 | -@Mapper | ||
| 15 | -public interface UserSessionMapper extends BaseMapperX<UserSessionDO> { | ||
| 16 | - | ||
| 17 | - default PageResult<UserSessionDO> selectPage(UserSessionPageReqVO reqVO, Collection<Long> userIds) { | ||
| 18 | - return selectPage(reqVO, new LambdaQueryWrapperX<UserSessionDO>() | ||
| 19 | - .inIfPresent(UserSessionDO::getUserId, userIds) | ||
| 20 | - .likeIfPresent(UserSessionDO::getUserIp, reqVO.getUserIp())); | ||
| 21 | - } | ||
| 22 | - | ||
| 23 | - default List<UserSessionDO> selectListBySessionTimoutLt() { | ||
| 24 | - return selectList(new LambdaQueryWrapperX<UserSessionDO>() | ||
| 25 | - .lt(UserSessionDO::getSessionTimeout, new Date())); | ||
| 26 | - } | ||
| 27 | - | ||
| 28 | - default void updateByToken(String token, UserSessionDO updateObj) { | ||
| 29 | - update(updateObj, new LambdaQueryWrapperX<UserSessionDO>() | ||
| 30 | - .eq(UserSessionDO::getToken, token)); | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | - default void deleteByToken(String token) { | ||
| 34 | - delete(new LambdaQueryWrapperX<UserSessionDO>().eq(UserSessionDO::getToken, token)); | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/redis/RedisKeyConstants.java
| 1 | package cn.iocoder.yudao.module.system.dal.redis; | 1 | package cn.iocoder.yudao.module.system.dal.redis; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine; | 3 | import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine; |
| 4 | -import cn.iocoder.yudao.framework.security.core.LoginUser; | ||
| 5 | import cn.iocoder.yudao.module.system.dal.dataobject.auth.OAuth2AccessTokenDO; | 4 | import cn.iocoder.yudao.module.system.dal.dataobject.auth.OAuth2AccessTokenDO; |
| 6 | 5 | ||
| 7 | import java.time.Duration; | 6 | import java.time.Duration; |
| @@ -19,10 +18,6 @@ public interface RedisKeyConstants { | @@ -19,10 +18,6 @@ public interface RedisKeyConstants { | ||
| 19 | "captcha_code:%s", // 参数为 uuid | 18 | "captcha_code:%s", // 参数为 uuid |
| 20 | STRING, String.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); | 19 | STRING, String.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); |
| 21 | 20 | ||
| 22 | - RedisKeyDefine LOGIN_USER = new RedisKeyDefine("登录用户的缓存", | ||
| 23 | - "login_user:%s", // 参数为 token 令牌 | ||
| 24 | - STRING, LoginUser.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); | ||
| 25 | - | ||
| 26 | RedisKeyDefine OAUTH2_ACCESS_TOKEN = new RedisKeyDefine("访问令牌的缓存", | 21 | RedisKeyDefine OAUTH2_ACCESS_TOKEN = new RedisKeyDefine("访问令牌的缓存", |
| 27 | "oauth2_access_token:%s", // 参数为访问令牌 token | 22 | "oauth2_access_token:%s", // 参数为访问令牌 token |
| 28 | STRING, OAuth2AccessTokenDO.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); | 23 | STRING, OAuth2AccessTokenDO.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/redis/auth/LoginUserRedisDAO.java deleted
| 1 | -package cn.iocoder.yudao.module.system.dal.redis.auth; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | ||
| 4 | -import cn.iocoder.yudao.framework.security.config.SecurityProperties; | ||
| 5 | -import cn.iocoder.yudao.framework.security.core.LoginUser; | ||
| 6 | -import org.springframework.data.redis.core.StringRedisTemplate; | ||
| 7 | -import org.springframework.stereotype.Repository; | ||
| 8 | - | ||
| 9 | -import javax.annotation.Resource; | ||
| 10 | - | ||
| 11 | -import static cn.iocoder.yudao.module.system.dal.redis.RedisKeyConstants.LOGIN_USER; | ||
| 12 | - | ||
| 13 | -/** | ||
| 14 | - * {@link LoginUser} 的 RedisDAO | ||
| 15 | - * | ||
| 16 | - * @author 芋道源码 | ||
| 17 | - */ | ||
| 18 | -@Repository | ||
| 19 | -public class LoginUserRedisDAO { | ||
| 20 | - | ||
| 21 | - @Resource | ||
| 22 | - private StringRedisTemplate stringRedisTemplate; | ||
| 23 | - | ||
| 24 | - @Resource | ||
| 25 | - private SecurityProperties securityProperties; | ||
| 26 | - | ||
| 27 | - public LoginUser get(String token) { | ||
| 28 | - String redisKey = formatKey(token); | ||
| 29 | - return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), LoginUser.class); | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - public Boolean exists(String token) { | ||
| 33 | - String redisKey = formatKey(token); | ||
| 34 | - return stringRedisTemplate.hasKey(redisKey); | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | - public void set(String token, LoginUser loginUser) { | ||
| 38 | - String redisKey = formatKey(token); | ||
| 39 | - stringRedisTemplate.opsForValue().set(redisKey, JsonUtils.toJsonString(loginUser), | ||
| 40 | - securityProperties.getSessionTimeout()); | ||
| 41 | - } | ||
| 42 | - | ||
| 43 | - public void delete(String token) { | ||
| 44 | - String redisKey = formatKey(token); | ||
| 45 | - stringRedisTemplate.delete(redisKey); | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | - private static String formatKey(String token) { | ||
| 49 | - return LOGIN_USER.formatKey(token); | ||
| 50 | - } | ||
| 51 | - | ||
| 52 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthService.java
| @@ -17,9 +17,9 @@ public interface AdminAuthService { | @@ -17,9 +17,9 @@ public interface AdminAuthService { | ||
| 17 | * 账号登录 | 17 | * 账号登录 |
| 18 | * | 18 | * |
| 19 | * @param reqVO 登录信息 | 19 | * @param reqVO 登录信息 |
| 20 | - * @return 身份令牌,使用 JWT 方式 | 20 | + * @return 登录结果 |
| 21 | */ | 21 | */ |
| 22 | - String login(@Valid AuthLoginReqVO reqVO); | 22 | + AuthLoginRespVO login(@Valid AuthLoginReqVO reqVO); |
| 23 | 23 | ||
| 24 | /** | 24 | /** |
| 25 | * 基于 token 退出登录 | 25 | * 基于 token 退出登录 |
| @@ -39,26 +39,24 @@ public interface AdminAuthService { | @@ -39,26 +39,24 @@ public interface AdminAuthService { | ||
| 39 | * 短信登录 | 39 | * 短信登录 |
| 40 | * | 40 | * |
| 41 | * @param reqVO 登录信息 | 41 | * @param reqVO 登录信息 |
| 42 | - * @return 身份令牌,使用 JWT 方式 | 42 | + * @return 登录结果 |
| 43 | */ | 43 | */ |
| 44 | - String smsLogin(AuthSmsLoginReqVO reqVO) ; | 44 | + AuthLoginRespVO smsLogin(AuthSmsLoginReqVO reqVO) ; |
| 45 | 45 | ||
| 46 | /** | 46 | /** |
| 47 | * 社交快捷登录,使用 code 授权码 | 47 | * 社交快捷登录,使用 code 授权码 |
| 48 | * | 48 | * |
| 49 | * @param reqVO 登录信息 | 49 | * @param reqVO 登录信息 |
| 50 | - * @return 身份令牌,使用 JWT 方式 | 50 | + * @return 登录结果 |
| 51 | */ | 51 | */ |
| 52 | - String socialQuickLogin(@Valid AuthSocialQuickLoginReqVO reqVO); | 52 | + AuthLoginRespVO socialQuickLogin(@Valid AuthSocialQuickLoginReqVO reqVO); |
| 53 | 53 | ||
| 54 | /** | 54 | /** |
| 55 | * 社交绑定登录,使用 code 授权码 + 账号密码 | 55 | * 社交绑定登录,使用 code 授权码 + 账号密码 |
| 56 | * | 56 | * |
| 57 | * @param reqVO 登录信息 | 57 | * @param reqVO 登录信息 |
| 58 | - * @param userIp 用户 IP | ||
| 59 | - * @param userAgent 用户 UA | ||
| 60 | - * @return 身份令牌,使用 JWT 方式 | 58 | + * @return 登录结果 |
| 61 | */ | 59 | */ |
| 62 | - String socialBindLogin(@Valid AuthSocialBindLoginReqVO reqVO); | 60 | + AuthLoginRespVO socialBindLogin(@Valid AuthSocialBindLoginReqVO reqVO); |
| 63 | 61 | ||
| 64 | } | 62 | } |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java
| @@ -12,6 +12,7 @@ import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.*; | @@ -12,6 +12,7 @@ import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.*; | ||
| 12 | import cn.iocoder.yudao.module.system.convert.auth.AuthConvert; | 12 | import cn.iocoder.yudao.module.system.convert.auth.AuthConvert; |
| 13 | import cn.iocoder.yudao.module.system.dal.dataobject.auth.OAuth2AccessTokenDO; | 13 | import cn.iocoder.yudao.module.system.dal.dataobject.auth.OAuth2AccessTokenDO; |
| 14 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | 14 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| 15 | +import cn.iocoder.yudao.module.system.enums.auth.OAuth2ClientIdEnum; | ||
| 15 | import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; | 16 | import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; |
| 16 | import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; | 17 | import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; |
| 17 | import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum; | 18 | import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum; |
| @@ -58,7 +59,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | @@ -58,7 +59,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | ||
| 58 | private SmsCodeApi smsCodeApi; | 59 | private SmsCodeApi smsCodeApi; |
| 59 | 60 | ||
| 60 | @Override | 61 | @Override |
| 61 | - public String login(AuthLoginReqVO reqVO) { | 62 | + public AuthLoginRespVO login(AuthLoginReqVO reqVO) { |
| 62 | // 判断验证码是否正确 | 63 | // 判断验证码是否正确 |
| 63 | verifyCaptcha(reqVO); | 64 | verifyCaptcha(reqVO); |
| 64 | 65 | ||
| @@ -80,7 +81,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | @@ -80,7 +81,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | ||
| 80 | } | 81 | } |
| 81 | 82 | ||
| 82 | @Override | 83 | @Override |
| 83 | - public String smsLogin(AuthSmsLoginReqVO reqVO) { | 84 | + public AuthLoginRespVO smsLogin(AuthSmsLoginReqVO reqVO) { |
| 84 | // 校验验证码 | 85 | // 校验验证码 |
| 85 | smsCodeApi.useSmsCode(AuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.ADMIN_MEMBER_LOGIN.getScene(), getClientIP())); | 86 | smsCodeApi.useSmsCode(AuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.ADMIN_MEMBER_LOGIN.getScene(), getClientIP())); |
| 86 | 87 | ||
| @@ -161,7 +162,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | @@ -161,7 +162,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | ||
| 161 | } | 162 | } |
| 162 | 163 | ||
| 163 | @Override | 164 | @Override |
| 164 | - public String socialQuickLogin(AuthSocialQuickLoginReqVO reqVO) { | 165 | + public AuthLoginRespVO socialQuickLogin(AuthSocialQuickLoginReqVO reqVO) { |
| 165 | // 使用 code 授权码,进行登录。然后,获得到绑定的用户编号 | 166 | // 使用 code 授权码,进行登录。然后,获得到绑定的用户编号 |
| 166 | Long userId = socialUserService.getBindUserId(UserTypeEnum.ADMIN.getValue(), reqVO.getType(), | 167 | Long userId = socialUserService.getBindUserId(UserTypeEnum.ADMIN.getValue(), reqVO.getType(), |
| 167 | reqVO.getCode(), reqVO.getState()); | 168 | reqVO.getCode(), reqVO.getState()); |
| @@ -180,7 +181,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | @@ -180,7 +181,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | ||
| 180 | } | 181 | } |
| 181 | 182 | ||
| 182 | @Override | 183 | @Override |
| 183 | - public String socialBindLogin(AuthSocialBindLoginReqVO reqVO) { | 184 | + public AuthLoginRespVO socialBindLogin(AuthSocialBindLoginReqVO reqVO) { |
| 184 | // 使用账号密码,进行登录。 | 185 | // 使用账号密码,进行登录。 |
| 185 | AdminUserDO user = login0(reqVO.getUsername(), reqVO.getPassword()); | 186 | AdminUserDO user = login0(reqVO.getUsername(), reqVO.getPassword()); |
| 186 | 187 | ||
| @@ -191,13 +192,14 @@ public class AdminAuthServiceImpl implements AdminAuthService { | @@ -191,13 +192,14 @@ public class AdminAuthServiceImpl implements AdminAuthService { | ||
| 191 | return createTokenAfterLoginSuccess(user.getId(), reqVO.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL); | 192 | return createTokenAfterLoginSuccess(user.getId(), reqVO.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL); |
| 192 | } | 193 | } |
| 193 | 194 | ||
| 194 | - private String createTokenAfterLoginSuccess(Long userId, String username, LoginLogTypeEnum logType) { | 195 | + private AuthLoginRespVO createTokenAfterLoginSuccess(Long userId, String username, LoginLogTypeEnum logType) { |
| 195 | // 插入登陆日志 | 196 | // 插入登陆日志 |
| 196 | createLoginLog(userId, username, logType, LoginResultEnum.SUCCESS); | 197 | createLoginLog(userId, username, logType, LoginResultEnum.SUCCESS); |
| 197 | // 创建访问令牌 | 198 | // 创建访问令牌 |
| 198 | - // TODO clientId | ||
| 199 | - return oauth2TokenService.createAccessToken(userId, getUserType().getValue(), 1L) | ||
| 200 | - .getAccessToken(); | 199 | + OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.createAccessToken(userId, getUserType().getValue(), |
| 200 | + OAuth2ClientIdEnum.DEFAULT.getId()); | ||
| 201 | + // 构建返回结果 | ||
| 202 | + return AuthConvert.INSTANCE.convert(accessTokenDO); | ||
| 201 | } | 203 | } |
| 202 | 204 | ||
| 203 | @Override | 205 | @Override |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/UserSessionService.java deleted
| 1 | -package cn.iocoder.yudao.module.system.service.auth; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 4 | -import cn.iocoder.yudao.module.system.controller.admin.auth.vo.session.UserSessionPageReqVO; | ||
| 5 | -import cn.iocoder.yudao.module.system.dal.dataobject.auth.UserSessionDO; | ||
| 6 | - | ||
| 7 | -/** | ||
| 8 | - * 在线用户 Session Service 接口 | ||
| 9 | - * | ||
| 10 | - * @author 芋道源码 | ||
| 11 | - */ | ||
| 12 | -public interface UserSessionService { | ||
| 13 | - | ||
| 14 | - /** | ||
| 15 | - * 获得在线用户分页列表 | ||
| 16 | - * | ||
| 17 | - * @param reqVO 分页条件 | ||
| 18 | - * @return 份额与列表 | ||
| 19 | - */ | ||
| 20 | - PageResult<UserSessionDO> getUserSessionPage(UserSessionPageReqVO reqVO); | ||
| 21 | - | ||
| 22 | - /** | ||
| 23 | - * 删除在线用户 Session | ||
| 24 | - * | ||
| 25 | - * @param token token 令牌 | ||
| 26 | - */ | ||
| 27 | - void deleteUserSession(String token); | ||
| 28 | - | ||
| 29 | - /** | ||
| 30 | - * 删除在线用户 Session | ||
| 31 | - * | ||
| 32 | - * @param id 编号 | ||
| 33 | - */ | ||
| 34 | - void deleteUserSession(Long id); | ||
| 35 | - | ||
| 36 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/UserSessionServiceImpl.java deleted
| 1 | -package cn.iocoder.yudao.module.system.service.auth; | ||
| 2 | - | ||
| 3 | -import cn.hutool.core.collection.CollUtil; | ||
| 4 | -import cn.hutool.core.util.StrUtil; | ||
| 5 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 6 | -import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; | ||
| 7 | -import cn.iocoder.yudao.framework.security.config.SecurityProperties; | ||
| 8 | -import cn.iocoder.yudao.module.system.api.logger.dto.LoginLogCreateReqDTO; | ||
| 9 | -import cn.iocoder.yudao.module.system.controller.admin.auth.vo.session.UserSessionPageReqVO; | ||
| 10 | -import cn.iocoder.yudao.module.system.dal.dataobject.auth.UserSessionDO; | ||
| 11 | -import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | ||
| 12 | -import cn.iocoder.yudao.module.system.dal.mysql.auth.UserSessionMapper; | ||
| 13 | -import cn.iocoder.yudao.module.system.dal.redis.auth.LoginUserRedisDAO; | ||
| 14 | -import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; | ||
| 15 | -import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; | ||
| 16 | -import cn.iocoder.yudao.module.system.service.logger.LoginLogService; | ||
| 17 | -import cn.iocoder.yudao.module.system.service.user.AdminUserService; | ||
| 18 | -import lombok.extern.slf4j.Slf4j; | ||
| 19 | -import org.springframework.stereotype.Service; | ||
| 20 | - | ||
| 21 | -import javax.annotation.Resource; | ||
| 22 | -import java.util.Collection; | ||
| 23 | - | ||
| 24 | -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; | ||
| 25 | - | ||
| 26 | -/** | ||
| 27 | - * 在线用户 Session Service 实现类 | ||
| 28 | - * | ||
| 29 | - * @author 芋道源码 | ||
| 30 | - */ | ||
| 31 | -@Slf4j | ||
| 32 | -@Service | ||
| 33 | -public class UserSessionServiceImpl implements UserSessionService { | ||
| 34 | - | ||
| 35 | - @Resource | ||
| 36 | - private UserSessionMapper userSessionMapper; | ||
| 37 | - | ||
| 38 | - @Resource | ||
| 39 | - private AdminUserService userService; | ||
| 40 | - @Resource | ||
| 41 | - private LoginLogService loginLogService; | ||
| 42 | - | ||
| 43 | - @Resource | ||
| 44 | - private LoginUserRedisDAO loginUserRedisDAO; | ||
| 45 | - | ||
| 46 | - @Resource | ||
| 47 | - private SecurityProperties securityProperties; | ||
| 48 | - | ||
| 49 | - @Override | ||
| 50 | - public PageResult<UserSessionDO> getUserSessionPage(UserSessionPageReqVO reqVO) { | ||
| 51 | - // 处理基于用户昵称的查询 | ||
| 52 | - Collection<Long> userIds = null; | ||
| 53 | - if (StrUtil.isNotEmpty(reqVO.getUsername())) { | ||
| 54 | - userIds = convertSet(userService.getUsersByUsername(reqVO.getUsername()), AdminUserDO::getId); | ||
| 55 | - if (CollUtil.isEmpty(userIds)) { | ||
| 56 | - return PageResult.empty(); | ||
| 57 | - } | ||
| 58 | - } | ||
| 59 | - return userSessionMapper.selectPage(reqVO, userIds); | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - private void createLogoutLog(UserSessionDO session, LoginLogTypeEnum type) { | ||
| 63 | - LoginLogCreateReqDTO reqDTO = new LoginLogCreateReqDTO(); | ||
| 64 | - reqDTO.setLogType(type.getType()); | ||
| 65 | - reqDTO.setTraceId(TracerUtils.getTraceId()); | ||
| 66 | - reqDTO.setUserId(session.getUserId()); | ||
| 67 | - reqDTO.setUserType(session.getUserType()); | ||
| 68 | - reqDTO.setUsername(session.getUsername()); | ||
| 69 | - reqDTO.setUserAgent(session.getUserAgent()); | ||
| 70 | - reqDTO.setUserIp(session.getUserIp()); | ||
| 71 | - reqDTO.setResult(LoginResultEnum.SUCCESS.getResult()); | ||
| 72 | - loginLogService.createLoginLog(reqDTO); | ||
| 73 | - } | ||
| 74 | - | ||
| 75 | - @Override | ||
| 76 | - public void deleteUserSession(String token) { | ||
| 77 | - // 删除 Redis 缓存 | ||
| 78 | - loginUserRedisDAO.delete(token); | ||
| 79 | - // 删除 DB 记录 | ||
| 80 | - userSessionMapper.deleteByToken(token); | ||
| 81 | - // 无需记录日志,因为退出那已经记录 | ||
| 82 | - } | ||
| 83 | - | ||
| 84 | - @Override | ||
| 85 | - public void deleteUserSession(Long id) { | ||
| 86 | - UserSessionDO session = userSessionMapper.selectById(id); | ||
| 87 | - if (session == null) { | ||
| 88 | - return; | ||
| 89 | - } | ||
| 90 | - // 删除 Redis 缓存 | ||
| 91 | - loginUserRedisDAO.delete(session.getToken()); | ||
| 92 | - // 删除 DB 记录 | ||
| 93 | - userSessionMapper.deleteById(id); | ||
| 94 | - // 记录退出日志 | ||
| 95 | - createLogoutLog(session, LoginLogTypeEnum.LOGOUT_DELETE); | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | -} |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/auth/AuthServiceImplTest.java
| 1 | package cn.iocoder.yudao.module.system.service.auth; | 1 | package cn.iocoder.yudao.module.system.service.auth; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 4 | -import cn.iocoder.yudao.framework.security.core.LoginUser; | 4 | +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 5 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | 5 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 6 | import cn.iocoder.yudao.framework.test.core.util.AssertUtils; | 6 | import cn.iocoder.yudao.framework.test.core.util.AssertUtils; |
| 7 | import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi; | 7 | import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi; |
| 8 | import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthLoginReqVO; | 8 | import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthLoginReqVO; |
| 9 | +import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.AuthLoginRespVO; | ||
| 10 | +import cn.iocoder.yudao.module.system.dal.dataobject.auth.OAuth2AccessTokenDO; | ||
| 9 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | 11 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| 10 | import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; | 12 | import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; |
| 11 | import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; | 13 | import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; |
| @@ -26,7 +28,6 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic | @@ -26,7 +28,6 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic | ||
| 26 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | 28 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 27 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; | 29 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; |
| 28 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | 30 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 29 | -import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 30 | import static org.mockito.ArgumentMatchers.eq; | 31 | import static org.mockito.ArgumentMatchers.eq; |
| 31 | import static org.mockito.Mockito.*; | 32 | import static org.mockito.Mockito.*; |
| 32 | 33 | ||
| @@ -43,11 +44,11 @@ public class AuthServiceImplTest extends BaseDbUnitTest { | @@ -43,11 +44,11 @@ public class AuthServiceImplTest extends BaseDbUnitTest { | ||
| 43 | @MockBean | 44 | @MockBean |
| 44 | private LoginLogService loginLogService; | 45 | private LoginLogService loginLogService; |
| 45 | @MockBean | 46 | @MockBean |
| 46 | - private UserSessionService userSessionService; | ||
| 47 | - @MockBean | ||
| 48 | private SocialUserService socialService; | 47 | private SocialUserService socialService; |
| 49 | @MockBean | 48 | @MockBean |
| 50 | private SmsCodeApi smsCodeApi; | 49 | private SmsCodeApi smsCodeApi; |
| 50 | + @MockBean | ||
| 51 | + private OAuth2TokenService oauth2TokenService; | ||
| 51 | 52 | ||
| 52 | @MockBean | 53 | @MockBean |
| 53 | private Validator validator; | 54 | private Validator validator; |
| @@ -188,22 +189,20 @@ public class AuthServiceImplTest extends BaseDbUnitTest { | @@ -188,22 +189,20 @@ public class AuthServiceImplTest extends BaseDbUnitTest { | ||
| 188 | // mock 验证码正确 | 189 | // mock 验证码正确 |
| 189 | when(captchaService.getCaptchaCode(reqVO.getUuid())).thenReturn(reqVO.getCode()); | 190 | when(captchaService.getCaptchaCode(reqVO.getUuid())).thenReturn(reqVO.getCode()); |
| 190 | // mock user 数据 | 191 | // mock user 数据 |
| 191 | - AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setUsername("test_username") | 192 | + AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setId(1L).setUsername("test_username") |
| 192 | .setPassword("test_password").setStatus(CommonStatusEnum.ENABLE.getStatus())); | 193 | .setPassword("test_password").setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 193 | when(userService.getUserByUsername(eq("test_username"))).thenReturn(user); | 194 | when(userService.getUserByUsername(eq("test_username"))).thenReturn(user); |
| 194 | // mock password 匹配 | 195 | // mock password 匹配 |
| 195 | when(userService.isPasswordMatch(eq("test_password"), eq(user.getPassword()))).thenReturn(true); | 196 | when(userService.isPasswordMatch(eq("test_password"), eq(user.getPassword()))).thenReturn(true); |
| 196 | // mock 缓存登录用户到 Redis | 197 | // mock 缓存登录用户到 Redis |
| 197 | - String token = randomString(); | ||
| 198 | -// when(userSessionService.createUserSession(argThat(argument -> { | ||
| 199 | -// AssertUtils.assertPojoEquals(user, argument); | ||
| 200 | -// return true; | ||
| 201 | -// }), eq(userIp), eq(userAgent))).thenReturn(token); | ||
| 202 | - // TODO 芋艿:oauth2 | 198 | + OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class, o -> o.setUserId(1L) |
| 199 | + .setUserType(UserTypeEnum.ADMIN.getValue())); | ||
| 200 | + when(oauth2TokenService.createAccessToken(eq(1L), eq(UserTypeEnum.ADMIN.getValue()), eq(1L))) | ||
| 201 | + .thenReturn(accessTokenDO); | ||
| 203 | 202 | ||
| 204 | // 调用, 并断言异常 | 203 | // 调用, 并断言异常 |
| 205 | - String result = authService.login(reqVO); | ||
| 206 | - assertEquals(token, result); | 204 | + AuthLoginRespVO loginRespVO = authService.login(reqVO); |
| 205 | + assertPojoEquals(accessTokenDO, loginRespVO); | ||
| 207 | // 校验调用参数 | 206 | // 校验调用参数 |
| 208 | verify(loginLogService).createLoginLog( | 207 | verify(loginLogService).createLoginLog( |
| 209 | argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType()) | 208 | argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType()) |
| @@ -216,18 +215,28 @@ public class AuthServiceImplTest extends BaseDbUnitTest { | @@ -216,18 +215,28 @@ public class AuthServiceImplTest extends BaseDbUnitTest { | ||
| 216 | public void testLogout_success() { | 215 | public void testLogout_success() { |
| 217 | // 准备参数 | 216 | // 准备参数 |
| 218 | String token = randomString(); | 217 | String token = randomString(); |
| 219 | - LoginUser loginUser = randomPojo(LoginUser.class); | ||
| 220 | // mock | 218 | // mock |
| 221 | -// when(userSessionService.getLoginUser(token)).thenReturn(loginUser); | ||
| 222 | - // TODO @芋艿:oauth2 | 219 | + OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class, o -> o.setUserId(1L) |
| 220 | + .setUserType(UserTypeEnum.ADMIN.getValue())); | ||
| 221 | + when(oauth2TokenService.removeAccessToken(eq(token))).thenReturn(accessTokenDO); | ||
| 222 | + | ||
| 223 | // 调用 | 223 | // 调用 |
| 224 | authService.logout(token); | 224 | authService.logout(token); |
| 225 | // 校验调用参数 | 225 | // 校验调用参数 |
| 226 | - verify(userSessionService, times(1)).deleteUserSession(token); | ||
| 227 | - verify(loginLogService, times(1)).createLoginLog( | ||
| 228 | - argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGOUT_SELF.getType()) | 226 | + verify(loginLogService).createLoginLog(argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGOUT_SELF.getType()) |
| 229 | && o.getResult().equals(LoginResultEnum.SUCCESS.getResult())) | 227 | && o.getResult().equals(LoginResultEnum.SUCCESS.getResult())) |
| 230 | ); | 228 | ); |
| 231 | } | 229 | } |
| 232 | 230 | ||
| 231 | + @Test | ||
| 232 | + public void testLogout_fail() { | ||
| 233 | + // 准备参数 | ||
| 234 | + String token = randomString(); | ||
| 235 | + | ||
| 236 | + // 调用 | ||
| 237 | + authService.logout(token); | ||
| 238 | + // 校验调用参数 | ||
| 239 | + verify(loginLogService, never()).createLoginLog(any()); | ||
| 240 | + } | ||
| 241 | + | ||
| 233 | } | 242 | } |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/auth/UserSessionServiceImplTest.java deleted
| 1 | -package cn.iocoder.yudao.module.system.service.auth; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||
| 4 | -import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | ||
| 5 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 6 | -import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | ||
| 7 | -import cn.iocoder.yudao.framework.security.config.SecurityProperties; | ||
| 8 | -import cn.iocoder.yudao.framework.security.core.LoginUser; | ||
| 9 | -import cn.iocoder.yudao.framework.test.core.ut.BaseDbAndRedisUnitTest; | ||
| 10 | -import cn.iocoder.yudao.module.system.controller.admin.auth.vo.session.UserSessionPageReqVO; | ||
| 11 | -import cn.iocoder.yudao.module.system.dal.dataobject.auth.UserSessionDO; | ||
| 12 | -import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | ||
| 13 | -import cn.iocoder.yudao.module.system.dal.mysql.auth.UserSessionMapper; | ||
| 14 | -import cn.iocoder.yudao.module.system.dal.redis.auth.LoginUserRedisDAO; | ||
| 15 | -import cn.iocoder.yudao.module.system.enums.common.SexEnum; | ||
| 16 | -import cn.iocoder.yudao.module.system.service.logger.LoginLogService; | ||
| 17 | -import cn.iocoder.yudao.module.system.service.user.AdminUserService; | ||
| 18 | -import org.junit.jupiter.api.BeforeEach; | ||
| 19 | -import org.junit.jupiter.api.Test; | ||
| 20 | -import org.springframework.boot.test.mock.mockito.MockBean; | ||
| 21 | -import org.springframework.context.annotation.Import; | ||
| 22 | - | ||
| 23 | -import javax.annotation.Resource; | ||
| 24 | -import java.time.Duration; | ||
| 25 | - | ||
| 26 | -import static cn.hutool.core.util.RandomUtil.randomEle; | ||
| 27 | -import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; | ||
| 28 | -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | ||
| 29 | -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; | ||
| 30 | -import static java.util.Collections.singletonList; | ||
| 31 | -import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 32 | -import static org.junit.jupiter.api.Assertions.assertNull; | ||
| 33 | -import static org.mockito.ArgumentMatchers.eq; | ||
| 34 | -import static org.mockito.Mockito.when; | ||
| 35 | - | ||
| 36 | -/** | ||
| 37 | - * {@link UserSessionServiceImpl} 的单元测试 | ||
| 38 | - * | ||
| 39 | - * @author Lyon | ||
| 40 | - */ | ||
| 41 | -@Import({UserSessionServiceImpl.class, LoginUserRedisDAO.class}) | ||
| 42 | -public class UserSessionServiceImplTest extends BaseDbAndRedisUnitTest { | ||
| 43 | - | ||
| 44 | - @Resource | ||
| 45 | - private UserSessionServiceImpl userSessionService; | ||
| 46 | - | ||
| 47 | - @Resource | ||
| 48 | - private UserSessionMapper userSessionMapper; | ||
| 49 | - | ||
| 50 | - @MockBean | ||
| 51 | - private AdminUserService userService; | ||
| 52 | - @MockBean | ||
| 53 | - private LoginLogService loginLogService; | ||
| 54 | - @Resource | ||
| 55 | - private LoginUserRedisDAO loginUserRedisDAO; | ||
| 56 | - | ||
| 57 | - @MockBean | ||
| 58 | - private SecurityProperties securityProperties; | ||
| 59 | - | ||
| 60 | - @BeforeEach | ||
| 61 | - public void setUp() { | ||
| 62 | - when(securityProperties.getSessionTimeout()).thenReturn(Duration.ofDays(1L)); | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - @Test | ||
| 66 | - public void testGetUserSessionPage_success() { | ||
| 67 | - // mock 数据 | ||
| 68 | - AdminUserDO dbUser = randomPojo(AdminUserDO.class, o -> { | ||
| 69 | - o.setSex(randomEle(SexEnum.values()).getSex()); | ||
| 70 | - o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||
| 71 | - }); | ||
| 72 | - when(userService.getUsersByUsername(eq(dbUser.getUsername()))).thenReturn(singletonList(dbUser)); | ||
| 73 | - // 插入可被查询到的数据 | ||
| 74 | - String userIp = randomString(); | ||
| 75 | - UserSessionDO dbSession = randomPojo(UserSessionDO.class, o -> { | ||
| 76 | - o.setUserId(dbUser.getId()); | ||
| 77 | - o.setUserType(randomEle(UserTypeEnum.values()).getValue()); | ||
| 78 | - o.setUserIp(userIp); | ||
| 79 | - }); | ||
| 80 | - userSessionMapper.insert(dbSession); | ||
| 81 | - // 测试 username 不匹配 | ||
| 82 | - userSessionMapper.insert(ObjectUtils.cloneIgnoreId(dbSession, o -> o.setUserId(123456L))); | ||
| 83 | - // 测试 userIp 不匹配 | ||
| 84 | - userSessionMapper.insert(ObjectUtils.cloneIgnoreId(dbSession, o -> o.setUserIp("testUserIp"))); | ||
| 85 | - // 准备参数 | ||
| 86 | - UserSessionPageReqVO reqVO = new UserSessionPageReqVO(); | ||
| 87 | - reqVO.setUsername(dbUser.getUsername()); | ||
| 88 | - reqVO.setUserIp(userIp); | ||
| 89 | - | ||
| 90 | - // 调用 | ||
| 91 | - PageResult<UserSessionDO> pageResult = userSessionService.getUserSessionPage(reqVO); | ||
| 92 | - // 断言 | ||
| 93 | - assertEquals(1, pageResult.getTotal()); | ||
| 94 | - assertEquals(1, pageResult.getList().size()); | ||
| 95 | - assertPojoEquals(dbSession, pageResult.getList().get(0)); | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | - @Test | ||
| 99 | - public void testDeleteUserSession_Token() { | ||
| 100 | - // 准备参数 | ||
| 101 | - String token = randomString(); | ||
| 102 | - | ||
| 103 | - // mock redis 数据 | ||
| 104 | - loginUserRedisDAO.set(token, new LoginUser()); | ||
| 105 | - // mock db 数据 | ||
| 106 | - UserSessionDO userSession = randomPojo(UserSessionDO.class, o -> { | ||
| 107 | - o.setUserType(randomEle(UserTypeEnum.values()).getValue()); | ||
| 108 | - o.setToken(token); | ||
| 109 | - }); | ||
| 110 | - userSessionMapper.insert(userSession); | ||
| 111 | - | ||
| 112 | - // 调用 | ||
| 113 | - userSessionService.deleteUserSession(token); | ||
| 114 | - // 校验数据不存在了 | ||
| 115 | - assertNull(loginUserRedisDAO.get(token)); | ||
| 116 | - assertNull(userSessionMapper.selectOne(UserSessionDO::getToken, token)); | ||
| 117 | - } | ||
| 118 | - | ||
| 119 | - @Test | ||
| 120 | - public void testDeleteUserSession_Id() { | ||
| 121 | - // mock db 数据 | ||
| 122 | - UserSessionDO userSession = randomPojo(UserSessionDO.class, o -> { | ||
| 123 | - o.setUserType(randomEle(UserTypeEnum.values()).getValue()); | ||
| 124 | - }); | ||
| 125 | - userSessionMapper.insert(userSession); | ||
| 126 | - // mock redis 数据 | ||
| 127 | - loginUserRedisDAO.set(userSession.getToken(), new LoginUser()); | ||
| 128 | - | ||
| 129 | - // 准备参数 | ||
| 130 | - Long id = userSession.getId(); | ||
| 131 | - | ||
| 132 | - // 调用 | ||
| 133 | - userSessionService.deleteUserSession(id); | ||
| 134 | - // 校验数据不存在了 | ||
| 135 | - assertNull(loginUserRedisDAO.get(userSession.getToken())); | ||
| 136 | - assertNull(userSessionMapper.selectById(id)); | ||
| 137 | - } | ||
| 138 | - | ||
| 139 | -} |
yudao-ui-admin/src/components/Editor/index.vue
| @@ -22,7 +22,7 @@ import Quill from "quill"; | @@ -22,7 +22,7 @@ import Quill from "quill"; | ||
| 22 | import "quill/dist/quill.core.css"; | 22 | import "quill/dist/quill.core.css"; |
| 23 | import "quill/dist/quill.snow.css"; | 23 | import "quill/dist/quill.snow.css"; |
| 24 | import "quill/dist/quill.bubble.css"; | 24 | import "quill/dist/quill.bubble.css"; |
| 25 | -import { getToken } from "@/utils/auth"; | 25 | +import { getAccessToken } from "@/utils/auth"; |
| 26 | 26 | ||
| 27 | export default { | 27 | export default { |
| 28 | name: "Editor", | 28 | name: "Editor", |
| @@ -62,7 +62,7 @@ export default { | @@ -62,7 +62,7 @@ export default { | ||
| 62 | return { | 62 | return { |
| 63 | uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址 | 63 | uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址 |
| 64 | headers: { | 64 | headers: { |
| 65 | - Authorization: "Bearer " + getToken() | 65 | + Authorization: "Bearer " + getAccessToken() |
| 66 | }, | 66 | }, |
| 67 | Quill: null, | 67 | Quill: null, |
| 68 | currentValue: "", | 68 | currentValue: "", |
yudao-ui-admin/src/components/FileUpload/index.vue
| @@ -40,7 +40,7 @@ | @@ -40,7 +40,7 @@ | ||
| 40 | </template> | 40 | </template> |
| 41 | 41 | ||
| 42 | <script> | 42 | <script> |
| 43 | -import { getToken } from "@/utils/auth"; | 43 | +import { getAccessToken } from "@/utils/auth"; |
| 44 | 44 | ||
| 45 | export default { | 45 | export default { |
| 46 | name: "FileUpload", | 46 | name: "FileUpload", |
| @@ -75,7 +75,7 @@ export default { | @@ -75,7 +75,7 @@ export default { | ||
| 75 | baseUrl: process.env.VUE_APP_BASE_API, | 75 | baseUrl: process.env.VUE_APP_BASE_API, |
| 76 | uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址 | 76 | uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址 |
| 77 | headers: { | 77 | headers: { |
| 78 | - Authorization: "Bearer " + getToken(), | 78 | + Authorization: "Bearer " + getAccessToken(), |
| 79 | }, | 79 | }, |
| 80 | fileList: [], | 80 | fileList: [], |
| 81 | }; | 81 | }; |
yudao-ui-admin/src/components/ImageUpload/index.vue
| @@ -43,7 +43,7 @@ | @@ -43,7 +43,7 @@ | ||
| 43 | </template> | 43 | </template> |
| 44 | 44 | ||
| 45 | <script> | 45 | <script> |
| 46 | -import { getToken } from "@/utils/auth"; | 46 | +import { getAccessToken } from "@/utils/auth"; |
| 47 | 47 | ||
| 48 | export default { | 48 | export default { |
| 49 | props: { | 49 | props: { |
| @@ -79,7 +79,7 @@ export default { | @@ -79,7 +79,7 @@ export default { | ||
| 79 | baseUrl: process.env.VUE_APP_BASE_API, | 79 | baseUrl: process.env.VUE_APP_BASE_API, |
| 80 | uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址 | 80 | uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址 |
| 81 | headers: { | 81 | headers: { |
| 82 | - Authorization: "Bearer " + getToken(), | 82 | + Authorization: "Bearer " + getAccessToken(), |
| 83 | }, | 83 | }, |
| 84 | fileList: [] | 84 | fileList: [] |
| 85 | }; | 85 | }; |
yudao-ui-admin/src/components/UploadImage/index.vue
| @@ -18,7 +18,7 @@ | @@ -18,7 +18,7 @@ | ||
| 18 | </template> | 18 | </template> |
| 19 | 19 | ||
| 20 | <script> | 20 | <script> |
| 21 | -import { getToken } from "@/utils/auth"; | 21 | +import { getAccessToken } from "@/utils/auth"; |
| 22 | 22 | ||
| 23 | export default { | 23 | export default { |
| 24 | components: {}, | 24 | components: {}, |
| @@ -26,7 +26,7 @@ export default { | @@ -26,7 +26,7 @@ export default { | ||
| 26 | return { | 26 | return { |
| 27 | uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址 | 27 | uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址 |
| 28 | headers: { | 28 | headers: { |
| 29 | - Authorization: "Bearer " + getToken(), | 29 | + Authorization: "Bearer " + getAccessToken(), |
| 30 | }, | 30 | }, |
| 31 | }; | 31 | }; |
| 32 | }, | 32 | }, |
| @@ -65,4 +65,4 @@ export default { | @@ -65,4 +65,4 @@ export default { | ||
| 65 | width: 100%; | 65 | width: 100%; |
| 66 | height: 100%; | 66 | height: 100%; |
| 67 | } | 67 | } |
| 68 | -</style> | ||
| 69 | \ No newline at end of file | 68 | \ No newline at end of file |
| 69 | +</style> |
yudao-ui-admin/src/permission.js
| @@ -3,7 +3,7 @@ import store from './store' | @@ -3,7 +3,7 @@ import store from './store' | ||
| 3 | import { Message } from 'element-ui' | 3 | import { Message } from 'element-ui' |
| 4 | import NProgress from 'nprogress' | 4 | import NProgress from 'nprogress' |
| 5 | import 'nprogress/nprogress.css' | 5 | import 'nprogress/nprogress.css' |
| 6 | -import { getToken } from '@/utils/auth' | 6 | +import { getAccessToken } from '@/utils/auth' |
| 7 | import { isRelogin } from '@/utils/request' | 7 | import { isRelogin } from '@/utils/request' |
| 8 | 8 | ||
| 9 | NProgress.configure({ showSpinner: false }) | 9 | NProgress.configure({ showSpinner: false }) |
| @@ -13,7 +13,7 @@ const whiteList = ['/login', '/social-login', '/auth-redirect', '/bind', '/regi | @@ -13,7 +13,7 @@ const whiteList = ['/login', '/social-login', '/auth-redirect', '/bind', '/regi | ||
| 13 | 13 | ||
| 14 | router.beforeEach((to, from, next) => { | 14 | router.beforeEach((to, from, next) => { |
| 15 | NProgress.start() | 15 | NProgress.start() |
| 16 | - if (getToken()) { | 16 | + if (getAccessToken()) { |
| 17 | to.meta.title && store.dispatch('settings/setTitle', to.meta.title) | 17 | to.meta.title && store.dispatch('settings/setTitle', to.meta.title) |
| 18 | /* has token*/ | 18 | /* has token*/ |
| 19 | if (to.path === '/login') { | 19 | if (to.path === '/login') { |
yudao-ui-admin/src/store/modules/user.js
| 1 | import {login, logout, getInfo, socialQuickLogin, socialBindLogin, smsLogin} from '@/api/login' | 1 | import {login, logout, getInfo, socialQuickLogin, socialBindLogin, smsLogin} from '@/api/login' |
| 2 | -import { getToken, setToken, removeToken } from '@/utils/auth' | 2 | +import {getAccessToken, setToken, removeToken, getRefreshToken} from '@/utils/auth' |
| 3 | 3 | ||
| 4 | const user = { | 4 | const user = { |
| 5 | state: { | 5 | state: { |
| 6 | - token: getToken(), | 6 | + accessToken: getAccessToken(), |
| 7 | + refreshToken: getRefreshToken(), | ||
| 7 | id: 0, // 用户编号 | 8 | id: 0, // 用户编号 |
| 8 | name: '', | 9 | name: '', |
| 9 | avatar: '', | 10 | avatar: '', |
| @@ -16,7 +17,8 @@ const user = { | @@ -16,7 +17,8 @@ const user = { | ||
| 16 | state.id = id | 17 | state.id = id |
| 17 | }, | 18 | }, |
| 18 | SET_TOKEN: (state, token) => { | 19 | SET_TOKEN: (state, token) => { |
| 19 | - state.token = token | 20 | + state.accessToken = token.accessToken |
| 21 | + state.refreshToken = token.refreshToken | ||
| 20 | }, | 22 | }, |
| 21 | SET_NAME: (state, name) => { | 23 | SET_NAME: (state, name) => { |
| 22 | state.name = name | 24 | state.name = name |
| @@ -42,8 +44,9 @@ const user = { | @@ -42,8 +44,9 @@ const user = { | ||
| 42 | return new Promise((resolve, reject) => { | 44 | return new Promise((resolve, reject) => { |
| 43 | login(username, password, code, uuid).then(res => { | 45 | login(username, password, code, uuid).then(res => { |
| 44 | res = res.data; | 46 | res = res.data; |
| 45 | - setToken(res.token) | ||
| 46 | - commit('SET_TOKEN', res.token) | 47 | + // 设置 token |
| 48 | + setToken(res) | ||
| 49 | + commit('SET_TOKEN', res) | ||
| 47 | resolve() | 50 | resolve() |
| 48 | }).catch(error => { | 51 | }).catch(error => { |
| 49 | reject(error) | 52 | reject(error) |
| @@ -59,8 +62,9 @@ const user = { | @@ -59,8 +62,9 @@ const user = { | ||
| 59 | return new Promise((resolve, reject) => { | 62 | return new Promise((resolve, reject) => { |
| 60 | socialQuickLogin(type, code, state).then(res => { | 63 | socialQuickLogin(type, code, state).then(res => { |
| 61 | res = res.data; | 64 | res = res.data; |
| 62 | - setToken(res.token) | ||
| 63 | - commit('SET_TOKEN', res.token) | 65 | + // 设置 token |
| 66 | + setToken(res) | ||
| 67 | + commit('SET_TOKEN', res) | ||
| 64 | resolve() | 68 | resolve() |
| 65 | }).catch(error => { | 69 | }).catch(error => { |
| 66 | reject(error) | 70 | reject(error) |
| @@ -78,8 +82,9 @@ const user = { | @@ -78,8 +82,9 @@ const user = { | ||
| 78 | return new Promise((resolve, reject) => { | 82 | return new Promise((resolve, reject) => { |
| 79 | socialBindLogin(type, code, state, username, password).then(res => { | 83 | socialBindLogin(type, code, state, username, password).then(res => { |
| 80 | res = res.data; | 84 | res = res.data; |
| 81 | - setToken(res.token) | ||
| 82 | - commit('SET_TOKEN', res.token) | 85 | + // 设置 token |
| 86 | + setToken(res) | ||
| 87 | + commit('SET_TOKEN', res) | ||
| 83 | resolve() | 88 | resolve() |
| 84 | }).catch(error => { | 89 | }).catch(error => { |
| 85 | reject(error) | 90 | reject(error) |
| @@ -93,8 +98,9 @@ const user = { | @@ -93,8 +98,9 @@ const user = { | ||
| 93 | return new Promise((resolve, reject) => { | 98 | return new Promise((resolve, reject) => { |
| 94 | smsLogin(mobile,mobileCode).then(res => { | 99 | smsLogin(mobile,mobileCode).then(res => { |
| 95 | res = res.data; | 100 | res = res.data; |
| 96 | - setToken(res.token) | ||
| 97 | - commit('SET_TOKEN', res.token) | 101 | + // 设置 token |
| 102 | + setToken(res) | ||
| 103 | + commit('SET_TOKEN', res) | ||
| 98 | resolve() | 104 | resolve() |
| 99 | }).catch(error => { | 105 | }).catch(error => { |
| 100 | reject(error) | 106 | reject(error) |
| @@ -151,15 +157,6 @@ const user = { | @@ -151,15 +157,6 @@ const user = { | ||
| 151 | reject(error) | 157 | reject(error) |
| 152 | }) | 158 | }) |
| 153 | }) | 159 | }) |
| 154 | - }, | ||
| 155 | - | ||
| 156 | - // 前端 登出 | ||
| 157 | - FedLogOut({ commit }) { | ||
| 158 | - return new Promise(resolve => { | ||
| 159 | - commit('SET_TOKEN', '') | ||
| 160 | - removeToken() | ||
| 161 | - resolve() | ||
| 162 | - }) | ||
| 163 | } | 160 | } |
| 164 | } | 161 | } |
| 165 | } | 162 | } |
yudao-ui-admin/src/utils/auth.js
| 1 | import Cookies from 'js-cookie' | 1 | import Cookies from 'js-cookie' |
| 2 | 2 | ||
| 3 | -const TokenKey = 'Admin-Token' | 3 | +const AccessTokenKey = 'ACCESS_TOKEN' |
| 4 | +const RefreshTokenKey = 'REFRESH_TOKEN' | ||
| 4 | 5 | ||
| 5 | -export function getToken() { | ||
| 6 | - return Cookies.get(TokenKey) | 6 | +export function getAccessToken() { |
| 7 | + return Cookies.get(AccessTokenKey) | ||
| 8 | +} | ||
| 9 | + | ||
| 10 | +export function getRefreshToken() { | ||
| 11 | + return Cookies.get(AccessTokenKey) | ||
| 7 | } | 12 | } |
| 8 | 13 | ||
| 9 | export function setToken(token) { | 14 | export function setToken(token) { |
| 10 | - return Cookies.set(TokenKey, token) | 15 | + Cookies.set(AccessTokenKey, token.accessToken) |
| 16 | + Cookies.set(RefreshTokenKey, token.refreshToken) | ||
| 11 | } | 17 | } |
| 12 | 18 | ||
| 13 | export function removeToken() { | 19 | export function removeToken() { |
| 14 | - return Cookies.remove(TokenKey) | 20 | + Cookies.remove(AccessTokenKey) |
| 21 | + Cookies.remove(RefreshTokenKey) | ||
| 15 | } | 22 | } |
yudao-ui-admin/src/utils/request.js
| 1 | import axios from 'axios' | 1 | import axios from 'axios' |
| 2 | import { Notification, MessageBox, Message } from 'element-ui' | 2 | import { Notification, MessageBox, Message } from 'element-ui' |
| 3 | import store from '@/store' | 3 | import store from '@/store' |
| 4 | -import { getToken } from '@/utils/auth' | 4 | +import { getAccessToken } from '@/utils/auth' |
| 5 | import errorCode from '@/utils/errorCode' | 5 | import errorCode from '@/utils/errorCode' |
| 6 | import Cookies from "js-cookie"; | 6 | import Cookies from "js-cookie"; |
| 7 | import {getPath, getTenantEnable} from "@/utils/ruoyi"; | 7 | import {getPath, getTenantEnable} from "@/utils/ruoyi"; |
| @@ -21,8 +21,8 @@ const service = axios.create({ | @@ -21,8 +21,8 @@ const service = axios.create({ | ||
| 21 | service.interceptors.request.use(config => { | 21 | service.interceptors.request.use(config => { |
| 22 | // 是否需要设置 token | 22 | // 是否需要设置 token |
| 23 | const isToken = (config.headers || {}).isToken === false | 23 | const isToken = (config.headers || {}).isToken === false |
| 24 | - if (getToken() && !isToken) { | ||
| 25 | - config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改 | 24 | + if (getAccessToken() && !isToken) { |
| 25 | + config.headers['Authorization'] = 'Bearer ' + getAccessToken() // 让每个请求携带自定义token 请根据实际情况自行修改 | ||
| 26 | } | 26 | } |
| 27 | // 设置租户 | 27 | // 设置租户 |
| 28 | if (getTenantEnable()) { | 28 | if (getTenantEnable()) { |
| @@ -133,7 +133,7 @@ service.interceptors.response.use(res => { | @@ -133,7 +133,7 @@ service.interceptors.response.use(res => { | ||
| 133 | 133 | ||
| 134 | export function getBaseHeader() { | 134 | export function getBaseHeader() { |
| 135 | return { | 135 | return { |
| 136 | - 'Authorization': "Bearer " + getToken(), | 136 | + 'Authorization': "Bearer " + getAccessToken(), |
| 137 | 'tenant-id': Cookies.get('tenantId'), | 137 | 'tenant-id': Cookies.get('tenantId'), |
| 138 | } | 138 | } |
| 139 | } | 139 | } |
yudao-ui-admin/src/views/infra/file/index.vue
| @@ -77,7 +77,7 @@ | @@ -77,7 +77,7 @@ | ||
| 77 | 77 | ||
| 78 | <script> | 78 | <script> |
| 79 | import { deleteFile, getFilePage } from "@/api/infra/file"; | 79 | import { deleteFile, getFilePage } from "@/api/infra/file"; |
| 80 | -import {getToken} from "@/utils/auth"; | 80 | +import {getAccessToken} from "@/utils/auth"; |
| 81 | 81 | ||
| 82 | export default { | 82 | export default { |
| 83 | name: "File", | 83 | name: "File", |
| @@ -108,7 +108,7 @@ export default { | @@ -108,7 +108,7 @@ export default { | ||
| 108 | title: "", // 弹出层标题 | 108 | title: "", // 弹出层标题 |
| 109 | isUploading: false, // 是否禁用上传 | 109 | isUploading: false, // 是否禁用上传 |
| 110 | url: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 请求地址 | 110 | url: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 请求地址 |
| 111 | - headers: { Authorization: "Bearer " + getToken() }, // 设置上传的请求头部 | 111 | + headers: { Authorization: "Bearer " + getAccessToken() }, // 设置上传的请求头部 |
| 112 | data: {} // 上传的额外数据,用于文件名 | 112 | data: {} // 上传的额外数据,用于文件名 |
| 113 | }, | 113 | }, |
| 114 | }; | 114 | }; |
yudao-ui-admin/src/views/system/user/index.vue
| @@ -237,7 +237,7 @@ import { | @@ -237,7 +237,7 @@ import { | ||
| 237 | resetUserPwd, | 237 | resetUserPwd, |
| 238 | updateUser | 238 | updateUser |
| 239 | } from "@/api/system/user"; | 239 | } from "@/api/system/user"; |
| 240 | -import {getToken} from "@/utils/auth"; | 240 | +import {getAccessToken} from "@/utils/auth"; |
| 241 | import Treeselect from "@riophae/vue-treeselect"; | 241 | import Treeselect from "@riophae/vue-treeselect"; |
| 242 | import "@riophae/vue-treeselect/dist/vue-treeselect.css"; | 242 | import "@riophae/vue-treeselect/dist/vue-treeselect.css"; |
| 243 | 243 |