Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
springboot-mybatis
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wzy
springboot-mybatis
Commits
2b5039f5
Commit
2b5039f5
authored
Jun 10, 2021
by
wzy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
post
parent
09dbb0d8
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
72 additions
and
25 deletions
+72
-25
pom.xml
+6
-1
src/main/java/cn/blog/controller/AccountController.java
+1
-2
src/main/java/cn/blog/controller/PostController.java
+55
-11
src/main/java/cn/blog/entity/User.java
+0
-10
src/main/java/cn/blog/utils/JwtUtils.java
+0
-1
src/main/java/cn/blog/utils/ShiroUtil.java
+10
-0
No files found.
pom.xml
View file @
2b5039f5
...
...
@@ -14,7 +14,7 @@
<name>
blog
</name>
<description>
Demo project for Spring Boot
</description>
<properties>
<java.version>
1
5
</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>
...
...
src/main/java/cn/blog/controller/AccountController.java
View file @
2b5039f5
...
...
@@ -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
);
...
...
src/main/java/cn/blog/controller/PostController.java
View file @
2b5039f5
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
);
}
}
src/main/java/cn/blog/entity/User.java
View file @
2b5039f5
...
...
@@ -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
)
...
...
src/main/java/cn/blog/utils/JwtUtils.java
View file @
2b5039f5
...
...
@@ -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
...
...
src/main/java/cn/blog/utils/ShiroUtil.java
0 → 100644
View file @
2b5039f5
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
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment