Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
yangxiujun
/
paidan_demo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 0ae1e257
authored
Jul 12, 2023
by
chamberone
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 加入全局鉴权,传递用户id参数
1 parent
b0096c34
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
31 additions
and
11 deletions
project-gateway/src/main/java/com/dituhui/pea/gateway/config/AuthFilter.java
project-interface/src/main/java/com/dituhui/pea/pojo/UserLoginDTO.java
project-interface/src/main/java/com/dituhui/pea/user/IUser.java
project-user/src/main/java/com/dituhui/pea/user/controller/UserController.java
project-user/src/main/java/com/dituhui/pea/user/service/UserService.java
project-gateway/src/main/java/com/dituhui/pea/gateway/config/AuthFilter.java
View file @
0ae1e25
package
com
.
dituhui
.
pea
.
gateway
.
config
;
import
java.util.Set
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cloud.gateway.filter.GatewayFilterChain
;
import
org.springframework.cloud.gateway.filter.GlobalFilter
;
import
org.springframework.core.Ordered
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.server.reactive.ServerHttpRequest
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.server.ServerWebExchange
;
...
...
@@ -13,6 +16,7 @@ import com.dituhui.pea.common.Result;
import
com.dituhui.pea.common.ResultEnum
;
import
com.dituhui.pea.pojo.UserLoginDTO
;
import
com.dituhui.pea.user.IUser
;
import
com.google.common.collect.Sets
;
import
lombok.extern.slf4j.Slf4j
;
import
reactor.core.publisher.Mono
;
...
...
@@ -21,12 +25,21 @@ import reactor.core.publisher.Mono;
@Slf4j
public
class
AuthFilter
implements
GlobalFilter
,
Ordered
{
/**
* 白名单
*/
private
static
final
Set
<
String
>
authWhiteList
=
Sets
.
newHashSet
(
"/pea-user/login"
,
"/pea-user/login/"
);
@Autowired
IUser
userService
;
@Override
public
Mono
<
Void
>
filter
(
ServerWebExchange
exchange
,
GatewayFilterChain
chain
)
{
String
url
=
exchange
.
getRequest
().
getPath
().
toString
();
// 判断是否属于白名单中
if
(
authWhiteList
.
contains
(
url
))
{
return
chain
.
filter
(
exchange
);
}
// 下面的代码从Http Header的Authorization中获取token,也可以从其他header,cookie等中获取,看客户端怎么传递token
HttpHeaders
headers
=
exchange
.
getRequest
().
getHeaders
();
...
...
@@ -48,15 +61,21 @@ public class AuthFilter implements GlobalFilter, Ordered {
UserLoginDTO
userDTO
=
null
;
if
(
StringUtils
.
isNotEmpty
(
authToken
))
{
// 查询token对应的用户
//
Result<?> userResult = userService.getUserInfo(authToken);
//
if (ResultEnum.SUCCESS.getCode().equals(userResult.getCode())) {
//
userDTO = (UserLoginDTO) userResult.getResult();
//
}
Result
<?>
userResult
=
userService
.
getUserInfo
(
authToken
);
if
(
ResultEnum
.
SUCCESS
.
getCode
().
equals
(
userResult
.
getCode
()))
{
userDTO
=
(
UserLoginDTO
)
userResult
.
getResult
();
}
}
if
(
userDTO
==
null
)
{
log
.
info
(
"未授权访问{} ip:{}"
,
url
,
getRemoteIP
(
exchange
));
}
else
{
log
.
info
(
"用户{}访问{}"
,
userDTO
.
getAccount
(),
url
);
log
.
info
(
"用户:{} id:{} 访问{}"
,
userDTO
.
getAccount
(),
userDTO
.
getId
(),
url
);
// 获取当前的请求对象信息
ServerHttpRequest
.
Builder
builder
=
exchange
.
getRequest
().
mutate
();
// 向header中设置新的key,存储解析好的token对应基本信息
builder
.
header
(
"userId"
,
userDTO
.
getId
());
// 向下游传递
return
chain
.
filter
(
exchange
.
mutate
().
request
(
builder
.
build
()).
build
());
}
return
chain
.
filter
(
exchange
);
...
...
project-interface/src/main/java/com/dituhui/pea/pojo/UserLoginDTO.java
View file @
0ae1e25
...
...
@@ -9,6 +9,7 @@ import lombok.Data;
@Data
public
class
UserLoginDTO
{
private
String
id
;
private
String
account
;
@JsonIgnore
private
String
password
;
...
...
project-interface/src/main/java/com/dituhui/pea/user/IUser.java
View file @
0ae1e25
...
...
@@ -2,6 +2,7 @@ package com.dituhui.pea.user;
import
org.springframework.cloud.openfeign.FeignClient
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestHeader
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
...
...
@@ -24,7 +25,7 @@ public interface IUser {
public
Result
<?>
userLogin
(
@RequestBody
UserLoginParam
user
);
@RequestMapping
(
value
=
"/pea-user/userInfo"
,
method
=
RequestMethod
.
GET
)
public
Result
<?>
getUserInfo
(
@Request
Param
(
name
=
"token"
)
String
token
);
public
Result
<?>
getUserInfo
(
@Request
Header
(
name
=
"userId"
,
required
=
true
)
String
userId
);
/**
* 获取当前登陆用户信息
...
...
project-user/src/main/java/com/dituhui/pea/user/controller/UserController.java
View file @
0ae1e25
...
...
@@ -40,8 +40,8 @@ public class UserController implements IUser {
}
@Override
public
Result
<?>
getUserInfo
(
String
token
)
{
return
userService
.
getUserInfo
(
token
);
public
Result
<?>
getUserInfo
(
String
userId
)
{
return
userService
.
getUserInfo
(
userId
);
}
@Override
...
...
project-user/src/main/java/com/dituhui/pea/user/service/UserService.java
View file @
0ae1e25
...
...
@@ -148,10 +148,9 @@ public class UserService {
resourceInfo
.
setExtra
(
null
);
}
public
Result
<?>
getUserInfo
(
String
token
)
{
UserEntity
user
=
(
UserEntity
)
redisService
.
get
(
RedisKeyGroup
.
authToken
+
":"
+
token
);
public
Result
<?>
getUserInfo
(
String
userId
)
{
UserEntity
user
=
userDao
.
getById
(
userId
);
UserLoginDTO
userDTO
=
convertToUserLoginDTO
(
user
);
userDTO
.
setToken
(
token
);
return
Result
.
success
(
userDTO
);
}
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment