Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
alfa
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OZG-Cloud
app
alfa
Commits
52355689
Commit
52355689
authored
1 year ago
by
OZGCloud
Browse files
Options
Downloads
Patches
Plain Diff
OZG-3928 adjust currentUserId; fix order/modifierAccessor
parent
c384bdaf
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
alfa-service/src/main/java/de/ozgcloud/alfa/common/user/CurrentUserHelper.java
+20
-16
20 additions, 16 deletions
.../java/de/ozgcloud/alfa/common/user/CurrentUserHelper.java
with
20 additions
and
16 deletions
alfa-service/src/main/java/de/ozgcloud/alfa/common/user/CurrentUserHelper.java
+
20
−
16
View file @
52355689
...
...
@@ -34,6 +34,7 @@ import org.springframework.security.authentication.AuthenticationTrustResolverIm
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.GrantedAuthority
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.security.oauth2.jwt.Jwt
;
import
lombok.AccessLevel
;
import
lombok.NoArgsConstructor
;
...
...
@@ -41,20 +42,13 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
(
access
=
AccessLevel
.
PRIVATE
)
public
class
CurrentUserHelper
{
static
final
String
ROLE_PREFIX
=
"ROLE_"
;
private
static
final
String
SUB_CLAIM_KEY
=
"sub"
;
public
static
final
Predicate
<
String
>
HAS_ROLE
=
CurrentUserHelper:
:
hasRole
;
private
static
final
AuthenticationTrustResolver
TRUST_RESOLVER
=
new
AuthenticationTrustResolverImpl
();
private
static
final
Predicate
<
Authentication
>
TRUSTED
=
auth
->
!
TRUST_RESOLVER
.
isAnonymous
(
auth
);
public
static
Authentication
getAuthentication
()
{
return
findAuthentication
().
orElseThrow
(()
->
new
IllegalStateException
(
"No authenticated User found"
));
}
public
static
Optional
<
Authentication
>
findAuthentication
()
{
return
Optional
.
ofNullable
(
SecurityContextHolder
.
getContext
().
getAuthentication
()).
filter
(
TRUSTED
);
}
public
static
boolean
hasRole
(
String
role
)
{
var
auth
=
getAuthentication
();
...
...
@@ -82,7 +76,15 @@ public class CurrentUserHelper {
return
containsRole
(
roleToCheck
,
authorities
);
}
public
static
String
prepareRoleForCheck
(
String
role
)
{
static
boolean
containsRole
(
String
role
,
Collection
<?
extends
GrantedAuthority
>
authorities
)
{
return
authorities
.
stream
().
anyMatch
(
a
->
isAuthorityEquals
(
role
,
a
.
getAuthority
()));
}
private
static
boolean
isAuthorityEquals
(
String
role
,
String
authority
)
{
return
StringUtils
.
equalsIgnoreCase
(
role
,
authority
)
||
StringUtils
.
equalsIgnoreCase
(
prepareRoleForCheck
(
role
),
authority
);
}
static
String
prepareRoleForCheck
(
String
role
)
{
if
((
Objects
.
nonNull
(
role
))
&&
(!
role
.
startsWith
(
ROLE_PREFIX
)))
{
return
ROLE_PREFIX
+
role
;
}
else
{
...
...
@@ -90,17 +92,19 @@ public class CurrentUserHelper {
}
}
public
static
boolean
containsRole
(
String
role
,
Collection
<?
extends
GrantedAuthority
>
authorities
)
{
return
authorities
.
stream
().
anyMatch
(
a
->
isAuthorityEquals
(
role
,
a
.
getAuthority
()
));
public
static
UserId
getCurrentUserId
(
)
{
return
UserId
.
from
(
getSubClaim
(
));
}
private
static
boolean
isAuthorityEquals
(
String
role
,
String
authority
)
{
String
roleToCheck
=
prepareRoleForCheck
(
role
);
return
StringUtils
.
equalsIgnoreCase
(
role
,
authority
)
||
StringUtils
.
equalsIgnoreCase
(
roleToCheck
,
authority
);
private
static
String
getSubClaim
()
{
return
((
Jwt
)
getAuthentication
().
getPrincipal
()).
getClaim
(
SUB_CLAIM_KEY
);
}
static
UserId
getCurrentUserId
()
{
return
UserId
.
from
(
get
Authentication
().
getName
(
));
public
static
Authentication
getAuthentication
()
{
return
find
Authentication
().
orElseThrow
(()
->
new
IllegalStateException
(
"No authenticated User found"
));
}
private
static
Optional
<
Authentication
>
findAuthentication
()
{
return
Optional
.
ofNullable
(
SecurityContextHolder
.
getContext
().
getAuthentication
()).
filter
(
TRUSTED
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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