Commit 2b5039f5 by wzy

post

parent 09dbb0d8
......@@ -14,7 +14,7 @@
<name>blog</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>15</java.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
......@@ -73,6 +73,11 @@
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>
......
......@@ -13,7 +13,6 @@ import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
......@@ -33,7 +32,7 @@ public class AccountController {
User user = userService.getOne(new QueryWrapper<User>().eq("username", loginRequest.getUsername()));
Assert.notNull(user, "用户不存在");
if(!user.getPassword().equals(SecureUtil.md5(loginRequest.getPassword()))) {
return Response.fail("密码错误!");
return Response.fail(401,"密码错误!", null);
}
String jwt = jwtUtils.generateToken(user.getId());
response.setHeader("Authorization", jwt);
......
package cn.blog.controller;
import cn.blog.entity.Post;
import cn.blog.service.IPostService;
import cn.blog.utils.ShiroUtil;
import cn.blog.vo.Response;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 前端控制器
* </p>
*
* @author core
* @since 2021-06-08
*/
@RestController
@RequestMapping("/post")
public class PostController {
@Resource
IPostService iPostService;
@GetMapping()
public Response getPosts(Integer currentPage) {
if(currentPage == null || currentPage < 1)
currentPage = 1;
Page page = new Page(currentPage, 5);
IPage pageData = iPostService.page(page, new QueryWrapper<Post>().orderByDesc("created"));
return Response.success(pageData);
}
@GetMapping("/{id}")
public Response getPost(@PathVariable(name = "id") Long id) {
Post post = iPostService.getById(id);
Assert.notNull(post, "该博客不存在");
return Response.success(post);
}
@PutMapping("/edit")
public Response edit(@Validated @RequestBody Post post) {
Post tempPost = null;
if(post.getId() != null) {
tempPost = iPostService.getById(post.getId());
// 只能编辑自己的文章
System.out.println(ShiroUtil.getProfile().getId());
Assert.isTrue(tempPost.getUserId().longValue() == ShiroUtil.getProfile().getId().longValue(), "没有权限编辑");
} else {
tempPost = new Post();
tempPost.setUserId(ShiroUtil.getProfile().getId());
tempPost.setCreated(LocalDateTime.now());
tempPost.setStatus(0);
}
BeanUtil.copyProperties(post, tempPost, "id", "userId", "created", "status");
iPostService.saveOrUpdate(tempPost);
return Response.success(null);
}
}
......@@ -7,23 +7,13 @@ import lombok.EqualsAndHashCode;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* <p>
*
* </p>
*
* @author core
* @since 2021-06-08
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class User implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
......
......@@ -7,7 +7,6 @@ import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
@Slf4j
......
package cn.blog.utils;
import cn.blog.shiro.AccountProfile;
import org.apache.shiro.SecurityUtils;
public class ShiroUtil {
public static AccountProfile getProfile() {
return (AccountProfile) SecurityUtils.getSubject().getPrincipal();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment