refactor(all): change sql to postgresql
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -79,6 +79,11 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-mail</artifactId>
|
<artifactId>spring-boot-starter-mail</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.postgresql</groupId>
|
||||||
|
<artifactId>postgresql</artifactId>
|
||||||
|
<version>42.7.3</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -13,17 +13,15 @@ public class CustomUserDetails implements UserDetails {
|
|||||||
// Getter 和 Setter
|
// Getter 和 Setter
|
||||||
@Setter
|
@Setter
|
||||||
@Getter
|
@Getter
|
||||||
private String u_id;
|
private Integer u_id;
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
private String role;
|
|
||||||
private Collection<? extends GrantedAuthority> authorities;
|
private Collection<? extends GrantedAuthority> authorities;
|
||||||
|
|
||||||
public CustomUserDetails(String username, String password, String u_id,String role, Collection<? extends GrantedAuthority> authorities) {
|
public CustomUserDetails(String username, String password, Integer u_id, Collection<? extends GrantedAuthority> authorities) {
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.u_id = u_id;
|
this.u_id = u_id;
|
||||||
this.role = role;
|
|
||||||
this.authorities = authorities;
|
this.authorities = authorities;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
package xin.merlin.myblog_server.config;
|
package xin.merlin.myblog_server.config;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import xin.merlin.myblog_server.entity.Account;
|
import xin.merlin.myblog_server.entity.User;
|
||||||
import xin.merlin.myblog_server.service.impl.AccountServiceImpl;
|
import xin.merlin.myblog_server.service.impl.UserServiceImpl;
|
||||||
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@@ -14,12 +14,15 @@ import java.util.ArrayList;
|
|||||||
public class LoginDetails implements UserDetailsService {
|
public class LoginDetails implements UserDetailsService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AccountServiceImpl accountServiceImpl;
|
private UserServiceImpl userServiceImpl;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CustomUserDetails loadUserByUsername(String u_account) throws UsernameNotFoundException {
|
public CustomUserDetails loadUserByUsername(String u_account) throws UsernameNotFoundException {
|
||||||
Account account = accountServiceImpl.getAccountInfo(u_account);
|
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||||
return new CustomUserDetails(account.getU_account(), account.getU_password(),account.getU_id(), account.getRole(),new ArrayList<>());
|
queryWrapper.eq("account", u_account);
|
||||||
|
User user = userServiceImpl.getOne(queryWrapper);
|
||||||
|
if(user == null) return null;
|
||||||
|
return new CustomUserDetails(user.getAccount(), user.getPassword(),user.getId(),new ArrayList<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
import xin.merlin.myblog_server.security.JWTAuthenticationFilter;
|
import xin.merlin.myblog_server.security.JWTAuthenticationFilter;
|
||||||
|
|
||||||
import static org.springframework.security.config.Customizer.withDefaults;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@@ -25,7 +24,6 @@ public class SecurityConfig {
|
|||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
http
|
http
|
||||||
.cors(withDefaults()) // <<<<<< 这里明确加上 withDefaults()
|
|
||||||
.csrf(AbstractHttpConfigurer::disable)
|
.csrf(AbstractHttpConfigurer::disable)
|
||||||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||||||
.authorizeHttpRequests(authz -> authz
|
.authorizeHttpRequests(authz -> authz
|
||||||
@@ -33,10 +31,8 @@ public class SecurityConfig {
|
|||||||
"/login",
|
"/login",
|
||||||
"/register",
|
"/register",
|
||||||
"/test",
|
"/test",
|
||||||
"/admin/login",
|
"/code/**",
|
||||||
"/admin/register",
|
"/blog/**"
|
||||||
"/code/sendcode",
|
|
||||||
"/code/verifycode"
|
|
||||||
).permitAll()
|
).permitAll()
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,259 +0,0 @@
|
|||||||
package xin.merlin.myblog_server.controller;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
import xin.merlin.myblog_server.config.CustomUserDetails;
|
|
||||||
import xin.merlin.myblog_server.config.LoginDetails;
|
|
||||||
import xin.merlin.myblog_server.entity.*;
|
|
||||||
import xin.merlin.myblog_server.service.impl.*;
|
|
||||||
import xin.merlin.myblog_server.utils.JwtUtil;
|
|
||||||
import xin.merlin.myblog_server.utils.RequestBack;
|
|
||||||
import xin.merlin.myblog_server.utils.SHA256Util;
|
|
||||||
import xin.merlin.myblog_server.utils.enums.ResultCode;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.temporal.ChronoUnit;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/admin")
|
|
||||||
public class AdminController {
|
|
||||||
@Autowired
|
|
||||||
private LoginDetails loginDetails;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private AccountServiceImpl accountService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private UserServiceImpl userService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ArticleServiceImpl articleService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private NewsServiceImpl newsService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private CommentServiceImpl commentService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ProjectServiceImpl projectService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private SHA256Util sha256Util;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private JwtUtil jwtUtil;
|
|
||||||
|
|
||||||
@Value("${file.image-dir}")
|
|
||||||
private String avatarDir;
|
|
||||||
|
|
||||||
// 登录逻辑p //admin:Blog_779528
|
|
||||||
@PostMapping("/login")
|
|
||||||
public RequestBack login(@RequestBody Account account) {
|
|
||||||
try {
|
|
||||||
CustomUserDetails userDetails = loginDetails
|
|
||||||
.loadUserByUsername(account.getU_account());
|
|
||||||
if(!userDetails.getRole().equals("Admin")) return RequestBack.fail(ResultCode.USER_NOT_FOUND);
|
|
||||||
account.setU_password(sha256Util
|
|
||||||
.encryptPassword(account.getU_password(),userDetails.getU_id()));
|
|
||||||
|
|
||||||
// 验证密码
|
|
||||||
if(!account.getU_password().equals(userDetails.getPassword()))
|
|
||||||
return RequestBack.success(ResultCode.USER_PASSWORD_ERROR,null);
|
|
||||||
// 生成token
|
|
||||||
String token = jwtUtil.generateToken(account.getU_account(),userDetails.getU_id());
|
|
||||||
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS, Map.of("token",token,"token_type","Bearer","role","Admin"));
|
|
||||||
} catch (UsernameNotFoundException e) {
|
|
||||||
System.out.println(e.getMessage());
|
|
||||||
return RequestBack.fail(ResultCode.SERVER_ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 注册逻辑
|
|
||||||
@PostMapping("/register")
|
|
||||||
public RequestBack register(@RequestBody Account account) {
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 分配id
|
|
||||||
String u_id;
|
|
||||||
u_id = "A000000001";
|
|
||||||
|
|
||||||
|
|
||||||
// 注册信息初始化
|
|
||||||
account.setU_id(u_id);
|
|
||||||
account.setU_password(sha256Util.encryptPassword(account.getU_password(),account.getU_id()));
|
|
||||||
account.setU_status(0);
|
|
||||||
account.setRole("Admin");
|
|
||||||
|
|
||||||
// 注册
|
|
||||||
accountService.register(account);
|
|
||||||
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS,null);
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println(e.getMessage());
|
|
||||||
return RequestBack.fail(ResultCode.SERVER_ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 获取文章p
|
|
||||||
@GetMapping("/get/article")
|
|
||||||
public RequestBack getArticle(@RequestParam Integer current, @RequestParam Integer size) {
|
|
||||||
Page<Article> page = new Page<> (current,size);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS,articleService.page(page));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 发送文章p
|
|
||||||
@PostMapping("/publish/article")
|
|
||||||
public RequestBack publishArticle(@RequestBody Article article) {
|
|
||||||
articleService.publishArticle(article);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS);
|
|
||||||
}
|
|
||||||
// 修改文章p
|
|
||||||
@PostMapping("/update/article")
|
|
||||||
public RequestBack updateArticle(@RequestBody Article article) {
|
|
||||||
articleService.updateArticle(article);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS);
|
|
||||||
}
|
|
||||||
// 删除文章p
|
|
||||||
@PostMapping("/delete/article")
|
|
||||||
public RequestBack deleteArticle(@RequestBody Article article) {
|
|
||||||
articleService.deleteArticle(article);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS);
|
|
||||||
}
|
|
||||||
// 获取新闻p
|
|
||||||
@GetMapping("/get/news")
|
|
||||||
public RequestBack getNews(@RequestParam Integer current,@RequestParam Integer size) {
|
|
||||||
Page<News> page = new Page<>(current,size);
|
|
||||||
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS,newsService.page(page));
|
|
||||||
}
|
|
||||||
// 发送新闻p
|
|
||||||
@PostMapping("/publish/news")
|
|
||||||
public RequestBack publishNews(@RequestBody News news) {
|
|
||||||
news.setPublished(LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS).toString());
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS,newsService.save(news));
|
|
||||||
}
|
|
||||||
// 删除新闻p
|
|
||||||
@PostMapping("/delete/news")
|
|
||||||
public RequestBack deleteNews(@RequestBody News news) {
|
|
||||||
newsService.deleteNews(news);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS);
|
|
||||||
}
|
|
||||||
// 查看评论p
|
|
||||||
@GetMapping("/get/comments")
|
|
||||||
public RequestBack getComments(@RequestParam Integer current,@RequestParam Integer size) {
|
|
||||||
Page<Comment> page = new Page<>(current,size);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS,commentService.page(page));
|
|
||||||
}
|
|
||||||
// 删除评论p
|
|
||||||
@PostMapping("/delete/comments")
|
|
||||||
public RequestBack deleteComments(@RequestBody Comment comment){
|
|
||||||
commentService.deleteComment(comment);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS);
|
|
||||||
}
|
|
||||||
// 查看用户p
|
|
||||||
@GetMapping("/get/users")
|
|
||||||
public RequestBack getUsers(@RequestParam Integer current,@RequestParam Integer size) {
|
|
||||||
Page<User> page = new Page<>(current,size);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS,userService.page(page));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 封禁用户p
|
|
||||||
@PostMapping("/ban/users")
|
|
||||||
public RequestBack banUsers(@RequestBody User user) {
|
|
||||||
String uid= user.getU_id();
|
|
||||||
UpdateWrapper<Account> updateWrapper = new UpdateWrapper<>();
|
|
||||||
updateWrapper.eq("u_id",uid)
|
|
||||||
.set("u_status",1);
|
|
||||||
accountService.update(updateWrapper);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS);
|
|
||||||
}
|
|
||||||
// 查看联系 //(缓)
|
|
||||||
@GetMapping("/get/contact")
|
|
||||||
public RequestBack getContact(@RequestParam Integer current,@RequestParam Integer size) {
|
|
||||||
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 允许的文件类型
|
|
||||||
private static final List<String> ALLOWED_EXTENSIONS = Arrays.asList(".jpg", ".jpeg", ".png", ".gif");
|
|
||||||
|
|
||||||
// 上传图片p
|
|
||||||
@PostMapping("/upload/img")
|
|
||||||
public RequestBack uploadImg(@RequestParam("image") MultipartFile file) {
|
|
||||||
try {
|
|
||||||
File dir = new File(avatarDir);
|
|
||||||
if (!dir.exists()) dir.mkdirs();
|
|
||||||
|
|
||||||
// 取文件扩展名并检查是否合法
|
|
||||||
String originalFileName = file.getOriginalFilename();
|
|
||||||
String fileExtension = "";
|
|
||||||
if (originalFileName != null && originalFileName.contains(".")) {
|
|
||||||
fileExtension = originalFileName.substring(originalFileName.lastIndexOf(".")).toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!ALLOWED_EXTENSIONS.contains(fileExtension)) {
|
|
||||||
throw new IOException("仅支持 JPG, PNG, GIF 格式");
|
|
||||||
}
|
|
||||||
// 生成 UUID 作为文件名
|
|
||||||
String uuid = UUID.randomUUID().toString();
|
|
||||||
String fileName = uuid+ fileExtension;
|
|
||||||
Path targetPath = Paths.get(avatarDir, fileName);
|
|
||||||
Files.copy(file.getInputStream(), targetPath, StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS,Map.of("url","https://merlin.xin/images/"+fileName));
|
|
||||||
}catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return RequestBack.fail(ResultCode.SERVER_ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取项目p
|
|
||||||
@GetMapping("/get/projects")
|
|
||||||
public RequestBack getProjects(@RequestParam Integer current,@RequestParam Integer size) {
|
|
||||||
Page<Project> page = new Page<>(current,size);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS,projectService.page(page));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 发布项目p
|
|
||||||
@PostMapping("/publish/project")
|
|
||||||
public RequestBack publishProject(@RequestBody Project project) {
|
|
||||||
projectService.save(project);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// 修改项目p
|
|
||||||
@PostMapping("/update/project")
|
|
||||||
public RequestBack updateProject(@RequestBody Project project) {
|
|
||||||
UpdateWrapper<Project> updateWrapper = new UpdateWrapper<>();
|
|
||||||
updateWrapper.eq("p_id",project.getP_id());
|
|
||||||
projectService.update(project,updateWrapper);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除项目p
|
|
||||||
@PostMapping("/delete/project")
|
|
||||||
public RequestBack deleteProject(@RequestBody Project project) {
|
|
||||||
projectService.deleteProject(project.getP_id());
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import xin.merlin.myblog_server.entity.Comment;
|
import xin.merlin.myblog_server.entity.Comment;
|
||||||
import xin.merlin.myblog_server.entity.News;
|
import xin.merlin.myblog_server.entity.News;
|
||||||
import xin.merlin.myblog_server.entity.Project;
|
|
||||||
import xin.merlin.myblog_server.service.impl.*;
|
import xin.merlin.myblog_server.service.impl.*;
|
||||||
import xin.merlin.myblog_server.utils.JwtUtil;
|
import xin.merlin.myblog_server.utils.JwtUtil;
|
||||||
import xin.merlin.myblog_server.utils.RequestBack;
|
import xin.merlin.myblog_server.utils.RequestBack;
|
||||||
@@ -26,9 +25,6 @@ public class BasicController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserServiceImpl userService;
|
private UserServiceImpl userService;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ProjectServiceImpl projectService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CommentServiceImpl commentService;
|
private CommentServiceImpl commentService;
|
||||||
|
|
||||||
@@ -53,12 +49,6 @@ public class BasicController {
|
|||||||
public RequestBack getArticle(@PathVariable Integer a_id) {
|
public RequestBack getArticle(@PathVariable Integer a_id) {
|
||||||
return RequestBack.success(ResultCode.SUCCESS,articleService.getById(a_id));
|
return RequestBack.success(ResultCode.SUCCESS,articleService.getById(a_id));
|
||||||
}
|
}
|
||||||
// 获取项目
|
|
||||||
@GetMapping("/get/projects")
|
|
||||||
public RequestBack getProjects(@RequestParam Integer current,@RequestParam Integer size) {
|
|
||||||
Page<Project> page = new Page<>(current,size);
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS,projectService.page(page));
|
|
||||||
}
|
|
||||||
// 参与项目
|
// 参与项目
|
||||||
// 发表评论
|
// 发表评论
|
||||||
@PostMapping("/publish/comment")
|
@PostMapping("/publish/comment")
|
||||||
|
|||||||
@@ -1,18 +1,21 @@
|
|||||||
package xin.merlin.myblog_server.controller;
|
package xin.merlin.myblog_server.controller;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.github.benmanes.caffeine.cache.Cache;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import xin.merlin.myblog_server.config.CustomUserDetails;
|
import xin.merlin.myblog_server.config.CustomUserDetails;
|
||||||
import xin.merlin.myblog_server.config.LoginDetails;
|
import xin.merlin.myblog_server.config.LoginDetails;
|
||||||
import xin.merlin.myblog_server.entity.Account;
|
import xin.merlin.myblog_server.entity.User;
|
||||||
import xin.merlin.myblog_server.service.impl.AccountServiceImpl;
|
import xin.merlin.myblog_server.service.CacheService;
|
||||||
|
import xin.merlin.myblog_server.service.impl.UserServiceImpl;
|
||||||
import xin.merlin.myblog_server.utils.JwtUtil;
|
import xin.merlin.myblog_server.utils.JwtUtil;
|
||||||
import xin.merlin.myblog_server.utils.RandomCode;
|
|
||||||
import xin.merlin.myblog_server.utils.RequestBack;
|
import xin.merlin.myblog_server.utils.RequestBack;
|
||||||
import xin.merlin.myblog_server.utils.SHA256Util;
|
import xin.merlin.myblog_server.utils.SHA256Util;
|
||||||
import xin.merlin.myblog_server.utils.enums.ResultCode;
|
import xin.merlin.myblog_server.utils.enums.ResultCode;
|
||||||
@@ -27,7 +30,7 @@ public class LoginController {
|
|||||||
private LoginDetails loginDetails;
|
private LoginDetails loginDetails;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AccountServiceImpl accountServiceImpl;
|
private UserServiceImpl userServiceImpl;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private SHA256Util sha256Util;
|
private SHA256Util sha256Util;
|
||||||
@@ -38,25 +41,32 @@ public class LoginController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private HttpServletRequest request;
|
private HttpServletRequest request;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CacheService cacheService;
|
||||||
|
|
||||||
// 登录逻辑
|
// 登录逻辑
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public RequestBack login(@RequestBody Account account) {
|
public RequestBack login(@RequestBody User user) {
|
||||||
try {
|
try {
|
||||||
String ip = request.getRemoteAddr();
|
String ip = request.getRemoteAddr();
|
||||||
account.setIp(ip);
|
user.setIp(ip);
|
||||||
|
|
||||||
CustomUserDetails userDetails = loginDetails
|
CustomUserDetails userDetails = loginDetails
|
||||||
.loadUserByUsername(account.getU_account());
|
.loadUserByUsername(user.getAccount());
|
||||||
account.setU_password(sha256Util
|
user.setPassword(sha256Util
|
||||||
.encryptPassword(account.getU_password(),userDetails.getU_id()));
|
.encryptPassword(user.getPassword()));
|
||||||
|
|
||||||
|
if (userDetails == null) {
|
||||||
|
return RequestBack.success(ResultCode.USER_NOT_FOUND);
|
||||||
|
}
|
||||||
//System.out.println(account.getU_password());
|
//System.out.println(account.getU_password());
|
||||||
// 验证密码
|
// 验证密码
|
||||||
if(!account.getU_password().equals(userDetails.getPassword()))
|
if(!user.getPassword().equals(userDetails.getPassword()))
|
||||||
return RequestBack.success(ResultCode.USER_PASSWORD_ERROR,null);
|
return RequestBack.success(ResultCode.USER_PASSWORD_ERROR,null);
|
||||||
|
|
||||||
//System.out.println(userDetails.getU_id());
|
//System.out.println(userDetails.getU_id());
|
||||||
// 生成token
|
// 生成token
|
||||||
String token = jwtUtil.generateToken(account.getU_account(),userDetails.getU_id());
|
String token = jwtUtil.generateToken(user.getAccount(),userDetails.getU_id());
|
||||||
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS, Map.of("token",token,"token_type","Bearer","role","User"));
|
return RequestBack.success(ResultCode.SUCCESS, Map.of("token",token,"token_type","Bearer","role","User"));
|
||||||
} catch (UsernameNotFoundException e) {
|
} catch (UsernameNotFoundException e) {
|
||||||
@@ -67,24 +77,20 @@ public class LoginController {
|
|||||||
|
|
||||||
// 注册逻辑
|
// 注册逻辑
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
public RequestBack register(@RequestBody Account account) {
|
public RequestBack register(@RequestBody User user, @RequestParam String c_id) {
|
||||||
|
if(c_id == null || cacheService.getWaitingList().getIfPresent(c_id)==null) return RequestBack.fail(ResultCode.USER_VERIFICATION_ERROR);
|
||||||
try {
|
try {
|
||||||
if(accountServiceImpl.isExist(account.getU_account())) return RequestBack.success(ResultCode.USER_EXIST,null);
|
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||||
// 分配id
|
queryWrapper.eq("account", user.getAccount());
|
||||||
String u_id;
|
if(userServiceImpl.exists(queryWrapper)) return RequestBack.success(ResultCode.USER_EXIST,null);
|
||||||
do{
|
|
||||||
u_id = "U"+ RandomCode.generateID();
|
|
||||||
}while(accountServiceImpl.idIsExist(u_id));
|
|
||||||
|
|
||||||
// 注册信息初始化
|
// 注册信息初始化
|
||||||
account.setU_id(u_id);
|
user.setIp(request.getRemoteAddr());
|
||||||
account.setU_password(sha256Util.encryptPassword(account.getU_password(),account.getU_id()));
|
user.setPassword(sha256Util.encryptPassword(user.getPassword()));
|
||||||
account.setU_status(0);
|
|
||||||
account.setRole("User");
|
|
||||||
|
|
||||||
// 注册
|
// 注册
|
||||||
accountServiceImpl.register(account);
|
userServiceImpl.save(user);
|
||||||
|
cacheService.getWaitingList().invalidate(c_id);
|
||||||
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS,null);
|
return RequestBack.success(ResultCode.SUCCESS,null);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -8,8 +8,9 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import xin.merlin.myblog_server.entity.Account;
|
|
||||||
import xin.merlin.myblog_server.entity.Code;
|
import xin.merlin.myblog_server.entity.Code;
|
||||||
|
import xin.merlin.myblog_server.entity.User;
|
||||||
|
import xin.merlin.myblog_server.service.CacheService;
|
||||||
import xin.merlin.myblog_server.service.impl.MailService;
|
import xin.merlin.myblog_server.service.impl.MailService;
|
||||||
import xin.merlin.myblog_server.utils.RandomCode;
|
import xin.merlin.myblog_server.utils.RandomCode;
|
||||||
import xin.merlin.myblog_server.utils.RequestBack;
|
import xin.merlin.myblog_server.utils.RequestBack;
|
||||||
@@ -21,10 +22,9 @@ import java.util.concurrent.TimeUnit;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/code")
|
@RequestMapping("/code")
|
||||||
public class MailController {
|
public class MailController {
|
||||||
private static final Cache<String, String> waitingList = Caffeine.newBuilder()
|
@Autowired
|
||||||
.expireAfterWrite(5, TimeUnit.MINUTES)
|
private CacheService cacheService;
|
||||||
.build();
|
|
||||||
|
|
||||||
// 冷却缓存:限制邮箱请求频率
|
// 冷却缓存:限制邮箱请求频率
|
||||||
private static final Cache<String, Boolean> emailCooldown = Caffeine.newBuilder()
|
private static final Cache<String, Boolean> emailCooldown = Caffeine.newBuilder()
|
||||||
.expireAfterWrite(60, TimeUnit.SECONDS) // 冷却 60 秒
|
.expireAfterWrite(60, TimeUnit.SECONDS) // 冷却 60 秒
|
||||||
@@ -39,10 +39,10 @@ public class MailController {
|
|||||||
private MailService mailService;
|
private MailService mailService;
|
||||||
|
|
||||||
@PostMapping("/sendcode")
|
@PostMapping("/sendcode")
|
||||||
RequestBack sendcode(@RequestBody Account account) {
|
RequestBack sendcode(@RequestBody User user) {
|
||||||
if (account.getU_account() == null) return RequestBack.fail(ResultCode.BAD_REQUEST);
|
if (user.getAccount() == null) return RequestBack.fail(ResultCode.BAD_REQUEST);
|
||||||
System.out.println("发送验证码到:" + account.getU_account());
|
System.out.println("发送验证码到:" + user.getAccount());
|
||||||
String email = account.getU_account();
|
String email = user.getAccount();
|
||||||
|
|
||||||
// 检查是否在冷却中
|
// 检查是否在冷却中
|
||||||
if (emailCooldown.getIfPresent(email) != null) {
|
if (emailCooldown.getIfPresent(email) != null) {
|
||||||
@@ -52,12 +52,12 @@ public class MailController {
|
|||||||
do {
|
do {
|
||||||
tempId = RandomCode.generateCode();
|
tempId = RandomCode.generateCode();
|
||||||
}
|
}
|
||||||
while (waitingList.getIfPresent(tempId) != null);
|
while (cacheService.getWaitingList().getIfPresent(tempId) != null);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
waitingList.put(tempId, mailService.sendMail(account.getU_account()));
|
cacheService.getWaitingList().put(tempId, mailService.sendMail(user.getAccount()));
|
||||||
// 加入验证码冷却
|
// 加入验证码冷却
|
||||||
emailCooldown.put(account.getU_account(), true);
|
emailCooldown.put(user.getAccount(), true);
|
||||||
return RequestBack.success(ResultCode.SUCCESS, Map.of("c_id", tempId));
|
return RequestBack.success(ResultCode.SUCCESS, Map.of("c_id", tempId));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return RequestBack.fail(ResultCode.SERVER_ERROR);
|
return RequestBack.fail(ResultCode.SERVER_ERROR);
|
||||||
@@ -81,13 +81,13 @@ public class MailController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
String tempCode = waitingList.getIfPresent(id);
|
String tempCode = cacheService.getWaitingList().getIfPresent(id);
|
||||||
System.out.println("waitingList" + tempCode + "\nv_id:" + id + "\ncode:" + code.getCode());
|
System.out.println("cacheService.getWaitingList()" + tempCode + "\nv_id:" + id + "\ncode:" + code.getCode());
|
||||||
if (tempCode == null) return RequestBack.success(ResultCode.USER_VERIFICATION_ERROR);
|
if (tempCode == null) return RequestBack.success(ResultCode.USER_VERIFICATION_ERROR);
|
||||||
if (!tempCode.equals(code.getCode())) return RequestBack.success(ResultCode.USER_VERIFICATION_ERROR);
|
if (!tempCode.equals(code.getCode())) return RequestBack.success(ResultCode.USER_VERIFICATION_ERROR);
|
||||||
waitingList.invalidate(id);
|
// cacheService.getWaitingList().invalidate(id);
|
||||||
codeFailCount.invalidate(id);
|
codeFailCount.invalidate(id);
|
||||||
emailCooldown.invalidate(code.getU_account());
|
emailCooldown.invalidate(code.getAccount());
|
||||||
|
|
||||||
return RequestBack.success(ResultCode.SUCCESS);
|
return RequestBack.success(ResultCode.SUCCESS);
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public class TestController {
|
|||||||
|
|
||||||
@GetMapping("/test")
|
@GetMapping("/test")
|
||||||
public String test() {
|
public String test() {
|
||||||
String token = jwtUtil.generateToken("1223","U123");
|
String token = jwtUtil.generateToken("1223",12);
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
package xin.merlin.myblog_server.entity;
|
|
||||||
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@TableName("account")
|
|
||||||
public class Account {
|
|
||||||
|
|
||||||
@TableId("u_id")
|
|
||||||
private String u_id;
|
|
||||||
private String u_account;
|
|
||||||
private String u_password;
|
|
||||||
private int u_status;
|
|
||||||
private String ip;
|
|
||||||
private String role;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -7,12 +7,11 @@ import lombok.Data;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@TableName("article")
|
@TableName("articles")
|
||||||
public class Article {
|
public class Article {
|
||||||
@TableId("a_id")
|
@TableId("id")
|
||||||
private Integer a_id;
|
private Integer id;
|
||||||
private String title;
|
private String title;
|
||||||
private String content;
|
private String content;
|
||||||
private LocalDateTime created;
|
private String published;
|
||||||
private LocalDateTime updated;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import lombok.Data;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class Code {
|
public class Code {
|
||||||
private String u_account;
|
private String account;
|
||||||
private String c_id;
|
private String c_id;
|
||||||
private String code;
|
private String code;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ import com.baomidou.mybatisplus.annotation.TableName;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@TableName("comment")
|
@TableName("comments")
|
||||||
public class Comment {
|
public class Comment {
|
||||||
@TableId("a_id")
|
@TableId("id")
|
||||||
|
private Integer id;
|
||||||
|
private Integer u_id;
|
||||||
private Integer a_id;
|
private Integer a_id;
|
||||||
private String sender;
|
private String published;
|
||||||
private String profile;
|
private String content;
|
||||||
private String comment;
|
|
||||||
private String sent;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
package xin.merlin.myblog_server.entity;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@TableName("contact")
|
|
||||||
public class Contact {
|
|
||||||
private String from;
|
|
||||||
private String to;
|
|
||||||
private String message;
|
|
||||||
private String time;
|
|
||||||
private int handled;
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
package xin.merlin.myblog_server.entity;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@TableName("garbage")
|
|
||||||
public class Garbage {
|
|
||||||
@TableId("delete_id")
|
|
||||||
private Integer delete_id;
|
|
||||||
private String type;
|
|
||||||
private String content;
|
|
||||||
|
|
||||||
public Garbage(String type, String content) {
|
|
||||||
this.type = type;
|
|
||||||
this.content = content;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,10 +9,11 @@ import java.time.LocalDateTime;
|
|||||||
@Data
|
@Data
|
||||||
@TableName("news")
|
@TableName("news")
|
||||||
public class News {
|
public class News {
|
||||||
@TableId("a_id")
|
@TableId("id")
|
||||||
private Integer a_id;
|
private Integer id;
|
||||||
private String n_title;
|
private String title;
|
||||||
private String synopsis;
|
private String content;
|
||||||
private String published;
|
private String published;
|
||||||
|
private Integer[] related;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
package xin.merlin.myblog_server.entity;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@TableName("project")
|
|
||||||
public class Project {
|
|
||||||
@TableId("p_id")
|
|
||||||
private Integer p_id;
|
|
||||||
private String p_name;
|
|
||||||
private String techstack;
|
|
||||||
private String details;
|
|
||||||
private String p_status;
|
|
||||||
}
|
|
||||||
@@ -6,10 +6,13 @@ import com.baomidou.mybatisplus.annotation.TableName;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@TableName("user")
|
@TableName("users")
|
||||||
public class User {
|
public class User {
|
||||||
@TableId("u_id")
|
@TableId("id")
|
||||||
private String u_id;
|
private Integer id;
|
||||||
private String u_name;
|
private String name;
|
||||||
private String u_avatar;
|
private String profile;
|
||||||
|
private String account;
|
||||||
|
private String password;
|
||||||
|
private String ip;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
package xin.merlin.myblog_server.mapper;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
|
||||||
import xin.merlin.myblog_server.entity.Account;
|
|
||||||
|
|
||||||
@Mapper
|
|
||||||
public interface AccountMapper extends BaseMapper<Account> {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package xin.merlin.myblog_server.mapper;
|
|
||||||
|
|
||||||
public interface ContactMapper {
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package xin.merlin.myblog_server.mapper;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
|
||||||
import xin.merlin.myblog_server.entity.Garbage;
|
|
||||||
|
|
||||||
@Mapper
|
|
||||||
public interface GarbageMapper extends BaseMapper<Garbage> {
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package xin.merlin.myblog_server.mapper;
|
|
||||||
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
|
||||||
import xin.merlin.myblog_server.entity.Project;
|
|
||||||
|
|
||||||
@Mapper
|
|
||||||
public interface ProjectMapper extends BaseMapper<Project> {
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package xin.merlin.myblog_server.service;
|
||||||
|
|
||||||
|
|
||||||
|
import com.github.benmanes.caffeine.cache.Cache;
|
||||||
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CacheService {
|
||||||
|
|
||||||
|
private static final Cache<String, String> waitingList = Caffeine.newBuilder()
|
||||||
|
.expireAfterWrite(5, TimeUnit.MINUTES)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
public Cache<String, String> getWaitingList() {
|
||||||
|
return waitingList;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package xin.merlin.myblog_server.service;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
|
||||||
import xin.merlin.myblog_server.entity.Project;
|
|
||||||
|
|
||||||
public interface ProjectService extends IService<Project> {
|
|
||||||
public void deleteProject(Integer p_id);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package xin.merlin.myblog_server.service;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import xin.merlin.myblog_server.entity.User;
|
||||||
|
|
||||||
|
public interface UserService extends IService<User> {
|
||||||
|
}
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
package xin.merlin.myblog_server.service.impl;
|
|
||||||
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import xin.merlin.myblog_server.entity.Account;
|
|
||||||
import xin.merlin.myblog_server.mapper.AccountMapper;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class AccountServiceImpl extends ServiceImpl<AccountMapper,Account> {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private AccountMapper accountMapper;
|
|
||||||
|
|
||||||
public Account getAccountInfo(String u_account) {
|
|
||||||
QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
|
|
||||||
queryWrapper.eq("u_account", u_account);
|
|
||||||
return accountMapper.selectOne(queryWrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isExist(String u_account) {
|
|
||||||
QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
|
|
||||||
queryWrapper.eq("u_account", u_account);
|
|
||||||
return accountMapper.selectCount(queryWrapper) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean idIsExist(String u_id){
|
|
||||||
QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
|
|
||||||
queryWrapper.eq("u_id", u_id);
|
|
||||||
return accountMapper.selectCount(queryWrapper) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void register(Account account) {
|
|
||||||
accountMapper.insert(account);
|
|
||||||
}
|
|
||||||
|
|
||||||
// public String getU_id(String u_account) {
|
|
||||||
// QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
|
|
||||||
// queryWrapper.eq("u_account", u_account);
|
|
||||||
// Account account = accountMapper.selectOne(queryWrapper);
|
|
||||||
// return account.getU_account();
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
@@ -4,10 +4,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|||||||
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 xin.merlin.myblog_server.entity.Article;
|
import xin.merlin.myblog_server.entity.Article;
|
||||||
import xin.merlin.myblog_server.entity.Garbage;
|
|
||||||
import xin.merlin.myblog_server.entity.News;
|
import xin.merlin.myblog_server.entity.News;
|
||||||
import xin.merlin.myblog_server.mapper.ArticleMapper;
|
import xin.merlin.myblog_server.mapper.ArticleMapper;
|
||||||
import xin.merlin.myblog_server.mapper.GarbageMapper;
|
|
||||||
import xin.merlin.myblog_server.mapper.NewsMapper;
|
import xin.merlin.myblog_server.mapper.NewsMapper;
|
||||||
import xin.merlin.myblog_server.service.ArticleService;
|
import xin.merlin.myblog_server.service.ArticleService;
|
||||||
import xin.merlin.myblog_server.utils.GarbageBaler;
|
import xin.merlin.myblog_server.utils.GarbageBaler;
|
||||||
@@ -25,30 +23,17 @@ public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> impl
|
|||||||
@Autowired
|
@Autowired
|
||||||
private NewsMapper newsMapper;
|
private NewsMapper newsMapper;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private GarbageMapper garbageMapper;
|
|
||||||
|
|
||||||
public void publishArticle(Article article) {
|
public void publishArticle(Article article) {
|
||||||
article.setCreated(LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS));
|
|
||||||
article.setUpdated(LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS));
|
|
||||||
articleMapper.insert(article);
|
articleMapper.insert(article);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateArticle(Article article) {
|
public void updateArticle(Article article) {
|
||||||
article.setUpdated(LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS));
|
|
||||||
articleMapper.updateById(article);
|
articleMapper.updateById(article);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteArticle(Article article) {
|
public void deleteArticle(Article article) {
|
||||||
Article article1 = articleMapper.selectById(article.getA_id());
|
|
||||||
article1.setUpdated(LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS));
|
|
||||||
News news1 = newsMapper.selectById(article.getA_id());
|
|
||||||
|
|
||||||
garbageMapper.insert(new Garbage("article", GarbageBaler.concatenateFields(article1)));
|
|
||||||
garbageMapper.insert(new Garbage("news", GarbageBaler.concatenateFields(news1)));
|
|
||||||
|
|
||||||
newsMapper.deleteById(article.getA_id());
|
|
||||||
articleMapper.deleteById(article.getA_id());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|||||||
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 xin.merlin.myblog_server.entity.Comment;
|
import xin.merlin.myblog_server.entity.Comment;
|
||||||
import xin.merlin.myblog_server.entity.Garbage;
|
|
||||||
import xin.merlin.myblog_server.mapper.CommentMapper;
|
import xin.merlin.myblog_server.mapper.CommentMapper;
|
||||||
import xin.merlin.myblog_server.mapper.GarbageMapper;
|
|
||||||
import xin.merlin.myblog_server.service.CommentService;
|
import xin.merlin.myblog_server.service.CommentService;
|
||||||
import xin.merlin.myblog_server.utils.GarbageBaler;
|
import xin.merlin.myblog_server.utils.GarbageBaler;
|
||||||
|
|
||||||
@@ -16,12 +14,9 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> impl
|
|||||||
@Autowired
|
@Autowired
|
||||||
private CommentMapper commentMapper;
|
private CommentMapper commentMapper;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private GarbageMapper garbageMapper;
|
|
||||||
|
|
||||||
public void deleteComment(Comment comment) {
|
public void deleteComment(Comment comment) {
|
||||||
garbageMapper.insert(new Garbage("comment", GarbageBaler.concatenateFields(comment)));
|
|
||||||
commentMapper.deleteById(comment.getA_id());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public class MailService {
|
|||||||
message.setFrom(sender);
|
message.setFrom(sender);
|
||||||
message.setTo(receiver);
|
message.setTo(receiver);
|
||||||
message.setSubject("Merlin`s Blog Server");
|
message.setSubject("Merlin`s Blog Server");
|
||||||
message.setText("欢迎注册Blog!\n"+"您的验证码为:"+code+"\n有效期5分钟\n验证码请勿泄露!");
|
message.setText("欢迎注册Merlin`s Blog!\n"+"您的验证码为:"+code+"\n有效期5分钟\n验证码请勿泄露!");
|
||||||
mailSender.send(message);
|
mailSender.send(message);
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,7 @@ package xin.merlin.myblog_server.service.impl;
|
|||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
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 xin.merlin.myblog_server.entity.Garbage;
|
|
||||||
import xin.merlin.myblog_server.entity.News;
|
import xin.merlin.myblog_server.entity.News;
|
||||||
import xin.merlin.myblog_server.mapper.GarbageMapper;
|
|
||||||
import xin.merlin.myblog_server.mapper.NewsMapper;
|
import xin.merlin.myblog_server.mapper.NewsMapper;
|
||||||
import xin.merlin.myblog_server.service.NewsService;
|
import xin.merlin.myblog_server.service.NewsService;
|
||||||
import xin.merlin.myblog_server.utils.GarbageBaler;
|
import xin.merlin.myblog_server.utils.GarbageBaler;
|
||||||
@@ -16,13 +14,8 @@ public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements Ne
|
|||||||
@Autowired
|
@Autowired
|
||||||
private NewsMapper newsMapper;
|
private NewsMapper newsMapper;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private GarbageMapper garbageMapper;
|
|
||||||
|
|
||||||
public void deleteNews(News news){
|
public void deleteNews(News news){
|
||||||
|
|
||||||
garbageMapper.insert(new Garbage("news", GarbageBaler.concatenateFields(news)));
|
|
||||||
|
|
||||||
newsMapper.deleteById(news.getA_id());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
package xin.merlin.myblog_server.service.impl;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import xin.merlin.myblog_server.entity.Garbage;
|
|
||||||
import xin.merlin.myblog_server.entity.Project;
|
|
||||||
import xin.merlin.myblog_server.mapper.GarbageMapper;
|
|
||||||
import xin.merlin.myblog_server.mapper.ProjectMapper;
|
|
||||||
import xin.merlin.myblog_server.service.ProjectService;
|
|
||||||
import xin.merlin.myblog_server.utils.GarbageBaler;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ProjectMapper projectMapper;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private GarbageMapper garbageMapper;
|
|
||||||
|
|
||||||
|
|
||||||
public void deleteProject(Integer p_id) {
|
|
||||||
Project project = projectMapper.selectById(p_id);
|
|
||||||
garbageMapper.insert(new Garbage("project", GarbageBaler.concatenateFields(project)));
|
|
||||||
projectMapper.deleteById(p_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,14 +5,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|||||||
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 xin.merlin.myblog_server.entity.User;
|
import xin.merlin.myblog_server.entity.User;
|
||||||
import xin.merlin.myblog_server.mapper.AccountMapper;
|
|
||||||
import xin.merlin.myblog_server.mapper.UserMapper;
|
import xin.merlin.myblog_server.mapper.UserMapper;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User> {
|
public class UserServiceImpl extends ServiceImpl<UserMapper, User> {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private AccountMapper accountMapper;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -37,13 +37,13 @@ public class JwtUtil {
|
|||||||
/**
|
/**
|
||||||
* 生成 JWT Token
|
* 生成 JWT Token
|
||||||
*/
|
*/
|
||||||
public String generateToken(String uAccount, String uId) {
|
public String generateToken(String uAccount, Integer uId) {
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
Date expireDate = new Date(now.getTime() + jwtProperties.getExpire() * 1000L);
|
Date expireDate = new Date(now.getTime() + jwtProperties.getExpire() * 1000L);
|
||||||
|
|
||||||
return Jwts.builder()
|
return Jwts.builder()
|
||||||
.subject(uAccount)
|
.subject(uAccount)
|
||||||
.claim("u_id", uId)
|
.claim("id", uId)
|
||||||
.id(UUID.randomUUID().toString())
|
.id(UUID.randomUUID().toString())
|
||||||
.issuedAt(now)
|
.issuedAt(now)
|
||||||
.expiration(expireDate)
|
.expiration(expireDate)
|
||||||
@@ -90,7 +90,7 @@ public class JwtUtil {
|
|||||||
*/
|
*/
|
||||||
public String getUId(String token) {
|
public String getUId(String token) {
|
||||||
Claims claims = getClaims(token);
|
Claims claims = getClaims(token);
|
||||||
return claims.get("u_id", String.class);
|
return claims.get("id", String.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 自定义异常类
|
// 自定义异常类
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package xin.merlin.myblog_server.utils;
|
package xin.merlin.myblog_server.utils;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
@@ -42,13 +43,14 @@ public class SHA256Util {
|
|||||||
/**
|
/**
|
||||||
* 使用用户ID生成盐值,并对密码进行加密
|
* 使用用户ID生成盐值,并对密码进行加密
|
||||||
* @param password 用户输入的密码
|
* @param password 用户输入的密码
|
||||||
* @param userId 用户ID
|
|
||||||
* @return 加密后的密码哈希值
|
* @return 加密后的密码哈希值
|
||||||
*/
|
*/
|
||||||
public String encryptPassword(String password, String userId) {
|
@Value("${jwt.salt}")
|
||||||
// 先对用户ID进行SHA-256加密,得到盐值
|
private String salt;
|
||||||
String salt = encryptSHA256(userId);
|
|
||||||
|
public String encryptPassword(String password) {
|
||||||
|
String s = encryptSHA256(salt);
|
||||||
// 将盐值与密码拼接后进行SHA-256加密
|
// 将盐值与密码拼接后进行SHA-256加密
|
||||||
return encryptSHA256(salt +salt + password + salt);
|
return encryptSHA256(s +s + password + s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,36 +1,45 @@
|
|||||||
server:
|
server:
|
||||||
# port: 8080
|
port: 8080
|
||||||
port: 8443
|
# port: 8443
|
||||||
ssl:
|
# ssl:
|
||||||
key-store: classpath:merlin.xin.pfx
|
# key-store: classpath:merlin.xin.pfx
|
||||||
key-store-password: 7p7vcfmu
|
# key-store-password: 7p7vcfmu
|
||||||
key-store-type: PKCS12
|
# key-store-type: PKCS12
|
||||||
address: 0.0.0.0
|
# address: 0.0.0.0
|
||||||
|
|
||||||
jwt:
|
jwt:
|
||||||
secret: CkmEXxVBNBsMUo4VNhDcH0YBhA1O4zSkQgSM243YzDY=
|
secret: CkmEXxVBNBsMUo4VNhDcH0YBhA1O4zSkQgSM243YzDY=
|
||||||
issuer: blogAdmin
|
issuer: blogAdmin
|
||||||
subject: Interesting
|
subject: Interesting
|
||||||
expire: 604800
|
expire: 604800
|
||||||
|
salt: sdjhjksdzkfhjkdzs
|
||||||
|
|
||||||
file:
|
file:
|
||||||
# image-dir: C:/uploads/images #图片储存目录
|
image-dir: C:/Temp/uploads/images #图片储存目录
|
||||||
# avatar-dir: C:/uploads/avatars # 头像存储目录(Windows 环境)
|
avatar-dir: C:/Temp/uploads/avatars # 头像存储目录(Windows 环境)
|
||||||
|
|
||||||
avatar-dir: /home/blog/uploads/avatars # 头像储存目录(Linux 环境)
|
# avatar-dir: /home/blog/uploads/avatars # 头像储存目录(Linux 环境)
|
||||||
image-dir: /home/blog/uploads/image
|
# image-dir: /home/blog/uploads/image
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
servlet:
|
servlet:
|
||||||
multipart:
|
multipart:
|
||||||
max-file-size: 50MB
|
max-file-size: 50MB
|
||||||
datasource:
|
datasource:
|
||||||
url: jdbc:mysql://8.138.214.149:3306/blog
|
driver-class-name: org.postgresql.Driver
|
||||||
|
url: jdbc:postgresql://localhost:5432/Blog
|
||||||
|
username: Merlin
|
||||||
|
password: 123456
|
||||||
|
|
||||||
|
jackson:
|
||||||
|
time-zone: Asia/Shanghai
|
||||||
|
date-format: yyyy-MM-dd HH:mm:ss
|
||||||
|
# url: jdbc:mysql://8.138.214.149:3306/blog
|
||||||
# username: root
|
# username: root
|
||||||
# password: 3604162
|
# password: 3604162
|
||||||
username: root
|
# username: root
|
||||||
password: server2025_xyf_Merlin
|
# password: server2025_xyf_Merlin
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
# driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
|
||||||
|
|
||||||
mail:
|
mail:
|
||||||
@@ -60,7 +69,7 @@ mybatis-plus:
|
|||||||
id-type: auto # 主键策略
|
id-type: auto # 主键策略
|
||||||
configuration:
|
configuration:
|
||||||
map-underscore-to-camel-case: false # 禁用驼峰命名自动映射
|
map-underscore-to-camel-case: false # 禁用驼峰命名自动映射
|
||||||
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 控制台打印 SQL(调试用)
|
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 控制台打印 SQL(调试用)
|
||||||
logging:
|
logging:
|
||||||
level:
|
level:
|
||||||
org:
|
org:
|
||||||
|
|||||||
Reference in New Issue
Block a user