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
6f5b4963
Commit
6f5b4963
authored
Dec 29, 2023
by
Mauro Paolo Josue Zuñiga Mallqui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[ADD] FILES ADDED 29122023
parent
1cf76e1d
Show whitespace changes
Inline
Side-by-side
Showing
22 changed files
with
593 additions
and
115 deletions
+593
-115
pom.xml
pom.xml
+33
-1
AppApi.java
src/main/java/web/multitask/app/api/AppApi.java
+8
-6
EmailApi.java
src/main/java/web/multitask/app/api/EmailApi.java
+41
-10
JWTApi.java
src/main/java/web/multitask/app/api/JWTApi.java
+0
-52
JWTokenApi.java
src/main/java/web/multitask/app/api/JWTokenApi.java
+93
-0
PDFApi.java
src/main/java/web/multitask/app/api/PDFApi.java
+40
-0
UtilsApi.java
src/main/java/web/multitask/app/api/UtilsApi.java
+65
-0
FilterConfig.java
src/main/java/web/multitask/app/config/FilterConfig.java
+4
-4
MVCConfig.java
src/main/java/web/multitask/app/config/MVCConfig.java
+4
-0
SecurityConfig.java
src/main/java/web/multitask/app/config/SecurityConfig.java
+13
-10
WebSocketAuthorizationSecurityConfig.java
...task/app/config/WebSocketAuthorizationSecurityConfig.java
+24
-0
WebSocketConfig.java
src/main/java/web/multitask/app/config/WebSocketConfig.java
+16
-1
MessageController.java
.../java/web/multitask/app/controller/MessageController.java
+1
-1
AuthChannelInterceptorAdapter.java
...b/multitask/app/filter/AuthChannelInterceptorAdapter.java
+53
-0
JWTokenFilter.java
src/main/java/web/multitask/app/filter/JWTokenFilter.java
+18
-11
EmailRequest.java
src/main/java/web/multitask/app/model/EmailRequest.java
+3
-0
FileRequest.java
src/main/java/web/multitask/app/model/FileRequest.java
+14
-0
EmailMysql.java
src/main/java/web/multitask/app/mysql/EmailMysql.java
+35
-0
EmailService.java
src/main/java/web/multitask/app/service/EmailService.java
+10
-13
PDFService.java
src/main/java/web/multitask/app/service/PDFService.java
+39
-0
CommonUtils.java
src/main/java/web/multitask/app/utils/CommonUtils.java
+71
-0
JWTokenUtil.java
src/main/java/web/multitask/app/utils/JWTokenUtil.java
+8
-6
No files found.
pom.xml
View file @
6f5b4963
...
...
@@ -17,6 +17,13 @@
<properties>
<java.version>
8
</java.version>
</properties>
<repositories>
<repository>
<id>
itext-releases
</id>
<name>
iText Repository - releases
</name>
<url>
https://repo.itextsupport.com/releases
</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
...
...
@@ -123,8 +130,33 @@
<artifactId>
javax.mail
</artifactId>
<version>
1.6.0
</version>
</dependency>
</dependencies>
<dependency>
<groupId>
org.springframework.security
</groupId>
<artifactId>
spring-security-messaging
</artifactId>
<version>
5.6.0
</version>
</dependency>
<dependency>
<groupId>
commons-io
</groupId>
<artifactId>
commons-io
</artifactId>
<version>
2.15.1
</version>
</dependency>
<dependency>
<groupId>
com.itextpdf
</groupId>
<artifactId>
itext-core
</artifactId>
<version>
8.0.2
</version>
<type>
pom
</type>
</dependency>
<dependency>
<groupId>
com.itextpdf
</groupId>
<artifactId>
itextpdf
</artifactId>
<version>
5.4.0
</version>
<!-- or the latest version available -->
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
</dependency>
</dependencies>
<build>
<finalName>
app
</finalName>
<plugins>
...
...
src/main/java/web/multitask/app/api/AppApi.java
View file @
6f5b4963
...
...
@@ -2,6 +2,7 @@ package web.multitask.app.api;
import
org.json.JSONArray
;
import
org.json.JSONObject
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
web.multitask.app.mysql.ProcedureMysql
;
import
web.multitask.app.repository.UserRespository
;
...
...
@@ -20,24 +21,24 @@ public class AppApi {
}
@PostMapping
(
"/private/procedure"
)
public
String
callProcedure
(
@RequestBody
String
body
)
{
public
ResponseEntity
<?>
callProcedure
(
@RequestBody
String
body
)
{
JSONObject
json
=
new
JSONObject
(
body
);
if
(
json
.
has
(
"procedure"
))
{
try
{
JSONArray
params
=
json
.
isNull
(
"params"
)
?
new
JSONArray
()
:
json
.
getJSONArray
(
"params"
);
JSONObject
response
=
procedureMysql
.
ProcedureExecution
(
json
.
getString
(
"procedure"
),
json
.
getString
(
"database"
),
params
.
toList
().
toArray
());
return
response
.
toString
(
);
return
ResponseEntity
.
ok
(
response
.
toMap
()
);
}
catch
(
Exception
e
)
{
return
new
JSONObject
().
put
(
"data"
,
new
JSONArray
()).
put
(
"message"
,
e
.
getMessage
()).
put
(
"status"
,
false
).
toString
(
);
return
ResponseEntity
.
internalServerError
().
body
(
new
JSONObject
().
put
(
"message"
,
e
.
getMessage
()).
put
(
"status"
,
false
).
toMap
()
);
}
}
else
{
return
new
JSONObject
().
put
(
"data"
,
new
JSONArray
()).
put
(
"message"
,
"Invalid Request"
).
put
(
"status"
,
false
).
toString
(
);
return
ResponseEntity
.
badRequest
().
body
(
new
JSONObject
().
put
(
"message"
,
"Invalid Request"
).
put
(
"status"
,
false
).
toMap
()
);
}
}
@GetMapping
(
"/private/users"
)
public
String
getUsers
(){
return
new
JSONObject
().
put
(
"data"
,
userRepo
.
findAll
()).
put
(
"message"
,
"Success"
).
put
(
"status"
,
true
).
toString
(
);
public
ResponseEntity
<?>
getUsers
(){
return
ResponseEntity
.
ok
(
new
JSONObject
().
put
(
"data"
,
userRepo
.
findAll
()).
put
(
"message"
,
"Success"
).
put
(
"status"
,
true
).
toMap
()
);
}
}
\ No newline at end of file
src/main/java/web/multitask/app/api/EmailApi.java
View file @
6f5b4963
package
web
.
multitask
.
app
.
api
;
import
javax.mail.internet.MimeMessage
;
import
io.github.cdimascio.dotenv.Dotenv
;
import
org.json.JSONObject
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.ModelAttribute
;
import
org.springframework.web.bind.annotation.RestController
;
import
web.multitask.app.model.EmailRequest
;
import
web.multitask.app.mysql.EmailMysql
;
import
web.multitask.app.service.EmailService
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
...
...
@@ -15,32 +19,59 @@ import org.springframework.web.bind.annotation.RequestMethod;
@RequestMapping
(
"/email"
)
public
class
EmailApi
{
private
final
Dotenv
dotenv
;
private
final
EmailService
emailService
;
private
final
EmailMysql
emailMysql
;
public
EmailApi
(
EmailService
emailService
)
{
public
EmailApi
(
EmailService
emailService
,
EmailMysql
emailMysql
,
Dotenv
dotenv
)
{
this
.
emailService
=
emailService
;
this
.
emailMysql
=
emailMysql
;
this
.
dotenv
=
dotenv
;
}
@RequestMapping
(
path
=
"/public/simple"
,
method
=
RequestMethod
.
POST
,
consumes
=
{
MediaType
.
MULTIPART_FORM_DATA_VALUE
})
public
String
simple
(
@ModelAttribute
EmailRequest
request
)
{
@RequestMapping
(
path
=
"/public/simple"
,
method
=
RequestMethod
.
POST
,
consumes
=
{
MediaType
.
MULTIPART_FORM_DATA_VALUE
})
public
ResponseEntity
<?>
simple
(
@ModelAttribute
EmailRequest
request
)
{
try
{
MimeMessage
message
=
emailService
.
simpleMessage
(
request
);
JSONObject
response
=
emailService
.
send
(
message
);
return
response
.
toString
(
);
return
ResponseEntity
.
ok
(
response
.
toMap
()
);
}
catch
(
Exception
e
)
{
return
new
JSONObject
().
put
(
"message"
,
e
.
getMessage
()).
put
(
"status"
,
false
).
toString
(
);
return
ResponseEntity
.
internalServerError
().
body
(
new
JSONObject
().
put
(
"message"
,
e
.
getMessage
()).
put
(
"status"
,
false
).
toMap
()
);
}
}
@RequestMapping
(
path
=
"/private/full"
,
method
=
RequestMethod
.
POST
,
consumes
=
{
MediaType
.
MULTIPART_FORM_DATA_VALUE
})
public
String
full
(
@ModelAttribute
EmailRequest
request
)
{
@RequestMapping
(
path
=
"/private/full"
,
method
=
RequestMethod
.
POST
,
consumes
=
{
MediaType
.
MULTIPART_FORM_DATA_VALUE
})
public
ResponseEntity
<?>
full
(
@ModelAttribute
EmailRequest
request
)
{
try
{
MimeMessage
message
=
emailService
.
htmlMessage
(
request
);
JSONObject
response
=
emailService
.
send
(
message
);
return
response
.
toString
();
return
ResponseEntity
.
ok
(
response
.
toMap
());
}
catch
(
Exception
e
)
{
return
ResponseEntity
.
internalServerError
().
body
(
new
JSONObject
().
put
(
"message"
,
e
.
getMessage
()).
put
(
"status"
,
false
).
toMap
());
}
}
@RequestMapping
(
path
=
"/service/confirmation"
,
method
=
RequestMethod
.
POST
,
consumes
=
{
MediaType
.
MULTIPART_FORM_DATA_VALUE
})
public
ResponseEntity
<?>
template
(
@ModelAttribute
EmailRequest
request
)
{
try
{
String
URL
=
dotenv
.
get
(
"URL_CONFIRMATION"
);
assert
URL
!=
null
;
JSONObject
extra
=
request
.
getExtra
();
JSONObject
response
=
emailMysql
.
getHTMLTemplate
(
extra
);
if
(
response
.
getBoolean
(
"status"
)){
JSONObject
data
=
response
.
getJSONArray
(
"data"
).
getJSONObject
(
0
);
String
html
=
data
.
getString
(
"html"
);
html
=
html
.
replaceAll
(
"TOKEN"
,
extra
.
getString
(
"token"
));
html
=
html
.
replaceAll
(
"URL"
,
URL
);
request
.
setBody
(
html
);
MimeMessage
message
=
emailService
.
htmlMessage
(
request
);
JSONObject
responseEmail
=
emailService
.
send
(
message
);
return
ResponseEntity
.
ok
(
responseEmail
.
toMap
());
}
else
{
return
ResponseEntity
.
internalServerError
().
body
(
new
JSONObject
().
put
(
"message"
,
response
.
getString
(
"message"
)).
put
(
"status"
,
false
).
toMap
());
}
}
catch
(
Exception
e
)
{
return
new
JSONObject
().
put
(
"message"
,
e
.
getMessage
()).
put
(
"status"
,
false
).
toString
(
);
return
ResponseEntity
.
internalServerError
().
body
(
new
JSONObject
().
put
(
"message"
,
e
.
getMessage
()).
put
(
"status"
,
false
).
toMap
()
);
}
}
...
...
src/main/java/web/multitask/app/api/JWTApi.java
deleted
100644 → 0
View file @
1cf76e1d
package
web
.
multitask
.
app
.
api
;
import
org.json.JSONObject
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.web.bind.annotation.*
;
import
web.multitask.app.model.User
;
import
web.multitask.app.repository.UserRespository
;
import
web.multitask.app.utils.JwtTokenUtil
;
import
java.util.Objects
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
@RestController
@RequestMapping
(
"/token"
)
@CrossOrigin
class
JWTApi
{
private
final
JwtTokenUtil
jwtTokenUtil
;
private
final
UserRespository
userRepo
;
public
JWTApi
(
JwtTokenUtil
jwtTokenUtil
,
UserRespository
userRepo
)
{
this
.
jwtTokenUtil
=
jwtTokenUtil
;
this
.
userRepo
=
userRepo
;
}
@PostMapping
(
"/authenticate"
)
public
String
createAuthenticationToken
(
@RequestBody
String
authenticationRequest
)
{
JSONObject
json
=
new
JSONObject
(
authenticationRequest
);
String
username
=
json
.
getString
(
"username"
);
UserDetails
userDetails
=
userRepo
.
findByUsername
(
username
);
if
(!
Objects
.
equals
(
userDetails
.
getPassword
(),
json
.
getString
(
"password"
))){
return
new
JSONObject
().
put
(
"token"
,
""
).
put
(
"message"
,
"Invalid Credentials"
).
put
(
"status"
,
false
).
toString
();
}
else
{
return
new
JSONObject
().
put
(
"token"
,
jwtTokenUtil
.
generateToken
((
User
)
userDetails
)).
put
(
"message"
,
"Generated"
).
put
(
"status"
,
true
).
toString
();
}
}
@PostMapping
(
"/validate"
)
public
String
validateToken
(
@RequestBody
String
token
)
{
JSONObject
json
=
new
JSONObject
(
token
);
if
(
jwtTokenUtil
.
validateToken
(
json
.
getString
(
"token"
)))
{
return
new
JSONObject
().
put
(
"message"
,
"Valid Token"
).
put
(
"status"
,
true
).
toString
();
}
else
{
return
new
JSONObject
().
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
).
toString
();
}
}
}
\ No newline at end of file
src/main/java/web/multitask/app/api/JWTokenApi.java
0 → 100644
View file @
6f5b4963
package
web
.
multitask
.
app
.
api
;
import
org.json.JSONObject
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.web.bind.annotation.*
;
import
web.multitask.app.model.User
;
import
web.multitask.app.repository.UserRespository
;
import
web.multitask.app.utils.JWTokenUtil
;
import
java.math.BigInteger
;
import
java.util.Objects
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
@RestController
@RequestMapping
(
"/token"
)
@CrossOrigin
class
JWTokenApi
{
private
final
JWTokenUtil
jwtTokenUtil
;
private
final
UserRespository
userRepo
;
public
JWTokenApi
(
JWTokenUtil
jwtTokenUtil
,
UserRespository
userRepo
)
{
this
.
jwtTokenUtil
=
jwtTokenUtil
;
this
.
userRepo
=
userRepo
;
}
@PostMapping
(
"/authenticate"
)
public
ResponseEntity
<?>
createAuthenticationToken
(
@RequestBody
String
authenticationRequest
)
{
JSONObject
response
;
JSONObject
json
=
new
JSONObject
(
authenticationRequest
);
String
username
=
json
.
getString
(
"username"
);
UserDetails
userDetails
=
userRepo
.
findByUsername
(
username
);
if
(!
Objects
.
equals
(
userDetails
.
getPassword
(),
json
.
getString
(
"password"
)))
{
response
=
new
JSONObject
().
put
(
"token"
,
""
).
put
(
"message"
,
"Invalid Credentials"
).
put
(
"status"
,
false
);
return
ResponseEntity
.
status
(
401
).
body
(
response
.
toMap
());
}
else
{
return
ResponseEntity
.
ok
(
new
JSONObject
().
put
(
"token"
,
jwtTokenUtil
.
generateToken
((
User
)
userDetails
,
json
.
optBigInteger
(
"ms"
,
BigInteger
.
valueOf
(
3600000
)))).
put
(
"message"
,
"Generated"
).
put
(
"status"
,
true
).
toMap
());
}
}
@PostMapping
(
"/validate"
)
public
ResponseEntity
<?>
validateToken
(
@RequestBody
String
token
)
{
JSONObject
response
;
JSONObject
json
=
new
JSONObject
(
token
);
if
(
jwtTokenUtil
.
validateToken
(
json
.
getString
(
"token"
)))
{
String
dataToken
=
jwtTokenUtil
.
getDataToken
(
json
.
getString
(
"token"
));
if
(
dataToken
==
null
)
{
response
=
new
JSONObject
().
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
);
}
else
{
boolean
isTokenExpired
=
jwtTokenUtil
.
isTokenExpired
(
json
.
getString
(
"token"
));
if
(
isTokenExpired
)
{
response
=
new
JSONObject
().
put
(
"message"
,
"Expired Token"
).
put
(
"status"
,
false
);
return
ResponseEntity
.
status
(
403
).
body
(
response
.
toMap
());
}
try
{
UserDetails
userDetails
=
userRepo
.
findByUsername
(
new
JSONObject
(
dataToken
).
getString
(
"username"
));
if
(
userDetails
.
getUsername
()
==
null
)
{
response
=
new
JSONObject
().
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
);
}
else
{
response
=
new
JSONObject
().
put
(
"message"
,
"Valid Token"
).
put
(
"status"
,
true
);
}
}
catch
(
Exception
e
)
{
response
=
new
JSONObject
().
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
);
}
}
}
else
{
response
=
new
JSONObject
().
put
(
"message"
,
"Invalid Token"
).
put
(
"status"
,
false
);
}
if
(
response
.
getBoolean
(
"status"
))
{
return
ResponseEntity
.
ok
(
response
.
toMap
());
}
else
{
return
ResponseEntity
.
status
(
401
).
body
(
response
.
toMap
());
}
}
@PostMapping
(
"/service/authenticate"
)
public
ResponseEntity
<?>
generateToken
(
@RequestBody
String
token
)
{
JSONObject
json
=
new
JSONObject
(
token
);
UserDetails
userDetails
=
userRepo
.
findByUsername
(
json
.
getString
(
"username"
));
if
(
userDetails
==
null
)
{
return
ResponseEntity
.
status
(
401
).
body
(
new
JSONObject
().
put
(
"token"
,
""
).
put
(
"message"
,
"Invalid Credentials"
).
put
(
"status"
,
false
).
toMap
());
}
else
if
(
userDetails
.
getAuthorities
().
stream
().
anyMatch
(
a
->
a
.
getAuthority
().
equals
(
"SERVICE"
)))
{
return
ResponseEntity
.
ok
(
new
JSONObject
().
put
(
"token"
,
jwtTokenUtil
.
generateToken
((
User
)
userDetails
,
json
.
optBigInteger
(
"ms"
,
BigInteger
.
valueOf
(
3600000
)))).
put
(
"message"
,
"Generated"
).
put
(
"status"
,
true
).
toMap
());
}
else
{
return
ResponseEntity
.
status
(
401
).
body
(
new
JSONObject
().
put
(
"token"
,
""
).
put
(
"message"
,
"Invalid Credentials"
).
put
(
"status"
,
false
).
toMap
());
}
}
}
\ No newline at end of file
src/main/java/web/multitask/app/api/PDFApi.java
0 → 100644
View file @
6f5b4963
package
web
.
multitask
.
app
.
api
;
import
org.json.JSONObject
;
import
org.springframework.core.io.ByteArrayResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
web.multitask.app.service.PDFService
;
@RestController
@CrossOrigin
(
origins
=
"*"
)
@RequestMapping
(
"pdf"
)
public
class
PDFApi
{
PDFService
pdfService
;
public
PDFApi
(
PDFService
pdfService
)
{
this
.
pdfService
=
pdfService
;
}
@PostMapping
(
"/public/html"
)
public
ResponseEntity
<
Resource
>
htmlToPdf
(
@RequestBody
String
json
)
{
try
{
JSONObject
bodyJson
=
new
JSONObject
(
json
);
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
add
(
HttpHeaders
.
CONTENT_DISPOSITION
,
"attachment; filename="
+
bodyJson
.
getString
(
"name"
));
ByteArrayResource
resource
=
pdfService
.
htmlToPdf
(
bodyJson
.
getString
(
"html"
));
return
ResponseEntity
.
ok
().
headers
(
headers
)
.
contentLength
(
resource
.
contentLength
())
.
contentType
(
org
.
springframework
.
http
.
MediaType
.
APPLICATION_PDF
)
.
body
(
resource
);
}
catch
(
Exception
e
){
System
.
out
.
println
(
e
);
return
ResponseEntity
.
badRequest
().
body
(
null
);
}
}
}
\ No newline at end of file
src/main/java/web/multitask/app/api/UtilsApi.java
0 → 100644
View file @
6f5b4963
package
web
.
multitask
.
app
.
api
;
import
org.json.JSONArray
;
import
org.json.JSONObject
;
import
org.springframework.core.io.FileSystemResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
web.multitask.app.model.FileRequest
;
import
web.multitask.app.utils.CommonUtils
;
import
java.io.File
;
import
java.util.Arrays
;
@RestController
@CrossOrigin
(
origins
=
"*"
)
@RequestMapping
(
"utils"
)
public
class
UtilsApi
{
CommonUtils
commonUtils
;
public
UtilsApi
(
CommonUtils
commonUtils
)
{
this
.
commonUtils
=
commonUtils
;
}
@RequestMapping
(
path
=
"/public/file/base64"
,
method
=
RequestMethod
.
POST
,
consumes
=
{
MediaType
.
MULTIPART_FORM_DATA_VALUE
})
public
ResponseEntity
<?>
fileBase64
(
@ModelAttribute
FileRequest
request
)
{
JSONObject
response
=
new
JSONObject
();
try
{
MultipartFile
[]
files
=
request
.
getFiles
();
JSONArray
jsonArray
=
new
JSONArray
();
Arrays
.
stream
(
files
).
forEach
(
file
->
{
jsonArray
.
put
(
commonUtils
.
convertToBase64
(
file
));
});
response
.
put
(
"files"
,
jsonArray
);
response
.
put
(
"message"
,
"Success"
);
response
.
put
(
"status"
,
true
);
return
ResponseEntity
.
ok
(
response
.
toMap
());
}
catch
(
Exception
e
){
System
.
out
.
println
(
e
);
return
ResponseEntity
.
badRequest
().
body
(
e
.
getMessage
());
}
}
@PostMapping
(
"/public/base64/file"
)
public
ResponseEntity
<
Resource
>
base64File
(
@RequestBody
String
json
)
{
try
{
JSONObject
bodyJson
=
new
JSONObject
(
json
);
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
add
(
HttpHeaders
.
CONTENT_DISPOSITION
,
"attachment; filename="
+
bodyJson
.
getString
(
"name"
));
File
outputFile
=
commonUtils
.
convertToFile
(
bodyJson
.
getString
(
"base64"
),
bodyJson
.
getString
(
"name"
));
Resource
resource
=
new
FileSystemResource
(
outputFile
);
return
ResponseEntity
.
ok
()
.
headers
(
headers
)
.
body
(
resource
);
}
catch
(
Exception
e
){
System
.
out
.
println
(
e
);
return
ResponseEntity
.
badRequest
().
body
(
null
);
}
}
}
\ No newline at end of file
src/main/java/web/multitask/app/config/FilterConfig.java
View file @
6f5b4963
...
...
@@ -3,15 +3,15 @@ package web.multitask.app.config;
import
org.springframework.boot.web.servlet.FilterRegistrationBean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
web.multitask.app.filter.J
wt
TokenFilter
;
import
web.multitask.app.filter.J
W
TokenFilter
;
@Configuration
public
class
FilterConfig
{
@Bean
FilterRegistrationBean
<
J
wt
TokenFilter
>
filterRegistrationBean
()
{
FilterRegistrationBean
<
J
wt
TokenFilter
>
registrationBean
=
new
FilterRegistrationBean
<>();
registrationBean
.
setFilter
(
new
J
wt
TokenFilter
());
FilterRegistrationBean
<
J
W
TokenFilter
>
filterRegistrationBean
()
{
FilterRegistrationBean
<
J
W
TokenFilter
>
registrationBean
=
new
FilterRegistrationBean
<>();
registrationBean
.
setFilter
(
new
J
W
TokenFilter
());
registrationBean
.
addUrlPatterns
(
"/**"
);
registrationBean
.
setOrder
(
1
);
return
registrationBean
;
...
...
src/main/java/web/multitask/app/config/MVCConfig.java
View file @
6f5b4963
package
web
.
multitask
.
app
.
config
;
import
com.fasterxml.jackson.annotation.JsonInclude
;
import
com.fasterxml.jackson.databind.PropertyNamingStrategy
;
import
org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
...
...
src/main/java/web/multitask/app/config/SecurityConfig.java
View file @
6f5b4963
...
...
@@ -10,24 +10,25 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import
org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
;
import
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
;
import
org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer
;
import
org.springframework.security.config.http.SessionCreationPolicy
;
import
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
;
import
org.springframework.security.crypto.password.PasswordEncoder
;
import
org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
;
import
org.springframework.web.cors.CorsConfiguration
;
import
org.springframework.web.cors.UrlBasedCorsConfigurationSource
;
import
org.springframework.web.filter.CorsFilter
;
import
web.multitask.app.filter.J
wt
TokenFilter
;
import
web.multitask.app.filter.J
W
TokenFilter
;
import
web.multitask.app.repository.UserRespository
;
import
web.multitask.app.utils.J
wt
TokenUtil
;
import
web.multitask.app.utils.J
W
TokenUtil
;
@Configuration
@EnableWebSecurity
public
class
SecurityConfig
extends
WebSecurityConfigurerAdapter
{
private
final
UserRespository
userRepo
;
private
final
J
wt
TokenUtil
jwtTokenUtil
;
private
final
J
W
TokenUtil
jwtTokenUtil
;
public
SecurityConfig
(
UserRespository
userRepo
,
J
wt
TokenUtil
jwtTokenUtil
)
{
public
SecurityConfig
(
UserRespository
userRepo
,
J
W
TokenUtil
jwtTokenUtil
)
{
this
.
userRepo
=
userRepo
;
this
.
jwtTokenUtil
=
jwtTokenUtil
;
}
...
...
@@ -46,17 +47,19 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected
void
configure
(
HttpSecurity
http
)
throws
Exception
{
http
.
cors
(
AbstractHttpConfigurer:
:
disable
).
csrf
(
AbstractHttpConfigurer:
:
disable
)
.
sessionManagement
().
sessionCreationPolicy
(
SessionCreationPolicy
.
STATELESS
).
and
()
.
authorizeHttpRequests
(
authorizeRequests
->
authorizeRequests
.
antMatchers
(
"/security/**"
).
hasAnyAuthority
(
"ADMIN"
)
// .antMatchers("/api/**").hasAnyAuthority("ADMIN", "USER")
.
antMatchers
(
"/token/**"
).
permitAll
()
.
regexMatchers
(
".*/private/.*"
).
hasAnyAuthority
(
"ADMIN"
,
"USER"
)
.
regexMatchers
(
".*/public/.*"
).
permitAll
()
.
regexMatchers
(
".*/service/.*"
).
hasAnyAuthority
(
"ADMIN"
,
"SERVICE"
)
.
antMatchers
(
HttpMethod
.
GET
,
"/**"
).
permitAll
()
.
anyRequest
()
.
authenticated
());
http
.
addFilterBefore
(
new
JwtTokenFilter
(
jwtTokenUtil
,
userRepo
),
UsernamePasswordAuthenticationFilter
.
class
);
// .antMatchers(HttpMethod.POST, "/**").permitAll()
.
antMatchers
(
"/token/**"
).
permitAll
());
// .anyRequest()
// .authenticated());
http
.
addFilterBefore
(
new
JWTokenFilter
(
jwtTokenUtil
,
userRepo
),
UsernamePasswordAuthenticationFilter
.
class
);
}
@Bean
...
...
@@ -68,7 +71,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
CorsFilter
corsFilter
()
{
UrlBasedCorsConfigurationSource
source
=
new
UrlBasedCorsConfigurationSource
();
CorsConfiguration
config
=
new
CorsConfiguration
();
config
.
setAllowCredentials
(
true
);
//
config.setAllowCredentials(true);
config
.
addAllowedOrigin
(
"*"
);
config
.
addAllowedHeader
(
"*"
);
config
.
addAllowedMethod
(
"*"
);
...
...
src/main/java/web/multitask/app/config/WebSocketAuthorizationSecurityConfig.java
0 → 100644
View file @
6f5b4963
package
web
.
multitask
.
app
.
config
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.security.config.annotation.web.messaging.MessageSecurityMetadataSourceRegistry
;
import
org.springframework.security.config.annotation.web.socket.AbstractSecurityWebSocketMessageBrokerConfigurer
;
import
org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker
;
@Configuration
@EnableWebSocketMessageBroker
public
class
WebSocketAuthorizationSecurityConfig
extends
AbstractSecurityWebSocketMessageBrokerConfigurer
{
@Override
protected
void
configureInbound
(
final
MessageSecurityMetadataSourceRegistry
messages
)
{
//onConnect
messages
.
simpMessageDestMatchers
(
"/websocket"
).
permitAll
();
//onSubscribe
messages
.
simpSubscribeDestMatchers
(
"/topic/**"
).
authenticated
();
}
@Override
protected
boolean
sameOriginDisabled
()
{
return
true
;
}
}
\ No newline at end of file
src/main/java/web/multitask/app/config/WebSocketConfig.java
View file @
6f5b4963
package
web
.
multitask
.
app
.
config
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.messaging.simp.config.ChannelRegistration
;
import
org.springframework.messaging.simp.config.MessageBrokerRegistry
;
import
org.springframework.messaging.support.ChannelInterceptor
;
import
org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker
;
import
org.springframework.web.socket.config.annotation.StompEndpointRegistry
;
import
org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer
;
import
web.multitask.app.filter.AuthChannelInterceptorAdapter
;
@Configuration
@EnableWebSocketMessageBroker
public
class
WebSocketConfig
implements
WebSocketMessageBrokerConfigurer
{
private
final
AuthChannelInterceptorAdapter
authChannelInterceptorAdapter
;
public
WebSocketConfig
(
AuthChannelInterceptorAdapter
authChannelInterceptorAdapter
)
{
this
.
authChannelInterceptorAdapter
=
authChannelInterceptorAdapter
;
}
@Override
public
void
configureMessageBroker
(
MessageBrokerRegistry
registry
)
{
registry
.
enableSimpleBroker
(
"/topic"
);
registry
.
setApplicationDestinationPrefixes
(
"/
app
"
);
registry
.
setApplicationDestinationPrefixes
(
"/
ws
"
);
}
@Override
...
...
@@ -21,4 +30,9 @@ public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
registry
.
addEndpoint
(
"websocket"
).
setAllowedOrigins
(
"*"
);
}
@Override
public
void
configureClientInboundChannel
(
ChannelRegistration
registration
)
{
registration
.
interceptors
(
authChannelInterceptorAdapter
);
}
}
\ No newline at end of file
src/main/java/web/multitask/app/controller/MessageController.java
View file @
6f5b4963
...
...
@@ -13,7 +13,7 @@ import java.util.Date;
@Controller
public
class
MessageController
{
@MessageMapping
(
"/
websocket/
{project}/{topic}"
)
@MessageMapping
(
"/{project}/{topic}"
)
@SendTo
(
"/topic/message/{project}/{topic}"
)
public
Response
envio
(
@PathVariable
(
"project"
)
String
project
,
@PathVariable
(
"topic"
)
String
topic
,
Message
message
)
{
...
...
src/main/java/web/multitask/app/filter/AuthChannelInterceptorAdapter.java
0 → 100644
View file @
6f5b4963
package
web
.
multitask
.
app
.
filter
;
import
org.jetbrains.annotations.NotNull
;
import
org.json.JSONObject
;
import
org.springframework.messaging.Message
;
import
org.springframework.messaging.MessageChannel
;
import
org.springframework.messaging.simp.stomp.StompCommand
;
import
org.springframework.messaging.simp.stomp.StompHeaderAccessor
;
import
org.springframework.messaging.support.ChannelInterceptor
;
import
org.springframework.messaging.support.MessageHeaderAccessor
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.LinkedMultiValueMap
;
import
web.multitask.app.repository.UserRespository
;
import
web.multitask.app.utils.JWTokenUtil
;
import
java.util.Objects
;
@Component
public
class
AuthChannelInterceptorAdapter
implements
ChannelInterceptor
{
private
final
JWTokenUtil
jwtTokenUtil
;
private
final
UserRespository
userRepo
;
public
AuthChannelInterceptorAdapter
(
JWTokenUtil
jwtTokenUtil
,
UserRespository
userRepo
)
{
this
.
jwtTokenUtil
=
jwtTokenUtil
;
this
.
userRepo
=
userRepo
;
}
@Override
public
Message
<?>
preSend
(
@NotNull
final
Message
<?>
message
,
@NotNull
final
MessageChannel
channel
)
{
final
StompHeaderAccessor
accessor
=
MessageHeaderAccessor
.
getAccessor
(
message
,
StompHeaderAccessor
.
class
);
assert
accessor
!=
null
;
if
(
StompCommand
.
CONNECT
==
accessor
.
getCommand
())
{
// final String authorization = Objects.requireNonNull(accessor.getHeader("nativeHeaders")).toString();
LinkedMultiValueMap
<
String
,
String
>
map
=
(
LinkedMultiValueMap
<
String
,
String
>)
accessor
.
getHeader
(
"nativeHeaders"
);
String
authorization
=
Objects
.
requireNonNull
(
Objects
.
requireNonNull
(
map
).
get
(
"Authorization"
)).
get
(
0
);
assert
authorization
!=
null
;
final
String
token
=
authorization
.
split
(
" "
)[
1
];
if
(
token
!=
null
&&
jwtTokenUtil
.
validateToken
(
token
)){
JSONObject
jsonToken
=
new
JSONObject
(
jwtTokenUtil
.
getDataToken
(
token
));
UserDetails
userDetails
=
userRepo
.
findByUsername
(
jsonToken
.
getString
(
"username"
));
final
UsernamePasswordAuthenticationToken
user
=
new
UsernamePasswordAuthenticationToken
(
userDetails
,
null
,
userDetails
.
getAuthorities
());
accessor
.
setUser
(
user
);
}
}
return
message
;
}
}
\ No newline at end of file
src/main/java/web/multitask/app/filter/J
wt
TokenFilter.java
→
src/main/java/web/multitask/app/filter/J
W
TokenFilter.java
View file @
6f5b4963
...
...
@@ -18,37 +18,44 @@ import org.springframework.web.filter.OncePerRequestFilter;
import
io.jsonwebtoken.io.IOException
;
import
web.multitask.app.repository.UserRespository
;
import
web.multitask.app.utils.JwtTokenUtil
;
import
web.multitask.app.utils.JWTokenUtil
;
import
java.util.Enumeration
;
@Component
@Order
(
1
)
public
class
J
wt
TokenFilter
extends
OncePerRequestFilter
{
public
class
J
W
TokenFilter
extends
OncePerRequestFilter
{
private
J
wt
TokenUtil
jwtTokenUtil
=
null
;
private
J
W
TokenUtil
jwtTokenUtil
=
null
;
private
UserRespository
userRepo
=
null
;
public
J
wtTokenFilter
(
Jwt
TokenUtil
jwtTokenUtil
,
UserRespository
userRepo
)
{
public
J
WTokenFilter
(
JW
TokenUtil
jwtTokenUtil
,
UserRespository
userRepo
)
{
this
.
jwtTokenUtil
=
jwtTokenUtil
;
this
.
userRepo
=
userRepo
;
}
public
J
wt
TokenFilter
()
{
public
J
W
TokenFilter
()
{
}
@Override
protected
void
doFilterInternal
(
HttpServletRequest
request
,
@NotNull
HttpServletResponse
response
,
@NotNull
FilterChain
chain
)
throws
ServletException
,
IOException
,
java
.
io
.
IOException
{
response
.
setContentType
(
"application/json"
);
// final String queryAuthorization = request.getParameter(HttpHeaders.AUTHORIZATION);
final
String
Authorization
=
request
.
getHeader
(
HttpHeaders
.
AUTHORIZATION
);
final
String
header
=
request
.
getHeader
(
HttpHeaders
.
AUTHORIZATION
)
;
String
token
=
""
;
try
{
token
=
header
.
split
(
" "
)[
1
];
}
catch
(
Exception
e
)
{
String
token
;
try
{
token
=
Authorization
.
split
(
" "
)[
1
];
}
catch
(
Exception
e
)
{
token
=
null
;
}
if
(
token
==
null
||
token
.
isEmpty
())
{
SecurityContextHolder
.
getContext
().
setAuthentication
(
null
);
//
SecurityContextHolder.getContext().setAuthentication(null);
chain
.
doFilter
(
request
,
response
);
}
else
{
if
(
jwtTokenUtil
.
validateToken
(
token
))
{
...
...
src/main/java/web/multitask/app/model/EmailRequest.java
View file @
6f5b4963
package
web
.
multitask
.
app
.
model
;
import
org.json.JSONObject
;
import
org.springframework.web.multipart.MultipartFile
;
import
lombok.Getter
;
...
...
@@ -16,4 +17,5 @@ public class EmailRequest {
private
String
subject
;
private
String
body
;
private
MultipartFile
[]
files
;
private
JSONObject
extra
;
}
\ No newline at end of file
src/main/java/web/multitask/app/model/FileRequest.java
0 → 100644
View file @
6f5b4963
package
web
.
multitask
.
app
.
model
;
import
lombok.Getter
;
import
lombok.NoArgsConstructor
;
import
lombok.Setter
;
import
org.springframework.web.multipart.MultipartFile
;
@Getter
@Setter
@NoArgsConstructor
public
class
FileRequest
{
MultipartFile
[]
files
;
}
\ No newline at end of file
src/main/java/web/multitask/app/mysql/EmailMysql.java
0 → 100644
View file @
6f5b4963
package
web
.
multitask
.
app
.
mysql
;
import
org.json.JSONObject
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.stereotype.Service
;
import
web.multitask.app.utils.JWTokenUtil
;
import
java.util.List
;
import
java.util.Map
;
@Service
public
class
EmailMysql
{
private
final
JdbcTemplate
jdbcTemplate
;
public
EmailMysql
(
JdbcTemplate
jdbcTemplate
)
{
this
.
jdbcTemplate
=
jdbcTemplate
;
}
public
JSONObject
getHTMLTemplate
(
JSONObject
json
)
{
String
id
=
json
.
optString
(
"id"
,
null
);
if
(
id
==
null
){
return
new
JSONObject
().
put
(
"message"
,
"Invalid Request"
).
put
(
"status"
,
false
);
}
String
sql
=
"SELECT * FROM security.email_template WHERE id = ?"
;
Object
[]
params
=
new
Object
[]
{
id
};
try
{
List
<
Map
<
String
,
Object
>>
rows
=
jdbcTemplate
.
queryForList
(
sql
,
params
);
return
new
JSONObject
().
put
(
"data"
,
rows
).
put
(
"message"
,
"Success"
).
put
(
"status"
,
true
);
}
catch
(
Exception
e
){
return
new
JSONObject
().
put
(
"message"
,
e
.
getMessage
()).
put
(
"status"
,
false
);
}
}
}
\ No newline at end of file
src/main/java/web/multitask/app/service/EmailService.java
View file @
6f5b4963
...
...
@@ -32,16 +32,19 @@ import org.springframework.web.multipart.MultipartFile;
import
io.github.cdimascio.dotenv.Dotenv
;
import
web.multitask.app.model.EmailRequest
;
import
web.multitask.app.utils.CommonUtils
;
@Service
public
class
EmailService
{
Dotenv
dotenv
;
Session
session
;
CommonUtils
commonUtils
;
public
EmailService
(
Dotenv
dotenv
,
Session
session
)
{
public
EmailService
(
Dotenv
dotenv
,
Session
session
,
CommonUtils
commonUtils
)
{
this
.
dotenv
=
dotenv
;
this
.
session
=
session
;
this
.
commonUtils
=
commonUtils
;
}
public
MimeMessage
htmlMessage
(
EmailRequest
request
)
throws
UnsupportedEncodingException
{
...
...
@@ -96,16 +99,6 @@ public class EmailService {
}
}
public
void
deleteAllFiles
(
String
file_folder
){
File
file
=
new
File
(
file_folder
);
if
(
file
.
exists
()){
File
[]
files
=
file
.
listFiles
();
for
(
File
f:
files
){
f
.
delete
();
}
}
}
public
MimeMessage
simpleMessage
(
EmailRequest
request
)
throws
UnsupportedEncodingException
{
try
{
MimeMessage
message
=
new
MimeMessage
(
session
);
...
...
@@ -131,7 +124,11 @@ public class EmailService {
t
.
connect
(
email
,
password
);
t
.
sendMessage
(
message
,
message
.
getAllRecipients
());
t
.
close
();
deleteAllFiles
(
dotenv
.
get
(
"FILE_FOLDER"
));
// CommonUtils.deleteAllFiles();
boolean
deletes
=
commonUtils
.
deleteAllFiles
();
if
(!
deletes
){
System
.
out
.
println
(
"Error al eliminar los archivos"
);
}
return
new
JSONObject
().
put
(
"message"
,
"OK"
).
put
(
"status"
,
true
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
.
getMessage
());
...
...
@@ -147,7 +144,7 @@ public class EmailService {
try
{
return
new
InternetAddress
(
recipient
);
}
catch
(
AddressException
e
)
{
e
.
printStackTrace
(
);
System
.
out
.
println
(
e
.
getMessage
()
);
return
null
;
}
}).
filter
(
Objects:
:
nonNull
).
collect
(
Collectors
.
toList
()).
toArray
(
addresses
);
...
...
src/main/java/web/multitask/app/service/PDFService.java
0 → 100644
View file @
6f5b4963
package
web
.
multitask
.
app
.
service
;
import
com.itextpdf.io.source.ByteArrayOutputStream
;
import
com.itextpdf.text.PageSize
;
import
com.itextpdf.text.html.simpleparser.HTMLWorker
;
import
com.itextpdf.text.pdf.PdfWriter
;
import
org.springframework.core.io.ByteArrayResource
;
import
org.springframework.stereotype.Service
;
import
com.itextpdf.text.Document
;
import
java.io.StringReader
;
@Service
public
class
PDFService
{
public
ByteArrayResource
htmlToPdf
(
String
html
){
try
{
byte
[]
pdfBytes
=
generatePdf
(
html
);
return
new
ByteArrayResource
(
pdfBytes
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
.
getMessage
());
return
null
;
}
}
private
byte
[]
generatePdf
(
String
htmlContent
)
throws
Exception
{
Document
document
=
new
Document
(
PageSize
.
A4
);
ByteArrayOutputStream
outputStream
=
new
ByteArrayOutputStream
();
PdfWriter
writer
=
PdfWriter
.
getInstance
(
document
,
outputStream
);
document
.
open
();
HTMLWorker
htmlWorker
=
new
HTMLWorker
(
document
);
htmlWorker
.
parse
(
new
StringReader
(
htmlContent
));
document
.
close
();
writer
.
close
();
return
outputStream
.
toByteArray
();
}
}
\ No newline at end of file
src/main/java/web/multitask/app/utils/CommonUtils.java
0 → 100644
View file @
6f5b4963
package
web
.
multitask
.
app
.
utils
;
import
io.github.cdimascio.dotenv.Dotenv
;
import
org.json.JSONObject
;
import
org.apache.commons.io.FileUtils
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.File
;
import
java.util.Arrays
;
import
java.util.Base64
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.stream.Collectors
;
@Service
public
class
CommonUtils
{
private
final
Dotenv
dotenv
;
public
CommonUtils
(
Dotenv
dotenv
)
{
this
.
dotenv
=
dotenv
;
}
public
JSONObject
convertToBase64
(
MultipartFile
file
)
{
JSONObject
response
=
new
JSONObject
();
try
{
byte
[]
byteArray
=
file
.
getBytes
();
String
base64
=
Base64
.
getEncoder
().
encodeToString
(
byteArray
);
response
.
put
(
"base64"
,
base64
);
response
.
put
(
"name"
,
file
.
getOriginalFilename
());
response
.
put
(
"size"
,
file
.
getSize
());
response
.
put
(
"extension"
,
file
.
getContentType
());
return
response
;
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
.
getMessage
());
return
null
;
}
}
public
File
convertToFile
(
String
base64
,
String
name
)
{
try
{
byte
[]
byteArray
=
Base64
.
getDecoder
().
decode
(
base64
);
String
folder
=
dotenv
.
get
(
"FILE_FOLDER"
)
+
"/"
+
name
;
File
outputFile
=
new
File
(
folder
);
FileUtils
.
writeByteArrayToFile
(
outputFile
,
byteArray
);
return
outputFile
;
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
.
getMessage
());
return
null
;
}
}
public
boolean
deleteAllFiles
()
{
try
{
String
folder
=
dotenv
.
get
(
"FILE_FOLDER"
);
assert
folder
!=
null
;
File
file
=
new
File
(
folder
);
if
(
file
.
isDirectory
())
{
Boolean
[]
results
=
Arrays
.
stream
(
Objects
.
requireNonNull
(
file
.
listFiles
())).
map
(
File:
:
delete
).
toArray
(
Boolean
[]::
new
);
return
Arrays
.
asList
(
results
).
contains
(
false
);
}
else
{
return
file
.
delete
();
}
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
.
getMessage
());
return
false
;
}
}
}
\ No newline at end of file
src/main/java/web/multitask/app/utils/J
wt
TokenUtil.java
→
src/main/java/web/multitask/app/utils/J
W
TokenUtil.java
View file @
6f5b4963
package
web
.
multitask
.
app
.
utils
;
import
java.io.Serializable
;
import
java.math.BigInteger
;
import
java.util.Date
;
import
org.json.JSONObject
;
...
...
@@ -10,17 +11,18 @@ import io.jsonwebtoken.Jwts;
import
io.jsonwebtoken.security.Keys
;
import
org.springframework.stereotype.Component
;
import
web.multitask.app.model.User
;
@Component
public
class
J
wt
TokenUtil
implements
Serializable
{
public
class
J
W
TokenUtil
implements
Serializable
{
@Value
(
"${app.jwtSecret}"
)
private
String
jwtSecret
;
public
String
generateToken
(
User
user
)
{
public
String
generateToken
(
User
user
,
BigInteger
ms
)
{
if
(
ms
==
null
){
ms
=
BigInteger
.
valueOf
(
3600000
);
}
Date
now
=
new
Date
();
Date
expiryDate
=
new
Date
(
now
.
getTime
()
+
3600000
);
Date
expiryDate
=
new
Date
(
now
.
getTime
()
+
ms
.
longValue
()
);
JSONObject
json
=
new
JSONObject
();
json
.
put
(
"username"
,
user
.
getUsername
());
return
Jwts
.
builder
()
...
...
@@ -44,7 +46,6 @@ public class JwtTokenUtil implements Serializable{
}
public
String
getDataToken
(
String
token
)
{
try
{
return
Jwts
.
parserBuilder
()
.
setSigningKey
(
Keys
.
hmacShaKeyFor
(
jwtSecret
.
getBytes
()))
...
...
@@ -52,6 +53,7 @@ public class JwtTokenUtil implements Serializable{
.
parseClaimsJws
(
token
)
.
getBody
().
getSubject
();
}
catch
(
Exception
e
){
System
.
out
.
println
(
e
.
getMessage
());
return
null
;
}
}
...
...
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