diff --git a/aurora-springboot/src/main/java/com/aurora/constant/RedisPrefixConst.java b/aurora-springboot/src/main/java/com/aurora/constant/RedisPrefixConst.java index 1a73f35..876f64d 100644 --- a/aurora-springboot/src/main/java/com/aurora/constant/RedisPrefixConst.java +++ b/aurora-springboot/src/main/java/com/aurora/constant/RedisPrefixConst.java @@ -57,4 +57,8 @@ public class RedisPrefixConst { */ public static final String LOGIN_USER = "login_user"; + /** + * 文章授权访问名单 + */ + public static final String USER_ARTICLE_ACCESS = "article_access"; } diff --git a/aurora-springboot/src/main/java/com/aurora/controller/ArticleController.java b/aurora-springboot/src/main/java/com/aurora/controller/ArticleController.java index 29ba7d7..aeb40ff 100644 --- a/aurora-springboot/src/main/java/com/aurora/controller/ArticleController.java +++ b/aurora-springboot/src/main/java/com/aurora/controller/ArticleController.java @@ -57,6 +57,13 @@ public class ArticleController { return Result.ok(articleService.getArticleById(articleId)); } + @ApiOperation("校验文章访问密码") + @PostMapping("/articles/access") + public Result accessArticle(@Valid @RequestBody ArticlePasswordVO articlePasswordVO) { + articleService.accessArticle(articlePasswordVO); + return Result.ok(); + } + @ApiOperation("根据标签id获取文章") @GetMapping("/articles/tagId") public Result> listArticlesByTagId(@RequestParam Integer tagId) { @@ -139,5 +146,5 @@ public class ArticleController { public Result> listArticlesBySearch(ConditionVO condition) { return Result.ok(articleService.listArticlesBySearch(condition)); } - + } diff --git a/aurora-springboot/src/main/java/com/aurora/dto/ArticleCardDTO.java b/aurora-springboot/src/main/java/com/aurora/dto/ArticleCardDTO.java index e65bff3..5e50a3f 100644 --- a/aurora-springboot/src/main/java/com/aurora/dto/ArticleCardDTO.java +++ b/aurora-springboot/src/main/java/com/aurora/dto/ArticleCardDTO.java @@ -24,6 +24,7 @@ public class ArticleCardDTO { private UserInfo author; private String categoryName; private List tags; + private Integer status; private LocalDateTime createTime; private LocalDateTime updateTime; } diff --git a/aurora-springboot/src/main/java/com/aurora/entity/Article.java b/aurora-springboot/src/main/java/com/aurora/entity/Article.java index ee3ea6f..ef26872 100644 --- a/aurora-springboot/src/main/java/com/aurora/entity/Article.java +++ b/aurora-springboot/src/main/java/com/aurora/entity/Article.java @@ -30,6 +30,7 @@ public class Article { private Integer isDelete; private Integer status; private Integer type; + private String password; private String originalUrl; @TableField(fill = FieldFill.INSERT) private LocalDateTime createTime; diff --git a/aurora-springboot/src/main/java/com/aurora/enums/StatusCodeEnum.java b/aurora-springboot/src/main/java/com/aurora/enums/StatusCodeEnum.java index 483a869..61c9e22 100644 --- a/aurora-springboot/src/main/java/com/aurora/enums/StatusCodeEnum.java +++ b/aurora-springboot/src/main/java/com/aurora/enums/StatusCodeEnum.java @@ -43,13 +43,13 @@ public enum StatusCodeEnum { */ USERNAME_NOT_EXIST(52002, "用户名不存在"), /** - * qq登录错误 + * 文章密码认证未通过 */ - QQ_LOGIN_ERROR(53001, "qq登录错误"), + ARTICLE_ACCESS_FAIL(52003, "文章密码认证未通过"), /** - * 微博登录错误 + * qq登录错误 */ - WEIBO_LOGIN_ERROR(53002, "微博登录错误"); + QQ_LOGIN_ERROR(53001, "qq登录错误"); /** * 状态码 diff --git a/aurora-springboot/src/main/java/com/aurora/service/ArticleService.java b/aurora-springboot/src/main/java/com/aurora/service/ArticleService.java index 5b8b745..983fedc 100644 --- a/aurora-springboot/src/main/java/com/aurora/service/ArticleService.java +++ b/aurora-springboot/src/main/java/com/aurora/service/ArticleService.java @@ -19,6 +19,8 @@ public interface ArticleService extends IService
{ ArticleDTO getArticleById(Integer articleId); + void accessArticle(ArticlePasswordVO articlePasswordVO); + PageResult listArticlesByTagId(Integer tagId); PageResult listArchives(); diff --git a/aurora-springboot/src/main/java/com/aurora/service/impl/ArticleServiceImpl.java b/aurora-springboot/src/main/java/com/aurora/service/impl/ArticleServiceImpl.java index 2201cfb..6b5c071 100644 --- a/aurora-springboot/src/main/java/com/aurora/service/impl/ArticleServiceImpl.java +++ b/aurora-springboot/src/main/java/com/aurora/service/impl/ArticleServiceImpl.java @@ -8,6 +8,7 @@ import com.aurora.entity.Category; import com.aurora.entity.Tag; import com.aurora.enums.FileExtEnum; import com.aurora.enums.FilePathEnum; +import com.aurora.enums.StatusCodeEnum; import com.aurora.exception.BizException; import com.aurora.mapper.ArticleMapper; import com.aurora.mapper.ArticleTagMapper; @@ -42,6 +43,7 @@ import java.util.stream.Collectors; import static com.aurora.constant.MQPrefixConst.SUBSCRIBE_EXCHANGE; import static com.aurora.constant.RedisPrefixConst.*; import static com.aurora.enums.ArticleStatusEnum.*; +import static com.aurora.enums.StatusCodeEnum.ARTICLE_ACCESS_FAIL; @Service public class ArticleServiceImpl extends ServiceImpl implements ArticleService { @@ -115,6 +117,21 @@ public class ArticleServiceImpl extends ServiceImpl impl @SneakyThrows @Override public ArticleDTO getArticleById(Integer articleId) { + Article articleForCheck = articleMapper.selectOne(new LambdaQueryWrapper
().eq(Article::getId, articleId)); + if (Objects.isNull(articleForCheck)) { + return null; + } + if (articleForCheck.getStatus().equals(2)) { + Boolean isAccess; + try { + isAccess = redisService.sIsMember(USER_ARTICLE_ACCESS + ":" + UserUtils.getUserDetailsDTO().getId(), articleId); + } catch (Exception exception) { + throw new BizException(ARTICLE_ACCESS_FAIL); + } + if (isAccess.equals(false)) { + throw new BizException(ARTICLE_ACCESS_FAIL); + } + } updateArticleViewsCount(articleId); CompletableFuture asyncArticle = CompletableFuture.supplyAsync(() -> articleMapper.getArticleById(articleId)); CompletableFuture asyncPreArticle = CompletableFuture.supplyAsync(() -> { @@ -144,6 +161,19 @@ public class ArticleServiceImpl extends ServiceImpl impl return article; } + @Override + public void accessArticle(ArticlePasswordVO articlePasswordVO) { + Article article = articleMapper.selectOne(new LambdaQueryWrapper
().eq(Article::getId, articlePasswordVO.getArticleId())); + if (Objects.isNull(article)) { + throw new BizException("文章不存在"); + } + if (article.getPassword().equals(articlePasswordVO.getArticlePassword())) { + redisService.sAdd(USER_ARTICLE_ACCESS + ":" + UserUtils.getUserDetailsDTO().getId(), articlePasswordVO.getArticleId()); + } else { + throw new BizException("密码错误"); + } + } + @SneakyThrows @Override public PageResult listArticlesByTagId(Integer tagId) { diff --git a/aurora-springboot/src/main/java/com/aurora/service/impl/TokenServiceImpl.java b/aurora-springboot/src/main/java/com/aurora/service/impl/TokenServiceImpl.java index 89cd660..c6a0bdb 100644 --- a/aurora-springboot/src/main/java/com/aurora/service/impl/TokenServiceImpl.java +++ b/aurora-springboot/src/main/java/com/aurora/service/impl/TokenServiceImpl.java @@ -58,6 +58,7 @@ public class TokenServiceImpl implements TokenService { redisService.hSet(LOGIN_USER, userId, userDetailsDTO, expireTime); } + @Override public void renewToken(UserDetailsDTO userDetailsDTO) { LocalDateTime expireTime = userDetailsDTO.getExpireTime(); LocalDateTime currentTime = LocalDateTime.now(); diff --git a/aurora-springboot/src/main/java/com/aurora/vo/ArticlePasswordVO.java b/aurora-springboot/src/main/java/com/aurora/vo/ArticlePasswordVO.java new file mode 100644 index 0000000..775f103 --- /dev/null +++ b/aurora-springboot/src/main/java/com/aurora/vo/ArticlePasswordVO.java @@ -0,0 +1,16 @@ +package com.aurora.vo; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class ArticlePasswordVO { + private Integer articleId; + private String articlePassword; +} diff --git a/aurora-springboot/src/main/resources/mapper/ArticleMapper.xml b/aurora-springboot/src/main/resources/mapper/ArticleMapper.xml index 8232057..1fc870e 100644 --- a/aurora-springboot/src/main/resources/mapper/ArticleMapper.xml +++ b/aurora-springboot/src/main/resources/mapper/ArticleMapper.xml @@ -8,6 +8,7 @@ + @@ -65,6 +66,7 @@ SUBSTR(article_content, 1, 500) AS article_content, is_top, is_featured, + status, a.create_time AS create_time, a.update_time AS update_time, u.nickname AS author_nickname, @@ -91,7 +93,7 @@ LEFT JOIN t_category c ON a.category_id = c.id LEFT JOIN t_user_info u ON a.user_id = u.id where a.is_delete = 0 - and a.status = 1 + and a.status in (1, 2) order by is_top desc, is_featured desc