Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
trismegisto-services
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mauro Paolo Josue Zuñiga Mallqui
trismegisto-services
Commits
38f3ad48
Commit
38f3ad48
authored
Sep 10, 2024
by
Mauro Paolo Josue Zuñiga Mallqui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[EDIT] MEJORA DE TOKEN
parent
5c563726
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
33 deletions
+32
-33
JWTokenApi.java
...ava/web/multitask/trismegistoservices/api/JWTokenApi.java
+12
-9
JWTokenUtil.java
.../web/multitask/trismegistoservices/utils/JWTokenUtil.java
+20
-24
No files found.
src/main/java/web/multitask/trismegistoservices/api/JWTokenApi.java
View file @
38f3ad48
package
web
.
multitask
.
trismegistoservices
.
api
;
import
org.json.JSONObject
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.security.core.userdetails.UserDetails
;
...
...
@@ -9,15 +8,13 @@ import web.multitask.trismegistoservices.model.User;
import
web.multitask.trismegistoservices.repository.UserRepository
;
import
web.multitask.trismegistoservices.singleton.TokenSingleton
;
import
web.multitask.trismegistoservices.utils.JWTokenUtil
;
import
java.math.BigInteger
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.Objects
;
import
java.util.concurrent.atomic.AtomicBoolean
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
@RestController
@RequestMapping
(
"/token"
)
@CrossOrigin
...
...
@@ -124,8 +121,14 @@ class JWTokenApi {
public
ResponseEntity
<?>
remainingTime
(
@RequestBody
String
token
)
{
JSONObject
json
=
new
JSONObject
(
token
);
try
{
int
remaining
=
jwtTokenUtil
.
getExperyTime
(
json
.
getString
(
"token"
));
return
ResponseEntity
.
ok
(
new
JSONObject
().
put
(
"remaining"
,
remaining
).
put
(
"message"
,
"OK"
).
put
(
"status"
,
true
).
toMap
());
Long
remaining
=
jwtTokenUtil
.
getExperyTime
(
json
.
getString
(
"token"
));
Date
expirationDate
=
new
Date
(
System
.
currentTimeMillis
()
+
remaining
);
SimpleDateFormat
dateFormat
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
return
ResponseEntity
.
ok
(
new
JSONObject
()
.
put
(
"remaining"
,
remaining
)
.
put
(
"message"
,
"OK"
)
.
put
(
"expiration"
,
dateFormat
.
format
(
expirationDate
))
.
put
(
"status"
,
true
).
toMap
());
}
catch
(
Exception
e
)
{
return
ResponseEntity
.
status
(
401
).
body
(
new
JSONObject
().
put
(
"remaining"
,
0
).
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
).
toMap
());
}
...
...
@@ -136,8 +139,8 @@ class JWTokenApi {
JSONObject
json
=
new
JSONObject
(
data
);
try
{
boolean
onelife
=
json
.
optBoolean
(
"onelife"
,
false
);
int
ms
=
json
.
optInt
(
"ms"
,
3600000
);
String
tokenized
=
jwtTokenUtil
.
tokenizeData
(
data
,
BigInteger
.
valueOf
(
ms
)
,
onelife
);
BigInteger
ms
=
json
.
optBigInteger
(
"ms"
,
BigInteger
.
valueOf
(
3600000
)
);
String
tokenized
=
jwtTokenUtil
.
tokenizeData
(
data
,
ms
,
onelife
);
if
(
onelife
){
tokenSingleton
.
addToken
(
tokenized
);
}
...
...
src/main/java/web/multitask/trismegistoservices/utils/JWTokenUtil.java
View file @
38f3ad48
...
...
@@ -25,26 +25,28 @@ public class JWTokenUtil implements Serializable{
TokenSingleton
tokenSingleton
=
new
TokenSingleton
();
public
String
generateToken
(
User
user
,
BigInteger
ms
,
boolean
onelife
)
{
JSONObject
json
=
new
JSONObject
();
json
.
put
(
"username"
,
user
.
getUsername
());
if
(
ms
==
null
){
ms
=
BigInteger
.
valueOf
(
3600000
);
}
Date
now
=
new
Date
();
Date
expiryDate
=
new
Date
(
now
.
getTime
()
+
ms
.
longValue
());
JSONObject
json
=
new
JSONObject
();
json
.
put
(
"username"
,
user
.
getUsername
());
Date
expirationDate
=
new
Date
(
System
.
currentTimeMillis
()
+
ms
.
longValue
());
return
Jwts
.
builder
()
.
setSubject
(
json
.
toString
())
.
setIssuedAt
(
new
Date
())
.
setExpiration
(
expir
y
Date
)
.
setExpiration
(
expir
ation
Date
)
.
signWith
(
Keys
.
hmacShaKeyFor
(
onelife
?
jwtSecret2
.
getBytes
()
:
jwtSecret
.
getBytes
()))
.
compact
();
}
public
String
tokenizeData
(
String
data
,
BigInteger
ms
,
boolean
onelife
){
Date
expirationDate
=
new
Date
(
System
.
currentTimeMillis
()
+
ms
.
longValue
());
return
Jwts
.
builder
()
.
setSubject
(
data
)
.
setIssuedAt
(
new
Date
())
.
setExpiration
(
new
Date
(
new
Date
().
getTime
()
+
ms
.
longValue
())
)
.
setExpiration
(
expirationDate
)
.
signWith
(
Keys
.
hmacShaKeyFor
(
onelife
?
jwtSecret2
.
getBytes
()
:
jwtSecret
.
getBytes
()))
.
compact
();
}
...
...
@@ -133,8 +135,7 @@ public class JWTokenUtil implements Serializable{
.
build
()
.
parseClaimsJws
(
token
)
.
getBody
()
.
getExpiration
()
.
before
(
new
Date
());
.
getExpiration
();
return
false
;
}
catch
(
Exception
e
)
{
try
{
...
...
@@ -143,8 +144,7 @@ public class JWTokenUtil implements Serializable{
.
build
()
.
parseClaimsJws
(
token
)
.
getBody
()
.
getExpiration
()
.
before
(
new
Date
());
.
getExpiration
();
return
false
;
}
catch
(
Exception
e2
)
{
System
.
out
.
println
(
e2
.
getMessage
());
...
...
@@ -153,9 +153,9 @@ public class JWTokenUtil implements Serializable{
}
}
public
int
getExperyTime
(
String
token
){
public
Long
getExperyTime
(
String
token
){
try
{
return
(
int
)
((
Jwts
.
parserBuilder
()
return
((
Jwts
.
parserBuilder
()
.
setSigningKey
(
Keys
.
hmacShaKeyFor
(
jwtSecret
.
getBytes
()))
.
build
()
.
parseClaimsJws
(
token
)
...
...
@@ -163,18 +163,13 @@ public class JWTokenUtil implements Serializable{
.
getExpiration
()
.
getTime
()
-
new
Date
().
getTime
()));
}
catch
(
Exception
e
){
try
{
return
(
int
)
((
Jwts
.
parserBuilder
()
.
setSigningKey
(
Keys
.
hmacShaKeyFor
(
jwtSecret2
.
getBytes
()))
.
build
()
.
parseClaimsJws
(
token
)
.
getBody
()
.
getExpiration
()
.
getTime
()
-
new
Date
().
getTime
()));
}
catch
(
Exception
e2
){
System
.
out
.
println
(
e2
.
getMessage
());
return
0
;
}
return
((
Jwts
.
parserBuilder
()
.
setSigningKey
(
Keys
.
hmacShaKeyFor
(
jwtSecret2
.
getBytes
()))
.
build
()
.
parseClaimsJws
(
token
)
.
getBody
()
.
getExpiration
()
.
getTime
()
-
new
Date
().
getTime
()));
}
}
}
\ No newline at end of file
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