Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
polysoft-nafmii
/
polysoft-parent
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 191f6256
authored
Apr 25, 2021
by
zhoujinghao
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
网关加入相关过滤器
1 parent
1d78b37e
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
314 additions
and
70 deletions
nafmii-app/pom.xml
nafmii-app/src/main/java/org/nafmii/service/impl/UserServiceImpl.java
nafmii-common/pom.xml
nafmii-common/src/main/java/org/nafmii/exception/JwtException.java
nafmii-app/src/main/java/org/nafmii/auth/JwtUtils.java → nafmii-common/src/main/java/org/nafmii/utils/JwtUtils.java
nafmii-common/src/main/java/org/nafmii/vo/AppUser.java
nafmii-gateway/pom.xml
nafmii-gateway/src/main/java/org/nafmii/config/AccessFilter.java
pom.xml
nafmii-app/pom.xml
View file @
191f625
...
...
@@ -86,22 +86,6 @@
<!--<artifactId>ojdbc14</artifactId>-->
<!--</dependency>-->
<!--JWT(Json Web Token)登录支持-->
<dependency>
<groupId>
io.jsonwebtoken
</groupId>
<artifactId>
jjwt
</artifactId>
<version>
0.7.0
</version>
</dependency>
<dependency>
<groupId>
com.auth0
</groupId>
<artifactId>
java-jwt
</artifactId>
<version>
3.2.0
</version>
<exclusions>
<exclusion>
<artifactId>
bcprov-jdk15on
</artifactId>
<groupId>
org.bouncycastle
</groupId>
</exclusion>
</exclusions>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.security.oauth.boot</groupId>-->
<!--<artifactId>spring-security-oauth2-autoconfigure</artifactId>-->
...
...
nafmii-app/src/main/java/org/nafmii/service/impl/UserServiceImpl.java
View file @
191f625
package
org
.
nafmii
.
service
.
impl
;
import
org.nafmii.auth.JwtUtils
;
import
org.nafmii.result.Result
;
import
org.nafmii.service.UserService
;
import
org.nafmii.utils.JwtUtils
;
import
org.nafmii.vo.UserAndRoleVo
;
import
org.nafmii.vo.UserVO
;
import
org.springframework.stereotype.Service
;
...
...
nafmii-common/pom.xml
View file @
191f625
...
...
@@ -53,6 +53,22 @@
<artifactId>
spring-boot-starter-data-redis
</artifactId>
</dependency>
<dependency>
<groupId>
io.jsonwebtoken
</groupId>
<artifactId>
jjwt
</artifactId>
<version>
0.7.0
</version>
</dependency>
<dependency>
<groupId>
com.auth0
</groupId>
<artifactId>
java-jwt
</artifactId>
<version>
3.2.0
</version>
<exclusions>
<exclusion>
<artifactId>
bcprov-jdk15on
</artifactId>
<groupId>
org.bouncycastle
</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>
org.springframework.data
</groupId>
<artifactId>
spring-data-commons
</artifactId>
</dependency>
...
...
nafmii-common/src/main/java/org/nafmii/exception/JwtException.java
deleted
100644 → 0
View file @
1d78b37
package
org
.
nafmii
.
exception
;
public
class
JwtException
extends
RuntimeException
{
public
JwtException
()
{
super
();
}
public
JwtException
(
String
message
)
{
super
(
message
);
}
public
JwtException
(
String
message
,
Throwable
cause
)
{
super
(
message
,
cause
);
}
public
JwtException
(
Throwable
cause
)
{
super
(
cause
);
}
protected
JwtException
(
String
message
,
Throwable
cause
,
boolean
enableSuppression
,
boolean
writableStackTrace
)
{
super
(
message
,
cause
,
enableSuppression
,
writableStackTrace
);
}
}
nafmii-
app/src/main/java/org/nafmii/auth
/JwtUtils.java
→
nafmii-
common/src/main/java/org/nafmii/utils
/JwtUtils.java
View file @
191f625
package
org
.
nafmii
.
auth
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.auth0.jwt.JWT
;
import
com.auth0.jwt.JWTCreator.Builder
;
import
com.auth0.jwt.JWTVerifier
;
import
com.auth0.jwt.algorithms.Algorithm
;
import
com.auth0.jwt.exceptions.JWTVerificationException
;
import
com.auth0.jwt.impl.JWTParser
;
import
com.auth0.jwt.interfaces.Claim
;
import
com.auth0.jwt.interfaces.DecodedJWT
;
import
com.auth0.jwt.interfaces.Header
;
import
com.auth0.jwt.interfaces.Payload
;
package
org
.
nafmii
.
utils
;
import
io.jsonwebtoken.Claims
;
import
io.jsonwebtoken.Jwts
;
import
io.jsonwebtoken.SignatureAlgorithm
;
import
org.apache.commons.codec.binary.Base64
;
import
org.apache.commons.codec.binary.StringUtils
;
import
org.nafmii.entity.AppUser
;
import
org.nafmii.exception.JwtException
;
import
org.nafmii.utils.DateUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.UnsupportedEncodingException
;
import
java.sql.Timestamp
;
import
java.text.SimpleDateFormat
;
import
org.nafmii.vo.AppUser
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
nafmii-common/src/main/java/org/nafmii/vo/AppUser.java
0 → 100644
View file @
191f625
package
org
.
nafmii
.
vo
;
import
io.swagger.annotations.ApiModelProperty
;
import
java.io.Serializable
;
import
java.util.Date
;
public
class
AppUser
implements
Serializable
{
private
Long
id
;
@ApiModelProperty
(
value
=
"昵称"
)
private
String
userName
;
@ApiModelProperty
(
value
=
"登录密码"
)
private
String
loginPwd
;
@ApiModelProperty
(
value
=
"中文姓名"
)
private
String
realName
;
@ApiModelProperty
(
value
=
"手机号/登录账号"
)
private
String
phone
;
@ApiModelProperty
(
value
=
"出生日期"
)
private
String
birthday
;
@ApiModelProperty
(
value
=
"邮箱"
)
private
String
email
;
@ApiModelProperty
(
value
=
"头像图片地址"
)
private
String
headPicture
;
@ApiModelProperty
(
value
=
"国籍"
)
private
String
nationality
;
@ApiModelProperty
(
value
=
"单位全称"
)
private
String
unitName
;
@ApiModelProperty
(
value
=
"微信OPENT_ID"
)
private
String
openId
;
@ApiModelProperty
(
value
=
"是否开启指纹登录 0 未开启 1 已开启"
)
private
String
figerprintFalg
;
@ApiModelProperty
(
value
=
"是否开启人脸识别登录 0 未开启 1 已开启"
)
private
String
faceidFalg
;
@ApiModelProperty
(
value
=
"创建时间"
)
private
Date
createTime
;
@ApiModelProperty
(
value
=
"更新时间"
)
private
Date
updateTime
;
@ApiModelProperty
(
value
=
"是否有效标识 0 有效用户 1无效用户"
)
private
String
isEnable
;
private
static
final
long
serialVersionUID
=
1L
;
public
Long
getId
()
{
return
id
;
}
public
void
setId
(
Long
id
)
{
this
.
id
=
id
;
}
public
String
getUserName
()
{
return
userName
;
}
public
void
setUserName
(
String
userName
)
{
this
.
userName
=
userName
;
}
public
String
getLoginPwd
()
{
return
loginPwd
;
}
public
void
setLoginPwd
(
String
loginPwd
)
{
this
.
loginPwd
=
loginPwd
;
}
public
String
getRealName
()
{
return
realName
;
}
public
void
setRealName
(
String
realName
)
{
this
.
realName
=
realName
;
}
public
String
getPhone
()
{
return
phone
;
}
public
void
setPhone
(
String
phone
)
{
this
.
phone
=
phone
;
}
public
String
getBirthday
()
{
return
birthday
;
}
public
void
setBirthday
(
String
birthday
)
{
this
.
birthday
=
birthday
;
}
public
String
getEmail
()
{
return
email
;
}
public
void
setEmail
(
String
email
)
{
this
.
email
=
email
;
}
public
String
getHeadPicture
()
{
return
headPicture
;
}
public
void
setHeadPicture
(
String
headPicture
)
{
this
.
headPicture
=
headPicture
;
}
public
String
getNationality
()
{
return
nationality
;
}
public
void
setNationality
(
String
nationality
)
{
this
.
nationality
=
nationality
;
}
public
String
getUnitName
()
{
return
unitName
;
}
public
void
setUnitName
(
String
unitName
)
{
this
.
unitName
=
unitName
;
}
public
String
getOpenId
()
{
return
openId
;
}
public
void
setOpenId
(
String
openId
)
{
this
.
openId
=
openId
;
}
public
String
getFigerprintFalg
()
{
return
figerprintFalg
;
}
public
void
setFigerprintFalg
(
String
figerprintFalg
)
{
this
.
figerprintFalg
=
figerprintFalg
;
}
public
String
getFaceidFalg
()
{
return
faceidFalg
;
}
public
void
setFaceidFalg
(
String
faceidFalg
)
{
this
.
faceidFalg
=
faceidFalg
;
}
public
Date
getCreateTime
()
{
return
createTime
;
}
public
void
setCreateTime
(
Date
createTime
)
{
this
.
createTime
=
createTime
;
}
public
Date
getUpdateTime
()
{
return
updateTime
;
}
public
void
setUpdateTime
(
Date
updateTime
)
{
this
.
updateTime
=
updateTime
;
}
public
String
getIsEnable
()
{
return
isEnable
;
}
public
void
setIsEnable
(
String
isEnable
)
{
this
.
isEnable
=
isEnable
;
}
@Override
public
String
toString
()
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
getClass
().
getSimpleName
());
sb
.
append
(
" ["
);
sb
.
append
(
"Hash = "
).
append
(
hashCode
());
sb
.
append
(
", id="
).
append
(
id
);
sb
.
append
(
", userName="
).
append
(
userName
);
sb
.
append
(
", loginPwd="
).
append
(
loginPwd
);
sb
.
append
(
", realName="
).
append
(
realName
);
sb
.
append
(
", phone="
).
append
(
phone
);
sb
.
append
(
", birthday="
).
append
(
birthday
);
sb
.
append
(
", email="
).
append
(
email
);
sb
.
append
(
", headPicture="
).
append
(
headPicture
);
sb
.
append
(
", nationality="
).
append
(
nationality
);
sb
.
append
(
", unitName="
).
append
(
unitName
);
sb
.
append
(
", openId="
).
append
(
openId
);
sb
.
append
(
", figerprintFalg="
).
append
(
figerprintFalg
);
sb
.
append
(
", faceidFalg="
).
append
(
faceidFalg
);
sb
.
append
(
", createTime="
).
append
(
createTime
);
sb
.
append
(
", updateTime="
).
append
(
updateTime
);
sb
.
append
(
", isEnable="
).
append
(
isEnable
);
sb
.
append
(
", serialVersionUID="
).
append
(
serialVersionUID
);
sb
.
append
(
"]"
);
return
sb
.
toString
();
}
}
\ No newline at end of file
nafmii-gateway/pom.xml
View file @
191f625
...
...
@@ -24,6 +24,10 @@
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
</dependency>
<dependency>
<groupId>
org.nafmii
</groupId>
<artifactId>
nafmii-common
</artifactId>
</dependency>
</dependencies>
<build>
...
...
nafmii-gateway/src/main/java/org/nafmii/config/AccessFilter.java
0 → 100644
View file @
191f625
package
org
.
nafmii
.
config
;
import
cn.hutool.core.date.DateTime
;
import
cn.hutool.log.Log
;
import
io.jsonwebtoken.Claims
;
import
io.micrometer.core.instrument.util.StringUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.nafmii.exception.BusinessException
;
import
org.nafmii.utils.JwtUtils
;
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.HttpStatus
;
import
org.springframework.http.server.reactive.ServerHttpResponse
;
import
org.springframework.util.AntPathMatcher
;
import
org.springframework.web.server.ServerWebExchange
;
import
reactor.core.publisher.Mono
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Set
;
/**
* Created by zhangzhenfang on 2018/4/17.
*/
@Slf4j
public
class
AccessFilter
implements
GlobalFilter
,
Ordered
{
@Autowired
private
AntPathMatcher
antPathMatcher
;
//路径匹配器
@Override
public
Mono
<
Void
>
filter
(
ServerWebExchange
exchange
,
GatewayFilterChain
chain
)
{
String
url
=
exchange
.
getRequest
().
getURI
().
getPath
();
List
<
String
>
skipAuthUrls
=
new
ArrayList
<>();
skipAuthUrls
.
add
(
"111111"
);
//跳过不需要验证的url
for
(
String
skip
:
skipAuthUrls
)
{
if
(
antPathMatcher
.
match
(
skip
,
url
)){
return
chain
.
filter
(
exchange
);
}
}
//获取token
String
token
=
exchange
.
getRequest
().
getHeaders
().
getFirst
(
JwtUtils
.
TOKEN_HEADER
);
if
(
StringUtils
.
isBlank
(
token
)){
//没有token
throw
new
BusinessException
(
"请登录"
);
}
else
{
try
{
//解析token
boolean
expiration
=
JwtUtils
.
isExpiration
(
token
);
if
(!
expiration
){
//签名验证通过
return
chain
.
filter
(
exchange
);
}
else
{
throw
new
BusinessException
(
"认证无效"
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
"检查token时异常: "
+
e
);
if
(
e
.
getMessage
().
contains
(
"JWT expired"
)){
throw
new
BusinessException
(
"认证过期"
);
}
else
{
throw
new
BusinessException
(
"认证失败"
);
}
}
}
}
@Override
public
int
getOrder
()
{
return
-
999
;
}
}
pom.xml
View file @
191f625
...
...
@@ -161,11 +161,6 @@
<version>
${ojdbc.version}
</version>
</dependency>
<!--JWT(Json Web Token)登录支持-->
<dependency>
<groupId>
io.jsonwebtoken
</groupId>
<artifactId>
jjwt
</artifactId>
<version>
${jjwt.version}
</version>
</dependency>
<!--集成logstash-->
<dependency>
...
...
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