Compare commits
6 Commits
feature-Ki
...
feature-Po
| Author | SHA1 | Date | |
|---|---|---|---|
| 1007a2a1e4 | |||
|
|
bd12c599b0 | ||
|
|
b5a15a3f01 | ||
|
|
039443dc0f | ||
| ae0cca5437 | |||
| c329eaef79 |
33
src/main/java/com/bao/dating/config/WebConfig.java
Normal file
33
src/main/java/com/bao/dating/config/WebConfig.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package com.bao.dating.config;
|
||||||
|
|
||||||
|
|
||||||
|
import com.bao.dating.interceptor.TokenInterceptor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TokenInterceptor tokenInterceptor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加拦截器到Spring MVC配置中
|
||||||
|
* @param registry 拦截器注册中心
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
//注册自定义拦截器对象
|
||||||
|
registry.addInterceptor(tokenInterceptor)
|
||||||
|
// 拦截所有请求
|
||||||
|
.addPathPatterns("/**")
|
||||||
|
// 忽略的接口
|
||||||
|
.excludePathPatterns(
|
||||||
|
"/user/login",
|
||||||
|
"/user/userRegister"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
33
src/main/java/com/bao/dating/context/UserContext.java
Normal file
33
src/main/java/com/bao/dating/context/UserContext.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package com.bao.dating.context;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户上下文类
|
||||||
|
* @author lenovo
|
||||||
|
*/
|
||||||
|
public class UserContext {
|
||||||
|
|
||||||
|
private static final ThreadLocal<Long> userHolder = new ThreadLocal<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置当前线程的用户ID
|
||||||
|
* @param userId 用户ID
|
||||||
|
*/
|
||||||
|
public static void setUserId(Long userId) {
|
||||||
|
userHolder.set(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前线程的用户ID
|
||||||
|
* @return 当前用户ID,如果未设置则返回null
|
||||||
|
*/
|
||||||
|
public static Long getUserId() {
|
||||||
|
return userHolder.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除当前线程的用户ID
|
||||||
|
*/
|
||||||
|
public static void clear() {
|
||||||
|
userHolder.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -34,13 +34,12 @@ public class PostController {
|
|||||||
/**
|
/**
|
||||||
* 发布动态接口 - JSON格式请求
|
* 发布动态接口 - JSON格式请求
|
||||||
* @param postDTO 动态信息
|
* @param postDTO 动态信息
|
||||||
* @param userId 用户ID
|
|
||||||
* @return 发布的动态对象
|
* @return 发布的动态对象
|
||||||
*/
|
*/
|
||||||
@PostMapping(consumes = "application/json")
|
@PostMapping(consumes = "application/json")
|
||||||
public Result<Post> createPostJson(@RequestBody PostRequestDTO postDTO, @RequestParam Long userId) {
|
public Result<Post> createPostJson(@RequestBody PostRequestDTO postDTO) {
|
||||||
// 调用 Service 层处理发布动态业务逻辑
|
// 调用 Service 层处理发布动态业务逻辑
|
||||||
Post result = postService.createPost(userId, postDTO);
|
Post result = postService.createPost(postDTO);
|
||||||
return Result.success(ResultCode.SUCCESS_REVIEW, "动态发布成功,等待审核。", result);
|
return Result.success(ResultCode.SUCCESS_REVIEW, "动态发布成功,等待审核。", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,15 @@ package com.bao.dating.controller;
|
|||||||
|
|
||||||
import com.bao.dating.common.Result;
|
import com.bao.dating.common.Result;
|
||||||
import com.bao.dating.common.ResultCode;
|
import com.bao.dating.common.ResultCode;
|
||||||
|
import com.bao.dating.pojo.entity.Post;
|
||||||
|
import com.bao.dating.pojo.entity.PostFavorite;
|
||||||
import com.bao.dating.pojo.entity.User;
|
import com.bao.dating.pojo.entity.User;
|
||||||
import com.bao.dating.service.PostFavoriteService;
|
import com.bao.dating.service.PostFavoriteService;
|
||||||
import com.bao.dating.service.PostService;
|
import com.bao.dating.service.PostService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -31,4 +34,11 @@ public class PostFavoriteController {
|
|||||||
Long userId = user.getUserId();
|
Long userId = user.getUserId();
|
||||||
return postFavoriteService.deletePostFavorite(userId, postId);
|
return postFavoriteService.deletePostFavorite(userId, postId);
|
||||||
}
|
}
|
||||||
|
@GetMapping("/{user_id}/favorites")
|
||||||
|
public Result<List<Post>> getFavorites(@PathVariable("user_id")Long postId){
|
||||||
|
if (postId == null){
|
||||||
|
return Result.error(ResultCode.PARAM_ERROR);
|
||||||
|
}
|
||||||
|
return postFavoriteService.getAllFavoritePost(postId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package com.bao.dating.controller;
|
package com.bao.dating.controller;
|
||||||
|
|
||||||
|
import com.bao.dating.common.Result;
|
||||||
|
import com.bao.dating.common.ResultCode;
|
||||||
|
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||||
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
import com.bao.dating.service.UserService;
|
import com.bao.dating.service.UserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/user")
|
@RequestMapping("/user")
|
||||||
@@ -11,4 +14,35 @@ public class UserController {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录
|
||||||
|
* @param userLoginDTO 登录参数
|
||||||
|
*/
|
||||||
|
@PostMapping("/login")
|
||||||
|
public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
|
||||||
|
UserLoginVO userloginVO = userService.userLogin(userLoginDTO);
|
||||||
|
return Result.success(ResultCode.SUCCESS, "登录成功", userloginVO);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户注册
|
||||||
|
* @param userAccount 用户名称
|
||||||
|
* @param userPassword 用户密码
|
||||||
|
* @return 用户id
|
||||||
|
*/
|
||||||
|
@PostMapping("/userRegister")
|
||||||
|
public Result userRegister(String userAccount, String userPassword){
|
||||||
|
long result = userService.userRegister(userAccount, userPassword);
|
||||||
|
if (result == -1){
|
||||||
|
return Result.error(ResultCode.SYSTEM_ERROR);
|
||||||
|
}
|
||||||
|
if (result==-2){
|
||||||
|
return Result.error(ResultCode.PARAM_ERROR);
|
||||||
|
}
|
||||||
|
if (result == -3){
|
||||||
|
return Result.error(ResultCode.FAIL,"用户名称相同");
|
||||||
|
}
|
||||||
|
return Result.success(ResultCode.SUCCESS,"注册成功",result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package com.bao.dating.interceptor;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import com.bao.dating.context.UserContext;
|
||||||
|
import com.bao.dating.util.JwtUtil;
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.method.HandlerMethod;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HttpToken拦截器类
|
||||||
|
* 用于拦截请求并验证JWT token的有效性,同时从token中解析用户信息
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class TokenInterceptor implements HandlerInterceptor {
|
||||||
|
/**
|
||||||
|
* 在请求处理之前进行拦截
|
||||||
|
* 从请求头或URL参数中获取token,验证其有效性,并将用户ID保存到ThreadLocal中
|
||||||
|
* @param request HTTP请求对象
|
||||||
|
* @param response HTTP响应对象
|
||||||
|
* @param handler 处理器
|
||||||
|
* @return 验证通过返回true,否则返回false
|
||||||
|
* @throws Exception 异常
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
|
||||||
|
//判断当前拦截到的是Controller的方法还是其他资源
|
||||||
|
if (!(handler instanceof HandlerMethod)) {
|
||||||
|
//当前拦截到的不是动态方法,直接放行
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// 从 header 获取 token
|
||||||
|
String token = request.getHeader("token");
|
||||||
|
|
||||||
|
try {
|
||||||
|
log.info("jwt校验: {}", token);
|
||||||
|
|
||||||
|
// 验证 token 是否有效(包括是否过期)
|
||||||
|
if (!JwtUtil.validateToken(token)) {
|
||||||
|
log.error("Token 无效或已过期");
|
||||||
|
response.setStatus(401);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析 token
|
||||||
|
String userId = JwtUtil.getSubjectFromToken(token);
|
||||||
|
log.info("用户: {}", userId);
|
||||||
|
|
||||||
|
// 保存 userId 到 ThreadLocal
|
||||||
|
UserContext.setUserId(Long.valueOf(userId));
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Token 校验失败: {}", e.getMessage());
|
||||||
|
response.setStatus(401);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在请求完成之后执行清理工作
|
||||||
|
* 清除保存在ThreadLocal中的用户ID,防止内存泄漏
|
||||||
|
* @param request HTTP请求对象
|
||||||
|
* @param response HTTP响应对象
|
||||||
|
* @param handler 处理器
|
||||||
|
* @param ex 异常对象
|
||||||
|
* @throws Exception 异常
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||||
|
UserContext.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.bao.dating.mapper;
|
package com.bao.dating.mapper;
|
||||||
|
|
||||||
|
import com.bao.dating.pojo.entity.Post;
|
||||||
import com.bao.dating.pojo.entity.PostFavorite;
|
import com.bao.dating.pojo.entity.PostFavorite;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
@@ -12,4 +13,5 @@ public interface PostFavoriteMapper {
|
|||||||
List<Long> selectUserIDByPostID(@Param("postId") Long postId);
|
List<Long> selectUserIDByPostID(@Param("postId") Long postId);
|
||||||
int addPostFavorite(PostFavorite postFavorite);
|
int addPostFavorite(PostFavorite postFavorite);
|
||||||
int deletePostFavorite(@Param("postId") Long postId);
|
int deletePostFavorite(@Param("postId") Long postId);
|
||||||
|
List<Post> getAllPost(@Param("userId") Long userId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,24 @@
|
|||||||
package com.bao.dating.mapper;
|
package com.bao.dating.mapper;
|
||||||
|
|
||||||
|
import com.bao.dating.pojo.entity.User;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface UserMapper {
|
public interface UserMapper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据用户名查询用户
|
||||||
|
*
|
||||||
|
* @param username 用户名
|
||||||
|
* @return 用户
|
||||||
|
*/
|
||||||
|
User getByUsername(String username);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加用户
|
||||||
|
* @param user 用户对象
|
||||||
|
* @return 受影响行数
|
||||||
|
*/
|
||||||
|
Long insertUser(User user);
|
||||||
|
long getMaxUserId();
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/main/java/com/bao/dating/pojo/dto/UserLoginDTO.java
Normal file
14
src/main/java/com/bao/dating/pojo/dto/UserLoginDTO.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com.bao.dating.pojo.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登录数据传输对象
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class UserLoginDTO implements Serializable {
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
}
|
||||||
@@ -38,4 +38,8 @@ public class User implements Serializable {
|
|||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
private LocalDateTime updatedAt;
|
private LocalDateTime updatedAt;
|
||||||
|
|
||||||
|
private String userEmail;
|
||||||
|
|
||||||
|
private String userPhone;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package com.bao.dating.service;
|
package com.bao.dating.service;
|
||||||
|
|
||||||
import com.bao.dating.common.Result;
|
import com.bao.dating.common.Result;
|
||||||
|
import com.bao.dating.pojo.entity.Post;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public interface PostFavoriteService {
|
public interface PostFavoriteService {
|
||||||
Result<Map<String,Long>> postFavorite(Long userid,Long postId);
|
Result<Map<String,Long>> postFavorite(Long userid,Long postId);
|
||||||
Result<?> deletePostFavorite(Long userid,Long postId);
|
Result<?> deletePostFavorite(Long userid,Long postId);
|
||||||
|
Result<List<Post>> getAllFavoritePost(Long userId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,11 +17,10 @@ public interface PostService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建动态
|
* 创建动态
|
||||||
* @param userId 用户ID
|
|
||||||
* @param postRequestDTO 动态数据传输对象
|
* @param postRequestDTO 动态数据传输对象
|
||||||
* @return 创建的动态对象
|
* @return 创建的动态对象
|
||||||
*/
|
*/
|
||||||
Post createPost(Long userId, PostRequestDTO postRequestDTO);
|
Post createPost(PostRequestDTO postRequestDTO);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量删除动态
|
* 批量删除动态
|
||||||
|
|||||||
@@ -1,4 +1,14 @@
|
|||||||
package com.bao.dating.service;
|
package com.bao.dating.service;
|
||||||
|
|
||||||
|
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||||
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
|
|
||||||
public interface UserService {
|
public interface UserService {
|
||||||
|
/**
|
||||||
|
* 登录
|
||||||
|
* @param userLoginDTO 登录参数
|
||||||
|
* @return 登录结果
|
||||||
|
*/
|
||||||
|
UserLoginVO userLogin(UserLoginDTO userLoginDTO);
|
||||||
|
long userRegister(String userAccount, String userPassword);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.bao.dating.common.Result;
|
|||||||
import com.bao.dating.common.ResultCode;
|
import com.bao.dating.common.ResultCode;
|
||||||
import com.bao.dating.mapper.PostFavoriteMapper;
|
import com.bao.dating.mapper.PostFavoriteMapper;
|
||||||
import com.bao.dating.mapper.PostMapper;
|
import com.bao.dating.mapper.PostMapper;
|
||||||
|
import com.bao.dating.pojo.entity.Post;
|
||||||
import com.bao.dating.pojo.entity.PostFavorite;
|
import com.bao.dating.pojo.entity.PostFavorite;
|
||||||
import com.bao.dating.service.PostFavoriteService;
|
import com.bao.dating.service.PostFavoriteService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -58,4 +59,10 @@ public class PostFavoriteServiceImpl implements PostFavoriteService {
|
|||||||
postMapper.decreaseFavoriteCount(postId);
|
postMapper.decreaseFavoriteCount(postId);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result<List<Post>> getAllFavoritePost(Long userId) {
|
||||||
|
List<Post> result = postFavoriteMapper.getAllPost(userId);
|
||||||
|
return Result.success(ResultCode.SUCCESS,"查询成功",result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.bao.dating.service.impl;
|
|||||||
|
|
||||||
import com.bao.dating.common.aliyun.GreenImageScan;
|
import com.bao.dating.common.aliyun.GreenImageScan;
|
||||||
import com.bao.dating.common.aliyun.GreenTextScan;
|
import com.bao.dating.common.aliyun.GreenTextScan;
|
||||||
|
import com.bao.dating.context.UserContext;
|
||||||
import com.bao.dating.mapper.PostMapper;
|
import com.bao.dating.mapper.PostMapper;
|
||||||
import com.bao.dating.pojo.dto.PostRequestDTO;
|
import com.bao.dating.pojo.dto.PostRequestDTO;
|
||||||
import com.bao.dating.pojo.entity.Post;
|
import com.bao.dating.pojo.entity.Post;
|
||||||
@@ -85,15 +86,15 @@ public class PostServiceImpl implements PostService {
|
|||||||
/**
|
/**
|
||||||
* 创建动态
|
* 创建动态
|
||||||
*
|
*
|
||||||
* @param userId 用户ID
|
|
||||||
* @param postRequestDTO 动态数据传输对象
|
* @param postRequestDTO 动态数据传输对象
|
||||||
* @return 创建的动态对象
|
* @return 创建的动态对象
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Post createPost(Long userId, PostRequestDTO postRequestDTO) {
|
public Post createPost(PostRequestDTO postRequestDTO) {
|
||||||
|
|
||||||
// 创建动态对象
|
// 创建动态对象
|
||||||
Post post = new Post();
|
Post post = new Post();
|
||||||
|
Long userId = UserContext.getUserId();
|
||||||
post.setUserId(userId);
|
post.setUserId(userId);
|
||||||
post.setContent(postRequestDTO.getContent());
|
post.setContent(postRequestDTO.getContent());
|
||||||
post.setTags(postRequestDTO.getTags());
|
post.setTags(postRequestDTO.getTags());
|
||||||
@@ -165,6 +166,20 @@ public class PostServiceImpl implements PostService {
|
|||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public int deletePostById(List<Long> postIds) {
|
public int deletePostById(List<Long> postIds) {
|
||||||
|
// 判断用户权限
|
||||||
|
Long userId = UserContext.getUserId();
|
||||||
|
|
||||||
|
// 遍历所有要删除的帖子ID,验证权限
|
||||||
|
for (Long postId : postIds) {
|
||||||
|
Post post = postMapper.selectById(postId);
|
||||||
|
if (post == null) {
|
||||||
|
throw new RuntimeException("动态不存在");
|
||||||
|
}
|
||||||
|
// 验证用户权限
|
||||||
|
if (post.getUserId() == null || !post.getUserId().equals(userId)) {
|
||||||
|
throw new RuntimeException("无权限删除此动态");
|
||||||
|
}
|
||||||
|
}
|
||||||
// 批量删除动态
|
// 批量删除动态
|
||||||
return postMapper.deletePostByIds(postIds);
|
return postMapper.deletePostByIds(postIds);
|
||||||
}
|
}
|
||||||
@@ -177,11 +192,15 @@ public class PostServiceImpl implements PostService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public PostEditVO getPostForEdit(Long postId) {
|
public PostEditVO getPostForEdit(Long postId) {
|
||||||
|
|
||||||
Post post = postMapper.selectById(postId);
|
Post post = postMapper.selectById(postId);
|
||||||
if (post == null) {
|
if (post == null) {
|
||||||
throw new RuntimeException("动态不存在");
|
throw new RuntimeException("动态不存在");
|
||||||
}
|
}
|
||||||
|
// 判断用户权限
|
||||||
|
Long userId = UserContext.getUserId();
|
||||||
|
if (post.getUserId() == null || !post.getUserId().equals(userId)){
|
||||||
|
throw new RuntimeException("无权限查看此动态");
|
||||||
|
}
|
||||||
PostEditVO postEditVO = new PostEditVO();
|
PostEditVO postEditVO = new PostEditVO();
|
||||||
BeanUtils.copyProperties(post, postEditVO);
|
BeanUtils.copyProperties(post, postEditVO);
|
||||||
return postEditVO;
|
return postEditVO;
|
||||||
@@ -200,6 +219,11 @@ public class PostServiceImpl implements PostService {
|
|||||||
if (post == null) {
|
if (post == null) {
|
||||||
throw new RuntimeException("动态不存在");
|
throw new RuntimeException("动态不存在");
|
||||||
}
|
}
|
||||||
|
// 判断用户权限
|
||||||
|
Long userId = UserContext.getUserId();
|
||||||
|
if (post.getUserId() == null || !post.getUserId().equals(userId)){
|
||||||
|
throw new RuntimeException("无权限修改此动态");
|
||||||
|
}
|
||||||
post.setContent(postRequestDTO.getContent());
|
post.setContent(postRequestDTO.getContent());
|
||||||
if (postRequestDTO.getMediaOssKeys() != null && !postRequestDTO.getMediaOssKeys().isEmpty()) {
|
if (postRequestDTO.getMediaOssKeys() != null && !postRequestDTO.getMediaOssKeys().isEmpty()) {
|
||||||
post.setMediaOssKeys(postRequestDTO.getMediaOssKeys());
|
post.setMediaOssKeys(postRequestDTO.getMediaOssKeys());
|
||||||
|
|||||||
@@ -1,13 +1,84 @@
|
|||||||
package com.bao.dating.service.impl;
|
package com.bao.dating.service.impl;
|
||||||
|
|
||||||
import com.bao.dating.mapper.UserMapper;
|
import com.bao.dating.mapper.UserMapper;
|
||||||
|
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||||
|
import com.bao.dating.pojo.entity.User;
|
||||||
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
import com.bao.dating.service.UserService;
|
import com.bao.dating.service.UserService;
|
||||||
|
import com.bao.dating.util.JwtUtil;
|
||||||
|
import com.bao.dating.util.MD5Util;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserServiceImpl implements UserService {
|
public class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserMapper userMapper;
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserLoginVO userLogin(UserLoginDTO userLoginDTO) {
|
||||||
|
// 参数校验
|
||||||
|
if (userLoginDTO == null || userLoginDTO.getUsername() == null || userLoginDTO.getPassword() == null) {
|
||||||
|
throw new RuntimeException("用户名或密码不能为空");
|
||||||
|
}
|
||||||
|
// 查询用户
|
||||||
|
User user = userMapper.getByUsername(userLoginDTO.getUsername());
|
||||||
|
if (user == null){
|
||||||
|
throw new RuntimeException("用户不存在");
|
||||||
|
}
|
||||||
|
// 密码校验
|
||||||
|
boolean match = MD5Util.verifyWithSalt(
|
||||||
|
userLoginDTO.getPassword(),
|
||||||
|
user.getSalt(),
|
||||||
|
user.getPasswordHash()
|
||||||
|
);
|
||||||
|
if (!match){
|
||||||
|
throw new RuntimeException("密码错误");
|
||||||
|
}
|
||||||
|
// 生成token
|
||||||
|
String token = JwtUtil.generateToken(String.valueOf(user.getUserId()));
|
||||||
|
// 封装返回
|
||||||
|
UserLoginVO userLoginVO = new UserLoginVO();
|
||||||
|
userLoginVO.setUserId(user.getUserId());
|
||||||
|
userLoginVO.setNickname(user.getNickname());
|
||||||
|
userLoginVO.setToken(token);
|
||||||
|
return userLoginVO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户注册
|
||||||
|
* @param userAccount 账户名称
|
||||||
|
* @param userPassword 密码
|
||||||
|
* @return 用户id
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public long userRegister(String userAccount, String userPassword) {
|
||||||
|
//验证非空
|
||||||
|
if (userAccount.isEmpty() ||userPassword.isEmpty()){
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
//校验账户不能重复
|
||||||
|
User resultUser = userMapper.getByUsername(userAccount);
|
||||||
|
if (resultUser!=null){
|
||||||
|
return -3;
|
||||||
|
}
|
||||||
|
//对密码进行加密
|
||||||
|
String encryptPassword = MD5Util.encryptWithSalt(userPassword, "yujian");
|
||||||
|
long maxUserId = userMapper.getMaxUserId();
|
||||||
|
User user = new User();
|
||||||
|
user.setUserId(maxUserId+1);
|
||||||
|
user.setUserName(userAccount);
|
||||||
|
user.setSalt("yujian");
|
||||||
|
user.setPasswordHash(encryptPassword);
|
||||||
|
user.setCreatedAt(LocalDateTime.now());
|
||||||
|
user.setUpdatedAt(LocalDateTime.now());
|
||||||
|
Long result = userMapper.insertUser(user);
|
||||||
|
if (result < 0){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return user.getUserId();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,19 @@ spring:
|
|||||||
username: root
|
username: root
|
||||||
password: JoyeeServe2025
|
password: JoyeeServe2025
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
redis:
|
||||||
|
host: 127.0.0.1
|
||||||
|
port: 6379
|
||||||
|
password: ""
|
||||||
|
database: 0
|
||||||
|
timeout: 10000
|
||||||
|
# 连接池配置(lettuce是Spring Boot默认Redis客户端,性能更优)
|
||||||
|
lettuce:
|
||||||
|
pool:
|
||||||
|
max-active: 8 # 连接池最大连接数(默认8,可根据业务并发调整)
|
||||||
|
max-wait: -1 # 连接池最大阻塞等待时间(毫秒,-1表示无限制)
|
||||||
|
max-idle: 8 # 连接池最大空闲连接数(默认8)
|
||||||
|
min-idle: 1 # 连接池最小空闲连接数(默认0,建议设置1-4,提高连接复用率)
|
||||||
# 邮箱SMTP配置
|
# 邮箱SMTP配置
|
||||||
mail:
|
mail:
|
||||||
host: smtp.163.com # QQ邮箱SMTP服务器地址
|
host: smtp.163.com # QQ邮箱SMTP服务器地址
|
||||||
|
|||||||
@@ -14,4 +14,8 @@
|
|||||||
<select id="selectUserIDByPostID" resultType="java.lang.Long">
|
<select id="selectUserIDByPostID" resultType="java.lang.Long">
|
||||||
SELECT user_id FROM post_favorite WHERE post_id = #{postId}
|
SELECT user_id FROM post_favorite WHERE post_id = #{postId}
|
||||||
</select>
|
</select>
|
||||||
|
<!--查询用户收藏的所有动态-->
|
||||||
|
<select id="getAllPost" resultType="com.bao.dating.pojo.entity.Post">
|
||||||
|
select * from post where post_id in (select post_id from post_favorite where user_id=#{userId})
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -3,6 +3,15 @@
|
|||||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
|
||||||
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
||||||
|
<insert id="insertUser" useGeneratedKeys="true" parameterType="com.bao.dating.pojo.entity.User">
|
||||||
|
insert into user(user_id,user_name,salt,password_hash,created_at,updated_at) values (#{userId},#{userName},#{salt},#{passwordHash},#{createdAt},#{updatedAt})
|
||||||
|
</insert>
|
||||||
|
|
||||||
|
<select id="getByUsername" resultType="com.bao.dating.pojo.entity.User">
|
||||||
|
SELECT * FROM user WHERE user_name = #{userName}
|
||||||
|
</select>
|
||||||
|
<select id="getMaxUserId" resultType="java.lang.Long">
|
||||||
|
SELECT IFNULL(MAX(user_id), 0) AS max_id FROM user;
|
||||||
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
Reference in New Issue
Block a user