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 3074b4d1
authored
Jul 12, 2023
by
chamberone
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 网关添加用户鉴权过滤器
1 parent
231ebc44
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
92 additions
and
2 deletions
config-init/config/project-gateway.yaml
project-gateway/src/main/java/com/dituhui/pea/gateway/config/AuthFilter.java
project-user/src/main/java/com/dituhui/pea/user/controller/UserController.java
config-init/config/project-gateway.yaml
View file @
3074b4d
...
...
@@ -24,5 +24,5 @@ spring:
-
Path=/dispatch/**
auth
:
path
:
/test/**,/v1/**,/gis/**
# 需要认证的路边列表,多个用逗号连接
path
:
/
pea-user/**,/
test/**,/v1/**,/gis/**
# 需要认证的路边列表,多个用逗号连接
project-gateway/src/main/java/com/dituhui/pea/gateway/config/AuthFilter.java
0 → 100644
View file @
3074b4d
package
com
.
dituhui
.
pea
.
gateway
.
config
;
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.stereotype.Component
;
import
org.springframework.web.server.ServerWebExchange
;
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
lombok.extern.slf4j.Slf4j
;
import
reactor.core.publisher.Mono
;
@Component
@Slf4j
public
class
AuthFilter
implements
GlobalFilter
,
Ordered
{
@Autowired
IUser
userService
;
@Override
public
Mono
<
Void
>
filter
(
ServerWebExchange
exchange
,
GatewayFilterChain
chain
)
{
String
url
=
exchange
.
getRequest
().
getPath
().
toString
();
// 下面的代码从Http Header的Authorization中获取token,也可以从其他header,cookie等中获取,看客户端怎么传递token
HttpHeaders
headers
=
exchange
.
getRequest
().
getHeaders
();
String
requestHeader
=
headers
.
getFirst
(
"Authorization"
);
// W3C的HTTP1.0规范: Authorization : <type> <authorization-parameters>
// Basic用于http-basic 认证;
// Bearer 常见于OAuth和JWT授权;
// AwS4-HMAC - SHA256 AwS授权
String
authToken
=
null
;
if
(
requestHeader
!=
null
&&
requestHeader
.
startsWith
(
"Bearer "
))
{
authToken
=
requestHeader
.
substring
(
7
);
}
if
(
StringUtils
.
isEmpty
(
authToken
))
{
authToken
=
exchange
.
getRequest
().
getQueryParams
().
getFirst
(
"token"
);
}
if
(
log
.
isTraceEnabled
())
{
log
.
trace
(
"token is {}"
,
authToken
);
}
UserLoginDTO
userDTO
=
null
;
if
(
StringUtils
.
isNotEmpty
(
authToken
))
{
// 查询token对应的用户
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
);
}
return
chain
.
filter
(
exchange
);
}
/**
* 客户端ip
*
* @param exchange
* @return
*/
private
String
getRemoteIP
(
ServerWebExchange
exchange
)
{
String
clientIp
=
""
;
if
(
StringUtils
.
isNotEmpty
(
exchange
.
getRequest
().
getHeaders
().
getFirst
(
"x-forwarded-for"
)))
{
clientIp
=
exchange
.
getRequest
().
getHeaders
().
getFirst
(
"x-forwarded-for"
);
}
else
if
(
StringUtils
.
isNotEmpty
(
exchange
.
getRequest
().
getHeaders
().
getFirst
(
"X-Forwarded-For"
)))
{
clientIp
=
exchange
.
getRequest
().
getHeaders
().
getFirst
(
"X-Forwarded-For"
);
}
else
if
(
StringUtils
.
isNotEmpty
(
exchange
.
getRequest
().
getRemoteAddress
().
getAddress
().
getHostAddress
()))
{
clientIp
=
exchange
.
getRequest
().
getRemoteAddress
().
getAddress
().
getHostAddress
();
}
if
(
"0:0:0:0:0:0:0:1"
.
equals
(
clientIp
))
{
clientIp
=
"127.0.0.1"
;
}
return
clientIp
;
}
@Override
public
int
getOrder
()
{
return
-
1
;
}
}
project-user/src/main/java/com/dituhui/pea/user/controller/UserController.java
View file @
3074b4d
...
...
@@ -36,7 +36,6 @@ public class UserController implements IUser {
@Override
public
Result
<?>
userLogin
(
UserLoginParam
user
)
{
System
.
out
.
println
(
user
.
toString
());
return
userService
.
userLogin
(
user
.
getAccount
(),
user
.
getPassword
());
}
...
...
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