Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
周海宝
/
auth-server-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 3daf8e3f
authored
Apr 20, 2023
by
刘宇峰
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
授权服务
1 parent
11795493
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
28 deletions
platform-auth-api/src/main/java/cn/com/glsx/auth/utils/GatewayJwtUtils.java
platform-auth/src/main/java/cn/com/glsx/auth/modules/controller/LoginController.java
platform-auth/src/main/resources/bootstrap.properties
platform-auth-api/src/main/java/cn/com/glsx/auth/utils/GatewayJwtUtils.java
View file @
3daf8e3
package
cn
.
com
.
glsx
.
auth
.
utils
;
package
cn
.
com
.
glsx
.
auth
.
utils
;
import
java.time.Instant
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.Map
;
import
java.util.Objects
;
import
java.util.Map.Entry
;
import
java.util.concurrent.TimeUnit
;
import
javax.annotation.Resource
;
import
cn.com.glsx.auth.model.GatewayService
;
import
cn.com.glsx.auth.model.GatewayService
;
import
com.auth0.jwt.JWT
;
import
com.auth0.jwt.JWT
;
import
com.auth0.jwt.JWTCreator
;
import
com.auth0.jwt.JWTCreator
;
...
@@ -20,10 +11,15 @@ import com.auth0.jwt.interfaces.DecodedJWT;
...
@@ -20,10 +11,15 @@ import com.auth0.jwt.interfaces.DecodedJWT;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
java.time.Instant
;
import
java.util.*
;
import
java.util.Map.Entry
;
import
java.util.concurrent.TimeUnit
;
@Component
@Component
public
class
GatewayJwtUtils
{
public
class
GatewayJwtUtils
{
...
@@ -82,15 +78,15 @@ public class GatewayJwtUtils {
...
@@ -82,15 +78,15 @@ public class GatewayJwtUtils {
Map
<
String
,
Object
>
newClaims
=
new
HashMap
();
Map
<
String
,
Object
>
newClaims
=
new
HashMap
();
Iterator
var9
=
claims
.
entrySet
().
iterator
();
Iterator
var9
=
claims
.
entrySet
().
iterator
();
while
(
var9
.
hasNext
())
{
while
(
var9
.
hasNext
())
{
Entry
<
String
,
Object
>
entry
=
(
Entry
)
var9
.
next
();
Entry
<
String
,
Object
>
entry
=
(
Entry
)
var9
.
next
();
String
key
=
(
String
)
entry
.
getKey
();
String
key
=
(
String
)
entry
.
getKey
();
Object
value
=
entry
.
getValue
();
Object
value
=
entry
.
getValue
();
if
(!
"class"
.
equals
(
key
)
&&
!
Objects
.
isNull
(
value
))
{
if
(!
"class"
.
equals
(
key
)
&&
!
Objects
.
isNull
(
value
))
{
newClaims
.
put
(
key
,
value
);
newClaims
.
put
(
key
,
value
);
}
}
}
}
JWTCreator
.
Builder
builder
=
JWT
.
create
().
withJWTId
(
gatewayService
.
getName
()).
withClaim
(
"iat"
,
Date
.
from
(
issueAt
.
plusSeconds
(
gatewayService
.
getJwtIssueAtOffset
()
==
null
?
0L
:
(
long
)(-
gatewayService
.
getJwtIssueAtOffset
())))).
withClaim
(
"exp"
,
Date
.
from
(
exp
)).
withClaim
(
"payloadMap"
,
newClaims
);
JWTCreator
.
Builder
builder
=
JWT
.
create
().
withJWTId
(
gatewayService
.
getName
()).
withClaim
(
"iat"
,
Date
.
from
(
issueAt
.
plusSeconds
(
gatewayService
.
getJwtIssueAtOffset
()
==
null
?
0L
:
(
long
)
(-
gatewayService
.
getJwtIssueAtOffset
())))).
withClaim
(
"exp"
,
Date
.
from
(
exp
)).
withClaim
(
"payloadMap"
,
newClaims
);
builder
.
withSubject
(
gatewayService
.
getName
()).
withClaim
(
"name"
,
gatewayService
.
getName
());
builder
.
withSubject
(
gatewayService
.
getName
()).
withClaim
(
"name"
,
gatewayService
.
getName
());
String
token
=
builder
.
sign
(
algorithm
);
String
token
=
builder
.
sign
(
algorithm
);
log
.
trace
(
"create token ["
+
token
+
"]; iat: "
+
Date
.
from
(
exp
)
+
" exp: "
+
Date
.
from
(
exp
));
log
.
trace
(
"create token ["
+
token
+
"]; iat: "
+
Date
.
from
(
exp
)
+
" exp: "
+
Date
.
from
(
exp
));
...
@@ -105,6 +101,9 @@ public class GatewayJwtUtils {
...
@@ -105,6 +101,9 @@ public class GatewayJwtUtils {
}
}
public
DecodedJWT
verify
(
String
token
,
GatewayService
gatewayService
)
{
public
DecodedJWT
verify
(
String
token
,
GatewayService
gatewayService
)
{
if
(
Objects
.
isNull
(
gatewayService
))
{
return
null
;
}
return
this
.
verify
(
gatewayService
.
getSecret
(),
token
);
return
this
.
verify
(
gatewayService
.
getSecret
(),
token
);
}
}
...
@@ -134,7 +133,7 @@ public class GatewayJwtUtils {
...
@@ -134,7 +133,7 @@ public class GatewayJwtUtils {
}
}
public
Class
getJwtUserClass
(
String
token
)
throws
ClassNotFoundException
{
public
Class
getJwtUserClass
(
String
token
)
throws
ClassNotFoundException
{
String
jwtUserClazz
=
(
String
)
this
.
parseClaim
(
token
).
get
(
"clazz"
);
String
jwtUserClazz
=
(
String
)
this
.
parseClaim
(
token
).
get
(
"clazz"
);
log
.
debug
(
"token jwt user clazz ["
+
jwtUserClazz
+
"]"
);
log
.
debug
(
"token jwt user clazz ["
+
jwtUserClazz
+
"]"
);
Class
clazz
=
Class
.
forName
(
jwtUserClazz
);
Class
clazz
=
Class
.
forName
(
jwtUserClazz
);
return
clazz
;
return
clazz
;
...
@@ -165,7 +164,7 @@ public class GatewayJwtUtils {
...
@@ -165,7 +164,7 @@ public class GatewayJwtUtils {
try
{
try
{
DecodedJWT
decodedJWT
=
this
.
verify
(
token
,
gatewayService
);
DecodedJWT
decodedJWT
=
this
.
verify
(
token
,
gatewayService
);
String
redisToken
=
(
String
)
this
.
stringRedisTemplate
.
opsForValue
().
get
(
decodedJWT
.
getId
());
String
redisToken
=
(
String
)
this
.
stringRedisTemplate
.
opsForValue
().
get
(
decodedJWT
.
getId
());
if
(!
token
.
equals
(
redisToken
))
{
if
(!
token
.
equals
(
redisToken
))
{
return
false
;
return
false
;
}
}
...
...
platform-auth/src/main/java/cn/com/glsx/auth/modules/controller/LoginController.java
View file @
3daf8e3
...
@@ -8,7 +8,10 @@ import io.swagger.v3.oas.annotations.Operation;
...
@@ -8,7 +8,10 @@ import io.swagger.v3.oas.annotations.Operation;
import
io.swagger.v3.oas.annotations.tags.Tag
;
import
io.swagger.v3.oas.annotations.tags.Tag
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.validation.annotation.Validated
;
import
org.springframework.validation.annotation.Validated
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -16,7 +19,6 @@ import java.util.Map;
...
@@ -16,7 +19,6 @@ import java.util.Map;
@Slf4j
@Slf4j
@Tag
(
name
=
"登录接口"
)
@Tag
(
name
=
"登录接口"
)
@RestController
@RestController
@RequestMapping
(
value
=
"/login"
)
public
class
LoginController
{
public
class
LoginController
{
@Operation
(
summary
=
"系统登录"
,
description
=
"通过账号密码或手机验证码方式登录系统"
)
@Operation
(
summary
=
"系统登录"
,
description
=
"通过账号密码或手机验证码方式登录系统"
)
...
...
platform-auth/src/main/resources/bootstrap.properties
View file @
3daf8e3
...
@@ -7,17 +7,9 @@ logging.path=/data/java_log/jetty/${spring.application.name}/${spring.applicatio
...
@@ -7,17 +7,9 @@ logging.path=/data/java_log/jetty/${spring.application.name}/${spring.applicatio
#nacos配置中心
#nacos配置中心
spring.cloud.nacos.config.server-
addr
=
192.168.0.63:8868
spring.cloud.nacos.config.server-
addr
=
192.168.0.63:8868
#nacos配置id,完整dataId=${spring.cloud.nacos.config.prefix}-${spring.profiles.active}.${file-extension(由新增配置时选的文件类型决定)}
#示例:http://192.168.0.63:8848/nacos/v1/cs/configs?dataId=glsx-rest-admin.properties&group=DEV_GROUP
#配置分组(改成项目相关的)
spring.cloud.nacos.config.group
=
PLATFORM_GROUP
spring.cloud.nacos.config.group
=
PLATFORM_GROUP
#配置prefix,默认值同${spring.application.name}
spring.cloud.nacos.config.prefix
=
${spring.application.name}
spring.cloud.nacos.config.prefix
=
${spring.application.name}
#配置环境
spring.profiles.active
=
dev
spring.profiles.active
=
dev
#配置格式
spring.cloud.nacos.config.file-
extension
=
properties
spring.cloud.nacos.config.file-
extension
=
properties
#微服务sentinel、zipkin、actuator等公共配置
#微服务sentinel、zipkin、actuator等公共配置
...
@@ -33,5 +25,4 @@ spring.cloud.nacos.config.shared-configs[2].data-id=plateform-auth-common-dev.pr
...
@@ -33,5 +25,4 @@ spring.cloud.nacos.config.shared-configs[2].data-id=plateform-auth-common-dev.pr
spring.cloud.nacos.config.shared-configs[2]
.group
=
${spring.cloud.nacos.config.group}
spring.cloud.nacos.config.shared-configs[2]
.group
=
${spring.cloud.nacos.config.group}
spring.cloud.nacos.config.shared-configs[2]
.refresh
=
true
spring.cloud.nacos.config.shared-configs[2]
.refresh
=
true
spring.main.allow-circular-
references
=
true
spring.main.allow-circular-
references
=
true
spring.mvc.pathmatch.matching-
strategy
=
ant_path_matcher
\ No newline at end of file
\ No newline at end of file
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