feat: 开发中...
This commit is contained in:
@@ -3,8 +3,10 @@ package cn.hamster3.application.blog.config;
|
||||
import cn.hamster3.application.blog.vo.ResponseVO;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@@ -22,6 +24,16 @@ public class BlogExceptionHandler {
|
||||
return ResponseVO.failed(e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseVO<String> onException(MethodArgumentNotValidException e) {
|
||||
return ResponseVO.failed(e.getAllErrors()
|
||||
.stream()
|
||||
.findFirst()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.orElse("未知错误!")
|
||||
);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseVO<String> onException(Exception e) {
|
||||
log.error("", e);
|
||||
|
@@ -1,11 +1,9 @@
|
||||
package cn.hamster3.application.blog.config;
|
||||
|
||||
import cn.hamster3.application.blog.util.BlogUtils;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.domain.AuditorAware;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.ProviderManager;
|
||||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||||
@@ -14,8 +12,6 @@ import org.springframework.security.core.userdetails.cache.SpringCacheBasedUserC
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig {
|
||||
@Resource
|
||||
@@ -26,11 +22,6 @@ public class WebConfig {
|
||||
return new BCryptPasswordEncoder(5);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AuditorAware<UUID> getUserIDAuditorAware() {
|
||||
return BlogUtils::currentUserUUID;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AuthenticationManager getAuthenticationManager() {
|
||||
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
||||
|
@@ -9,10 +9,10 @@ import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
public class BlogUser extends User {
|
||||
private final UUID uuid;
|
||||
private final UUID id;
|
||||
|
||||
public BlogUser(String username, String password, Collection<? extends GrantedAuthority> authorities, UUID uuid) {
|
||||
public BlogUser(String username, String password, Collection<? extends GrantedAuthority> authorities, UUID id) {
|
||||
super(username, password, authorities);
|
||||
this.uuid = uuid;
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,58 @@
|
||||
package cn.hamster3.application.blog.config.security;
|
||||
|
||||
import cn.hamster3.application.blog.dao.UserRepository;
|
||||
import cn.hamster3.application.blog.entity.UserEntity;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.data.domain.AuditorAware;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class UserAuditorAware implements AuditorAware<UserEntity> {
|
||||
@Resource
|
||||
private UserRepository userRepo;
|
||||
|
||||
@NotNull
|
||||
public static Optional<BlogUser> currentUser() {
|
||||
SecurityContext context = SecurityContextHolder.getContext();
|
||||
Authentication authentication = context.getAuthentication();
|
||||
if (authentication == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
log.info("==============================");
|
||||
log.info("current user authentication: {}", authentication);
|
||||
log.info("current user authentication getPrincipal: {}", authentication.getPrincipal());
|
||||
log.info("current user authentication getCredentials: {}", authentication.getCredentials());
|
||||
log.info("current user authentication getDetails: {}", authentication.getDetails());
|
||||
log.info("current user authentication getAuthorities: {}", authentication.getAuthorities());
|
||||
if (!authentication.isAuthenticated()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (!(authentication.getPrincipal() instanceof BlogUser user)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(user);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Optional<UUID> currentUserUUID() {
|
||||
return currentUser().map(BlogUser::getId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Optional<UserEntity> getCurrentAuditor() {
|
||||
UUID uuid = UserAuditorAware.currentUserUUID().orElse(null);
|
||||
if (uuid == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return userRepo.findById(uuid);
|
||||
}
|
||||
}
|
@@ -2,7 +2,6 @@ package cn.hamster3.application.blog.controller;
|
||||
|
||||
import cn.hamster3.application.blog.service.IBlogService;
|
||||
import cn.hamster3.application.blog.vo.ResponseVO;
|
||||
import cn.hamster3.application.blog.vo.blog.BlogCreateRequireVO;
|
||||
import cn.hamster3.application.blog.vo.blog.BlogInfoResponseVO;
|
||||
import cn.hamster3.application.blog.vo.blog.BlogUpdateRequireVO;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@@ -22,7 +21,7 @@ public class BlogController {
|
||||
private IBlogService blogService;
|
||||
|
||||
@PostMapping("/")
|
||||
public ResponseVO<Long> createBlog(@RequestBody @Valid BlogCreateRequireVO requireVO) {
|
||||
public ResponseVO<Long> createBlog(@RequestBody @Valid BlogUpdateRequireVO requireVO) {
|
||||
return blogService.createBlog(requireVO);
|
||||
}
|
||||
|
||||
@@ -37,7 +36,12 @@ public class BlogController {
|
||||
}
|
||||
|
||||
@PutMapping("/{blogID}/")
|
||||
public ResponseVO<Void> updateBlog(@PathVariable String blogID, @RequestBody @Valid BlogUpdateRequireVO requireVO) {
|
||||
return ResponseVO.success();
|
||||
public ResponseVO<Void> updateBlog(@PathVariable Long blogID, @RequestBody @Valid BlogUpdateRequireVO requireVO) {
|
||||
return blogService.updateBlog(blogID, requireVO);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{blogID}/")
|
||||
public ResponseVO<Void> removeBlog(@PathVariable Long blogID) {
|
||||
return blogService.removeBlog(blogID);
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,15 @@
|
||||
package cn.hamster3.application.blog.dao;
|
||||
|
||||
import cn.hamster3.application.blog.entity.BlogEntity;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface BlogRepository extends JpaRepository<BlogEntity, Long>, JpaSpecificationExecutor<BlogEntity> {
|
||||
@EntityGraph(attributePaths = {"content"})
|
||||
@Query("select b from BlogEntity b where b.id = ?1")
|
||||
Optional<BlogEntity> findByIDWithContent(Long id);
|
||||
}
|
@@ -3,14 +3,17 @@ package cn.hamster3.application.blog.entity;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.annotation.CreatedBy;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedDate;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Getter
|
||||
@Entity
|
||||
@Table(name = "attach_entity")
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
public class AttachEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@@ -23,6 +26,7 @@ public class AttachEntity {
|
||||
@Column(name = "data")
|
||||
private byte[] data;
|
||||
|
||||
@CreatedBy
|
||||
@ManyToOne(optional = false)
|
||||
@JoinColumn(name = "uploader_id", nullable = false)
|
||||
private UserEntity uploader;
|
||||
|
@@ -5,6 +5,7 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.JdbcTypeCode;
|
||||
import org.hibernate.type.SqlTypes;
|
||||
import org.springframework.data.annotation.CreatedBy;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedDate;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
@@ -17,6 +18,7 @@ import java.util.List;
|
||||
* 博文实体
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "blog_entity")
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@@ -26,21 +28,28 @@ public class BlogEntity {
|
||||
@Column(name = "id", nullable = false)
|
||||
private Long id;
|
||||
|
||||
@Setter
|
||||
@Column(name = "title", nullable = false, length = 128)
|
||||
private String title;
|
||||
|
||||
@Column(name = "abstracts", nullable = false, length = 512)
|
||||
private String abstracts;
|
||||
|
||||
@Basic(fetch = FetchType.LAZY)
|
||||
@Column(name = "password", length = 32)
|
||||
@Column(name = "password", length = 60)
|
||||
@JdbcTypeCode(SqlTypes.VARCHAR)
|
||||
private String password;
|
||||
|
||||
@Lob
|
||||
@Basic(fetch = FetchType.LAZY)
|
||||
@Column(name = "content")
|
||||
@Column(name = "content", nullable = false)
|
||||
private String content;
|
||||
|
||||
@OneToMany(mappedBy = "blogEntity", orphanRemoval = true)
|
||||
@Setter
|
||||
@OrderBy("create_time DESC")
|
||||
@OneToMany(mappedBy = "blogEntity", orphanRemoval = true)
|
||||
private List<BlogAttachEntity> attachEntities = new ArrayList<>();
|
||||
|
||||
@CreatedBy
|
||||
@ManyToOne(optional = false)
|
||||
@JoinColumn(name = "uploader_id", nullable = false)
|
||||
private UserEntity uploader;
|
||||
|
@@ -4,8 +4,9 @@ import cn.hamster3.application.blog.constant.UserPermissions;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.annotations.JdbcTypeCode;
|
||||
import org.hibernate.type.SqlTypes;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedDate;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
@@ -13,7 +14,6 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
import java.util.*;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
@Entity
|
||||
@Table(name = "user_entity")
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@@ -21,6 +21,7 @@ public class UserEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id", nullable = false, updatable = false)
|
||||
@JdbcTypeCode(SqlTypes.CHAR)
|
||||
private UUID id;
|
||||
|
||||
@Setter
|
||||
@@ -36,22 +37,19 @@ public class UserEntity {
|
||||
private String password;
|
||||
|
||||
@Setter
|
||||
@ToString.Exclude
|
||||
@ElementCollection
|
||||
@Column(name = "permission")
|
||||
@CollectionTable(name = "user_entity_permissions", joinColumns = @JoinColumn(name = "user_id"))
|
||||
private Set<UserPermissions> permissions = new HashSet<>();
|
||||
|
||||
@Setter
|
||||
@ToString.Exclude
|
||||
@OneToMany(mappedBy = "uploader", orphanRemoval = true)
|
||||
@OrderBy("create_time DESC")
|
||||
@OneToMany(mappedBy = "uploader", orphanRemoval = true)
|
||||
private List<AttachEntity> attachEntities = new ArrayList<>();
|
||||
|
||||
@Setter
|
||||
@ToString.Exclude
|
||||
@OneToMany(mappedBy = "uploader", orphanRemoval = true)
|
||||
@OrderBy("create_time DESC")
|
||||
@OneToMany(mappedBy = "uploader", orphanRemoval = true)
|
||||
private List<BlogEntity> blogEntities = new ArrayList<>();
|
||||
|
||||
@CreatedDate
|
||||
|
@@ -1,16 +1,22 @@
|
||||
package cn.hamster3.application.blog.entity.mapper;
|
||||
|
||||
import cn.hamster3.application.blog.entity.BlogEntity;
|
||||
import cn.hamster3.application.blog.vo.blog.BlogCreateRequireVO;
|
||||
import cn.hamster3.application.blog.entity.UserEntity;
|
||||
import cn.hamster3.application.blog.vo.blog.BlogUpdateRequireVO;
|
||||
import cn.hamster3.application.blog.vo.blog.BlogInfoResponseVO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.MappingConstants;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, componentModel = MappingConstants.ComponentModel.SPRING)
|
||||
public interface BlogMapper {
|
||||
BlogEntity voToEntity(BlogCreateRequireVO requireVO);
|
||||
BlogEntity voToEntity(BlogUpdateRequireVO requireVO);
|
||||
|
||||
BlogInfoResponseVO entityToInfoVO(BlogEntity requireVO);
|
||||
|
||||
default UUID map(UserEntity value) {
|
||||
return value.getId();
|
||||
}
|
||||
}
|
||||
|
@@ -1,17 +1,21 @@
|
||||
package cn.hamster3.application.blog.service;
|
||||
|
||||
import cn.hamster3.application.blog.vo.ResponseVO;
|
||||
import cn.hamster3.application.blog.vo.blog.BlogCreateRequireVO;
|
||||
import cn.hamster3.application.blog.vo.blog.BlogInfoResponseVO;
|
||||
import cn.hamster3.application.blog.vo.blog.BlogUpdateRequireVO;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface IBlogService {
|
||||
@NotNull ResponseVO<Long> createBlog(@NotNull BlogCreateRequireVO requireVO);
|
||||
@NotNull ResponseVO<Long> createBlog(@NotNull BlogUpdateRequireVO requireVO);
|
||||
|
||||
@NotNull ResponseVO<BlogInfoResponseVO> getBlogInfo(@NotNull Long blogID);
|
||||
|
||||
@NotNull ResponseVO<List<BlogInfoResponseVO>> getBlogInfoList(@NotNull PageRequest page);
|
||||
|
||||
@NotNull ResponseVO<Void> updateBlog(@NotNull Long blogID, @NotNull BlogUpdateRequireVO requireVO);
|
||||
|
||||
@NotNull ResponseVO<Void> removeBlog(@NotNull Long blogID);
|
||||
}
|
||||
|
@@ -1,21 +1,24 @@
|
||||
package cn.hamster3.application.blog.service.impl;
|
||||
|
||||
import cn.hamster3.application.blog.config.security.BlogUser;
|
||||
import cn.hamster3.application.blog.config.security.UserAuditorAware;
|
||||
import cn.hamster3.application.blog.dao.BlogRepository;
|
||||
import cn.hamster3.application.blog.entity.BlogEntity;
|
||||
import cn.hamster3.application.blog.entity.UserEntity;
|
||||
import cn.hamster3.application.blog.entity.mapper.BlogMapper;
|
||||
import cn.hamster3.application.blog.service.IBlogService;
|
||||
import cn.hamster3.application.blog.util.BlogUtils;
|
||||
import cn.hamster3.application.blog.vo.ResponseVO;
|
||||
import cn.hamster3.application.blog.vo.blog.BlogCreateRequireVO;
|
||||
import cn.hamster3.application.blog.vo.blog.BlogInfoResponseVO;
|
||||
import cn.hamster3.application.blog.vo.blog.BlogUpdateRequireVO;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class BlogService implements IBlogService {
|
||||
@Resource
|
||||
@@ -23,20 +26,24 @@ public class BlogService implements IBlogService {
|
||||
@Resource
|
||||
private BlogRepository blogRepo;
|
||||
|
||||
@Resource
|
||||
private UserAuditorAware userAuditorAware;
|
||||
|
||||
@Override
|
||||
public @NotNull ResponseVO<Long> createBlog(@NotNull BlogCreateRequireVO requireVO) {
|
||||
BlogUser user = BlogUtils.currentUser().orElse(null);
|
||||
public @NotNull ResponseVO<Long> createBlog(@NotNull BlogUpdateRequireVO requireVO) {
|
||||
log.info("create blog vo: {}", requireVO);
|
||||
UserEntity user = userAuditorAware.getCurrentAuditor().orElse(null);
|
||||
if (user == null) {
|
||||
return ResponseVO.failed("not login.");
|
||||
}
|
||||
BlogEntity entity = blogMapper.voToEntity(requireVO);
|
||||
BlogEntity save = blogRepo.save(entity);
|
||||
return ResponseVO.success(save.getId());
|
||||
entity = blogRepo.save(entity);
|
||||
return ResponseVO.success(entity.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ResponseVO<BlogInfoResponseVO> getBlogInfo(@NotNull Long blogID) {
|
||||
return blogRepo.findById(blogID)
|
||||
return blogRepo.findByIDWithContent(blogID)
|
||||
.map(o -> ResponseVO.success(blogMapper.entityToInfoVO(o)))
|
||||
.orElseThrow(() -> new IllegalArgumentException("未找到该文章!"));
|
||||
}
|
||||
@@ -49,4 +56,30 @@ public class BlogService implements IBlogService {
|
||||
.toList()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ResponseVO<Void> updateBlog(@NotNull Long blogID, @NotNull BlogUpdateRequireVO requireVO) {
|
||||
if (!blogRepo.existsById(blogID)) {
|
||||
return ResponseVO.failed("该博文不存在!");
|
||||
}
|
||||
BlogUser user = UserAuditorAware.currentUser().orElse(null);
|
||||
if (user == null) {
|
||||
return ResponseVO.failed("not login.");
|
||||
}
|
||||
//todo 权限检查
|
||||
BlogEntity entity = blogMapper.voToEntity(requireVO);
|
||||
entity.setId(blogID);
|
||||
blogRepo.save(entity);
|
||||
return ResponseVO.success();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ResponseVO<Void> removeBlog(@NotNull Long blogID) {
|
||||
if (!blogRepo.existsById(blogID)) {
|
||||
return ResponseVO.failed("该博文不存在!");
|
||||
}
|
||||
//todo 权限检查
|
||||
blogRepo.deleteById(blogID);
|
||||
return ResponseVO.success();
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package cn.hamster3.application.blog.service.impl;
|
||||
|
||||
import cn.hamster3.application.blog.config.security.UserAuditorAware;
|
||||
import cn.hamster3.application.blog.config.security.BlogUser;
|
||||
import cn.hamster3.application.blog.constant.UserPermissions;
|
||||
import cn.hamster3.application.blog.dao.UserRepository;
|
||||
@@ -7,7 +8,6 @@ import cn.hamster3.application.blog.entity.UserEntity;
|
||||
import cn.hamster3.application.blog.entity.mapper.AttachMapper;
|
||||
import cn.hamster3.application.blog.entity.mapper.UserMapper;
|
||||
import cn.hamster3.application.blog.service.IUserService;
|
||||
import cn.hamster3.application.blog.util.BlogUtils;
|
||||
import cn.hamster3.application.blog.vo.PageableResponseVO;
|
||||
import cn.hamster3.application.blog.vo.ResponseVO;
|
||||
import cn.hamster3.application.blog.vo.attach.AttachInfoResponseVO;
|
||||
@@ -46,7 +46,7 @@ public class UserService implements IUserService {
|
||||
|
||||
@Override
|
||||
public @NotNull ResponseVO<UserInfoResponseVO> getCurrentUserInfo() {
|
||||
UUID uuid = BlogUtils.currentUserUUID().orElse(null);
|
||||
UUID uuid = UserAuditorAware.currentUserUUID().orElse(null);
|
||||
if (uuid == null) {
|
||||
return ResponseVO.failed("not login.");
|
||||
}
|
||||
@@ -90,7 +90,7 @@ public class UserService implements IUserService {
|
||||
if (!(authentication.getPrincipal() instanceof BlogUser blogUser)) {
|
||||
return ResponseVO.failed("你没有这个权限!");
|
||||
}
|
||||
if (!blogUser.getUuid().equals(userEntity.getId())
|
||||
if (!blogUser.getId().equals(userEntity.getId())
|
||||
&& blogUser.getAuthorities().contains(UserPermissions.MODIFY_USER_INFO.getAuthority())) {
|
||||
return ResponseVO.failed("你没有这个权限!");
|
||||
}
|
||||
@@ -115,7 +115,7 @@ public class UserService implements IUserService {
|
||||
return ResponseVO.failed("你没有这个权限!");
|
||||
}
|
||||
// 用户必须具有 MODIFY_USER_PERMISSION 权限,且操作对象不为用户自己时才能更改权限
|
||||
if (blogUser.getUuid().equals(userEntity.getId())) {
|
||||
if (blogUser.getId().equals(userEntity.getId())) {
|
||||
return ResponseVO.failed("你不能更改自己的权限!");
|
||||
}
|
||||
userEntity.setPermissions(requireVO.getPermissions());
|
||||
|
@@ -1,41 +1,8 @@
|
||||
package cn.hamster3.application.blog.util;
|
||||
|
||||
import cn.hamster3.application.blog.config.security.BlogUser;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Slf4j
|
||||
public class BlogUtils {
|
||||
@NotNull
|
||||
public static Optional<BlogUser> currentUser() {
|
||||
SecurityContext context = SecurityContextHolder.getContext();
|
||||
Authentication authentication = context.getAuthentication();
|
||||
if (authentication == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
log.info("==============================");
|
||||
log.info("current user authentication: {}", authentication);
|
||||
log.info("current user authentication getPrincipal: {}", authentication.getPrincipal());
|
||||
log.info("current user authentication getCredentials: {}", authentication.getCredentials());
|
||||
log.info("current user authentication getDetails: {}", authentication.getDetails());
|
||||
log.info("current user authentication getAuthorities: {}", authentication.getAuthorities());
|
||||
if (!authentication.isAuthenticated()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (!(authentication.getPrincipal() instanceof BlogUser user)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(user);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Optional<UUID> currentUserUUID() {
|
||||
return currentUser().map(BlogUser::getUuid);
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +0,0 @@
|
||||
package cn.hamster3.application.blog.vo.blog;
|
||||
|
||||
|
||||
import jakarta.annotation.Nullable;
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BlogCreateRequireVO {
|
||||
@Nullable
|
||||
@Max(value = 16, message = "密码最大长度不能超过 16 个字!")
|
||||
private String password;
|
||||
@NotNull(message = "博客文章内容不能为空!")
|
||||
private String content;
|
||||
}
|
@@ -1,4 +1,19 @@
|
||||
package cn.hamster3.application.blog.vo.blog;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
public class BlogInfoResponseVO {
|
||||
private Long id;
|
||||
private String title;
|
||||
private String abstracts;
|
||||
private String password;
|
||||
private String content;
|
||||
private UUID uploader;
|
||||
private Date createTime;
|
||||
private Date updateTime;
|
||||
|
||||
}
|
||||
|
@@ -1,6 +1,27 @@
|
||||
package cn.hamster3.application.blog.vo.blog;
|
||||
|
||||
import cn.hamster3.application.blog.entity.BlogEntity;
|
||||
import jakarta.annotation.Nullable;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.Data;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
/**
|
||||
* @see BlogEntity
|
||||
*/
|
||||
@Data
|
||||
public class BlogUpdateRequireVO {
|
||||
@Length(max = 32, message = "标题长度不能超过 32 个字符!")
|
||||
@NotBlank(message = "标题不能为空!")
|
||||
private String title;
|
||||
|
||||
@Length(max = 512, message = "摘要长度不能超过 512 个字符!")
|
||||
private String abstracts;
|
||||
|
||||
@Nullable
|
||||
@Length(max = 16, message = "密码最大长度不能超过 16 个字符!")
|
||||
private String password;
|
||||
|
||||
@NotBlank(message = "博客文章内容不能为空!")
|
||||
private String content;
|
||||
}
|
||||
|
Reference in New Issue
Block a user