package com.itweid.auth.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.itweid.auth.mapper.AuthResourceMapper;
import com.itweid.auth.service.ILoginService;
import com.itweid.common.code.ErrorCode;
import com.itweid.common.code.RedisCode;
import com.itweid.common.entity.AuthResource;
import com.itweid.common.entity.AuthUser;
import com.itweid.common.pojo.*;
import com.itweid.jwt.pojo.JwtUserInfo;
import com.itweid.jwt.utils.AuthTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class LoginServiceImpl implements ILoginService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private AuthTokenUtils authTokenUtils;
@Autowired
private AuthResourceMapper authResourceMapper;
@Override
public BaseResult<LoginVO> doLogin(LoginQuery loginQuery) {
log.info(loginQuery.toString());
// 校验验证码是否正确
BaseResult checkCodeRes = checkCode(loginQuery.getKey(), loginQuery.getCode());
if(!checkCodeRes.isSuccess()) {
return BaseResult.setError(checkCodeRes);
}
// 校验账号、密码是否正确
BaseResult<AuthUser> checkPassRes = checkPass(loginQuery.getAccount(), loginQuery.getPassword());
if(!checkPassRes.isSuccess()) {
return BaseResult.setError(checkPassRes);
}
// 为用户生成jwt令牌
AuthUser authUser = checkPassRes.getData();
Token token = generateUserToken(authUser);
// 查询当前用户可以访问的资源权限
List<AuthResource> authResourceList = authResourceMapper.findVisibleResourceByUserId(authUser.getId());
List<String> permissionList = null;
if(authResourceList != null && !authResourceList.isEmpty()) {
// 用户对应的权限(给前端使用的)
permissionList = authResourceList.stream().map(AuthResource::getCode).collect(Collectors.toList());
// 将用户对应的权限(给后端网关使用的)进行缓存
List<String> visibleResource = authResourceList.stream().map((resource -> {
return resource.getMethod() + resource.getUrl();
})).collect(Collectors.toList());
stringRedisTemplate.opsForHash().put(RedisCode.USER_RESOURCE, authUser.getId().toString(), visibleResource.toString());
}
// 封装返回结果
UserVO userVO = new UserVO();
BeanUtil.copyProperties(authUser, userVO);
LoginVO loginDTO = LoginVO.builder()
.user(userVO)
.token(token)
.permissionsList(permissionList)
.build();
return BaseResult.setOk(loginDTO);
}
private BaseResult checkCode(String key, String code) {
// 从缓存中获取验证码并进行验证
Object captchaObj = stringRedisTemplate.opsForHash().get(RedisCode.CAPTCHA, key);
log.info("Redis => get {} {} => {}", RedisCode.CAPTCHA, key, captchaObj);
if (captchaObj == null) {
return BaseResult.setError(ErrorCode.VALID_CODE_IS_OVERDUE);
}
if (!StringUtils.equalsIgnoreCase(code, String.valueOf(captchaObj))) {
return BaseResult.setError(ErrorCode.VALID_CODE_IS_WRONG);
}
// 验证通过,立即从缓存中删除验证码
Long delete = stringRedisTemplate.opsForHash().delete(RedisCode.CAPTCHA, key);
log.info("Redis => del {} {} => {}", RedisCode.CAPTCHA, key, delete);
return BaseResult.setOk();
}
public BaseResult<AuthUser> checkPass(String account, String password) {
AuthUser authUser = Db.lambdaQuery(AuthUser.class)
.eq(AuthUser::getAccount, account)
.one();
// 将前端提交的密码进行md5加密
String md5Hex = DigestUtils.md5Hex(password);
if (authUser == null || !authUser.getPassword().equals(md5Hex)) {
// 认证失败
return BaseResult.setError(ErrorCode.ACCOUNT_PASS_IS_WRONG);
}
// 认证成功
return BaseResult.setOk(authUser);
}
private Token generateUserToken(AuthUser authUser){
JwtUserInfo jwtUserInfo = new JwtUserInfo(authUser.getId(), authUser.getAccount());
return authTokenUtils.generateUserToken(jwtUserInfo, null);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
后台管理系统的通用权限解决方案-代码 (439个子文件)
00e571349b7d24b9a7ac94054a1c545c8a9976 508B
018539677203918c47d4cbaaab7a27c172c7f0 329B
01ff2a63e47b88ddda8ad8efae2ec7e7467cd0 232B
07230f35d4f2363416ba0cd41c0b7ffe6820e7 738B
0b2b217d85a37d972e1603f3331654f3ca1e6e 182B
0b941e81b78a5d69f55129b8010756d038f960 80B
0c73abc2f5c9ca18f22965627fb12585de56b3 185B
10ad871b37d006408d3b94ec77e994dea00e44 112B
116c0430a71c1abef011d8dfcc21120e0af549 63B
13165ee1f84836823c3ff43f1be16aaf3abe68 47B
13252a64dcebef55db6d0b5d6cebb3485a0b29 82B
144d593d133050c47eabe4ccbc3d4aa311c7df 81B
14f2b280ce4e00ac55ad1fd52468f6b2323733 660B
1a0d9fbc98274c627f89237e9ca34315137b30 615B
1eb71dfd16ff5a83de05b4aa7e5eaa899f3a98 48B
202bef13518d84504f83f4ad4c88812bce7571 163B
21131549cffc92f08ef30182076de377708141 45B
22aa51ec2411af42be1b02eb30924baaa4cedb 81B
249f8b6d3b7da57ea0bac2f646657e9e7d2f28 79B
24f1a4bc1b9f5a5c1217ce2dea7e57ce2342b7 48B
27b25691fe7b8d7d6ad563fca9fc1c3079ebbd 153B
2afd21e79b127c5096140cd6a36f718904eaf2 811B
2c94f78bdf386661f556e697c53cfe9fc8d002 112B
2cbef6466c74967c4b69179081cfd12ce25261 185B
2db992b75f8daa4580c14b69261d73c35a73f5 161B
2e8338cc0f3850320d4ab04f00573e9374da45 257B
32c83e831df3a247eebd25596858f6cbca311b 467B
33236967ce6d67b522db0696afcdc0618cb035 46B
3a29de2c5057c45598453b65c6c8ffef6cf768 53B
3a61dd978d5a2a163861213836dc844b83372c 46B
3dd369c57c7fb79d119e53f0b4feae4fa0902a 182B
3eead465e9993afb18319f21500d3e2b20d846 329B
3f1c4d849063f9d29ee8c2c2e6f9018fe3c476 48B
3f5aa77078a228f849522a0fff0b68e873a601 112B
4010fd9a0b929b629bc6536e1789a5a34c8755 615B
413054c3190d638ae5d5a497d7b850bc283921 49B
41a0ca30f9e13361d10e77b400e5baeb00e1fa 48B
42f81cca9d93617830d1320f4ae515f1d1248b 1KB
43c29e07ad5d128802411c95a58cc4fd369267 361B
450bb70f51dade09874e1673256a48d3dcaac3 46B
47a58118a83f13d9e8459b5a822852f0cfdc37 970B
47f8dbf4a4155825d69cb5d877088184d61843 112B
482e0cbcd59a290cb4d3631f72d132f54f4bc9 835B
4849f25bd6a125f2545e4a2bf57af0ac4e6615 46B
492d1fd2c60f3cc9aff30a779e5538778c0c8d 691B
4932686229260dd0c76f0f51426b3b0266b221 181B
49645a3e8e8542fa550e0b9b9d5d28d61e6c94 112B
4a924b7fa2fcde7f808824ec67e8a3888a0c77 118B
503061558bd84fc28945eb4d5173419e78dfde 2KB
515480e28ea3c3299d75f2148237175b52de1c 90B
573196865fb1ac580a2dc22a4840e18a009e54 130B
58790c72801870723523faf1a07a18438c71d1 363B
58940c3af392e7c2f758e1f0842cb54b3d7149 219B
5bf2fe9d49e88a6cc2fcf86629e989aa27e866 189B
5cd05f8779bcee169526f5ca95fe0c895e426b 653B
5e0962aa31b8127000e40f12cee37fd4151c9e 46B
6018c18c4fd7c96527f7f02e4ff586386a99f2 425B
636541a49e3d6222f7a75d23144216a9a2eac0 46B
64e62cf6b14981d920b53dd06cbcadfcd2f321 504B
65547e1511cec8ee9fcdc40afd8f2af44a75ce 278B
664da1013c0ead9918c6fd853a571ad879459c 46B
68b88a09db818cf448a7e7f000b559b282f116 46B
6ad3905e65e6e4af432f5b7753b26f5c1ba671 88B
6dfad1b11c831958bc833210a3ca21255a6452 46B
6fa441f6d1433b74743f5aaf150bf1da66dd40 982B
702088cf695f1ad49eef37dbe44c8d086b7519 46B
706d6e9b363181c23c90fddb01681d651b8620 354B
70a83d719338f7c557091bf6ff44ae149dcb4a 80B
70ca172b17b346df5a4238f2339533a5aab9cd 159B
732f0b07c7f7571d0403957d37cd11117a7193 80B
7578ef5923d2adde12168e88a664e113e2cfee 183B
7636cd0fcececa4d492451e61ea0428753e7e8 456B
773e6042f52b451d0f16345982fb8185f35417 106B
77837d77d84526dfd381283bb932d97c5415d7 183B
7800b083e4570a42bd5c48180c81e2081cad54 423B
79cd20518fcceffc7e265bb2d5810cd35c577c 184B
7ea8724fe9ccad63198054aa47a43ab7b30c88 237B
811203ab5b108badbda531e285c26469d53c13 559B
81a57affcddf246934d2954cce364f96c5ac17 641B
82fc887261479cd6eec8d68f98496b8b6924c8 82B
83bfde6943555944930de671567a754023dd52 452B
8669769539f143e1791f767b98264db42ea7c9 397B
88bce928852232eb25fbc4752983eca4999c62 61B
88ce077d2cc7efbdf86d7edcf4335c21ce247a 82B
8cad95b512dd0c136e2a3f14a56b272471a98b 45B
8dbe365c36969b1bd1cd723b88615e6cff3fa8 130B
8e00bccb4fece519d07dd60dbc82c86d3bb7af 630B
8e2f6dca5abbe30c4a1ae12a4bb2590f9b63ec 397B
8e4f5f99127752e14aafbfe647dc5588dde8ef 60B
8edbe337824599419b16d5e254dbda9cb3d62a 884B
91171663fecee0a0197cacc0a06dbd688bbadf 89B
91e96562f770486bb411a31189bfd471d8b370 308B
92ff1dc329b6faa16b313db5ae8ba20d1462e9 46B
9397d05bbc9678f4272fa3ac2caa89850f3bb1 46B
975860faf49ce014dc7eb9d2bd14af2d590797 1KB
98a9c836b2f34745056e4d816bc3396b4c15e3 223B
997d562878c7ccddf527e307dab17332ca15bd 158B
99a550c20495d7829f389212d84849b472c2eb 1KB
9aa8cbeb36a7f019e423fb053b83ab0a88a37a 359B
9b5f8e9f80d5c554858097967187a13b815ac8 46B
共 439 条
- 1
- 2
- 3
- 4
- 5
资源评论
维先生d
- 粉丝: 2381
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功