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
4f41c3f6
Commit
4f41c3f6
authored
6 years ago
by
Manuel Spuhler
Browse files
Options
Downloads
Patches
Plain Diff
POC: createJWT API method
parent
bf32eb6a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
pom.xml
+17
-0
17 additions, 0 deletions
pom.xml
src/main/java/eu/hbp/mip/controllers/JWTApi.java
+56
-0
56 additions, 0 deletions
src/main/java/eu/hbp/mip/controllers/JWTApi.java
src/main/java/eu/hbp/mip/model/UserInfo.java
+1
-0
1 addition, 0 deletions
src/main/java/eu/hbp/mip/model/UserInfo.java
with
74 additions
and
0 deletions
pom.xml
+
17
−
0
View file @
4f41c3f6
...
...
@@ -293,6 +293,23 @@
<version>
${scala.release.version}
</version>
<scope>
compile
</scope>
</dependency>
<dependency>
<groupId>
io.jsonwebtoken
</groupId>
<artifactId>
jjwt-api
</artifactId>
<version>
0.10.5
</version>
</dependency>
<dependency>
<groupId>
io.jsonwebtoken
</groupId>
<artifactId>
jjwt-impl
</artifactId>
<version>
0.10.5
</version>
<scope>
runtime
</scope>
</dependency>
<dependency>
<groupId>
io.jsonwebtoken
</groupId>
<artifactId>
jjwt-jackson
</artifactId>
<version>
0.10.5
</version>
<scope>
runtime
</scope>
</dependency>
</dependencies>
<build>
...
...
This diff is collapsed.
Click to expand it.
src/main/java/eu/hbp/mip/controllers/JWTApi.java
0 → 100644
+
56
−
0
View file @
4f41c3f6
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
org.springframework.web.bind.annotation.*
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
@RestController
@RequestMapping
(
value
=
"/jwt"
,
produces
=
{
TEXT_PLAIN_VALUE
})
@Api
(
value
=
"/jwt"
,
description
=
"the jwt API"
)
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
,
signingKey
);
long
expMillis
=
nowMillis
+
86400
*
24
;
Date
exp
=
new
Date
(
expMillis
);
builder
.
setExpiration
(
exp
);
return
ResponseEntity
.
status
(
HttpStatus
.
CREATED
).
body
(
builder
.
compact
());
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/main/java/eu/hbp/mip/model/UserInfo.java
+
1
−
0
View file @
4f41c3f6
...
...
@@ -52,6 +52,7 @@ public class UserInfo {
user
=
new
User
();
user
.
setUsername
(
"anonymous"
);
user
.
setFullname
(
"anonymous"
);
user
.
setEmail
(
"anonymous@anonymous.com"
);
user
.
setPicture
(
"images/users/default_user.png"
);
}
else
{
user
=
new
User
(
getUserInfos
());
...
...
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