Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dawn-java-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dawn
java
dawn-java-common
Merge requests
!6
add jwt interceptor
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
add jwt interceptor
jwt
into
develop
Overview
2
Commits
1
Pipelines
0
Changes
1
Merged
Tucker Gary Siegel
requested to merge
jwt
into
develop
2 years ago
Overview
2
Commits
1
Pipelines
0
Changes
1
Expand
Auto-generated change type comment
What kind of change is this (only select one):
Chore
Refactor
Bug Fix
Improvement
Feature
Deprecation
Breaking
To learn more about what this is, see
dawn-bot
Edited
2 years ago
by
Tucker Gary Siegel
0
0
Merge request reports
Compare
develop
develop (base)
and
latest version
latest version
8629f913
1 commit,
2 years ago
1 file
+
56
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
src/main/java/edu/umd/dawn/common/interceptor/JWTInterceptor.java
0 → 100644
+
56
−
0
Options
package
edu.umd.dawn.common.interceptor
;
import
edu.umd.dawn.common.jwt.Claims
;
import
edu.umd.dawn.common.jwt.JWTUtil
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
import
lombok.extern.log4j.Log4j2
;
import
org.springframework.web.servlet.HandlerInterceptor
;
/**
* interceptor to pull the claims and token for a jwt
* @implNote This does NOT throw any error if a jwt is not provided
*/
@Log4j2
public
class
JWTInterceptor
implements
HandlerInterceptor
{
private
boolean
local
;
private
boolean
warn
;
private
String
accessSecret
;
/**
*
* @param accessSecret JWT Access Secret from configuration
* @param local is the environment a local env or not
* @param warn should a warning be thrown if no jwt is provided
*/
public
JWTInterceptor
(
String
accessSecret
,
boolean
local
,
boolean
warn
)
{
this
.
local
=
local
;
this
.
accessSecret
=
accessSecret
;
this
.
warn
=
warn
;
}
@Override
public
boolean
preHandle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
)
throws
Exception
{
if
(!
local
)
{
String
token
=
request
.
getHeader
(
"Authorization"
);
if
(
token
!=
null
&&
!
token
.
equals
(
""
))
{
if
(
token
.
startsWith
(
"Bearer "
))
{
token
=
token
.
replace
(
"Bearer "
,
""
);
}
Claims
claims
=
JWTUtil
.
parse
(
accessSecret
,
token
).
getClaims
();
request
.
setAttribute
(
"claims"
,
claims
);
request
.
setAttribute
(
"token"
,
token
);
}
else
if
(
warn
)
{
log
.
warn
(
"No jwt provided"
);
}
}
else
{
log
.
warn
(
"JWT interceptor has been disabled - if this is a production environment, consider this a"
+
" critical security error"
);
}
return
true
;
}
}
Loading