Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
portal-backend
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
HBP Medical Informatics Platform
portal-backend
Commits
71097e5d
Commit
71097e5d
authored
5 years ago
by
Manuel Spuhler
Browse files
Options
Downloads
Patches
Plain Diff
New JWT implementation
parent
009b96b5
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pom.xml
+3
-3
3 additions, 3 deletions
pom.xml
src/main/java/eu/hbp/mip/controllers/JWTApi.java
+8
-31
8 additions, 31 deletions
src/main/java/eu/hbp/mip/controllers/JWTApi.java
with
11 additions
and
34 deletions
pom.xml
+
3
−
3
View file @
71097e5d
...
...
@@ -294,9 +294,9 @@
<scope>
compile
</scope>
</dependency>
<dependency>
<groupId>
io.jsonwebtoken
</groupId>
<artifactId>
jjwt
</artifactId>
<version>
0.9.1
</version>
<groupId>
com.auth0
</groupId>
<artifactId>
j
ava-
jwt
</artifactId>
<version>
3.8.3
</version>
</dependency>
</dependencies>
...
...
This diff is collapsed.
Click to expand it.
src/main/java/eu/hbp/mip/controllers/JWTApi.java
+
8
−
31
View file @
71097e5d
package
eu.hbp.mip.controllers
;
import
static
org
.
springframework
.
http
.
MediaType
.
TEXT_PLAIN_VALUE
;
import
java.security.Key
;
import
java.util.Date
;
import
javax.crypto.spec.SecretKeySpec
;
import
javax.xml.bind.DatatypeConverter
;
import
eu.hbp.mip.model.UserInfo
;
import
org.slf4j.Logger
;
import
eu.hbp.mip.model.User
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
io.jsonwebtoken.*
;
import
com.auth0.jwt.algorithms.Algorithm
;
import
com.auth0.jwt.JWT
;
import
org.springframework.web.bind.annotation.*
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
...
...
@@ -24,35 +19,17 @@ public class JWTApi {
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
JWTApi
.
class
);
@Autowired
private
UserInfo
userInfo
;
@ApiOperation
(
value
=
"Create a JSON Web Token"
,
response
=
String
.
class
)
@RequestMapping
(
method
=
RequestMethod
.
POST
)
public
ResponseEntity
<
String
>
createJWT
()
{
LOGGER
.
info
(
"Create a JSON Web Token"
);
// SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
// String apiKey = "6v2oxpJMzU14U-dqVireln5AUKTtx5fBPSEgaBZiI983d98cfa6";
// byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(apiKey);
// Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
long
nowMillis
=
System
.
currentTimeMillis
();
Date
now
=
new
Date
(
nowMillis
);
User
user
=
userInfo
.
getUser
();
// Set the JWT Claims
JwtBuilder
builder
=
Jwts
.
builder
().
setIssuedAt
(
now
).
setIssuer
(
"mip.humanbrainproject.eu"
)
.
setSubject
(
user
.
getEmail
()).
signWith
(
SignatureAlgorithm
.
HS512
,
"hbpSecret"
);
String
token
=
builder
.
compact
();
LOGGER
.
info
(
token
);
// long expMillis = nowMillis + 86400 * 24;
// Date exp = new Date(expMillis);
// builder.setExpiration(exp);
Algorithm
algorithm
=
Algorithm
.
HMAC512
(
"secret"
);
String
token
=
JWT
.
create
()
.
withIssuer
(
"mip.humanbrainproject.eu"
)
.
withSubject
(
"subj"
)
.
sign
(
algorithm
);
return
ResponseEntity
.
status
(
HttpStatus
.
CREATED
).
body
(
token
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment