Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
jh-boot
Project
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
Jan Hrabal
jh-boot
Commits
6fc24eab
Commit
6fc24eab
authored
Sep 27, 2019
by
Jan Hrabal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
security
parent
c0d25a19
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
1 deletion
+34
-1
SecurityHelper.java
src/main/java/com/jh/boot/security/SecurityHelper.java
+5
-1
AppUser.java
src/main/java/com/jh/boot/security/model/AppUser.java
+26
-0
AppUserAuthService.java
...java/com/jh/boot/security/service/AppUserAuthService.java
+3
-0
No files found.
src/main/java/com/jh/boot/security/SecurityHelper.java
View file @
6fc24eab
...
@@ -3,6 +3,8 @@ package com.jh.boot.security;
...
@@ -3,6 +3,8 @@ package com.jh.boot.security;
import
java.util.Set
;
import
java.util.Set
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
import
org.springframework.security.core.Authentication
;
import
com.jh.boot.security.model.AppRole
;
import
com.jh.boot.security.model.AppRole
;
import
com.jh.boot.security.model.AppUser
;
import
com.jh.boot.security.model.AppUser
;
...
@@ -13,7 +15,9 @@ public class SecurityHelper {
...
@@ -13,7 +15,9 @@ public class SecurityHelper {
private
SecurityHelper
()
{
private
SecurityHelper
()
{
}
}
public
boolean
hasAnyRole
(
AppUser
user
,
String
...
roles
)
{
public
boolean
hasAnyRole
(
Authentication
auth
,
String
...
roles
)
{
AppUser
user
=
auth
instanceof
AppUserAuthentication
?
((
AppUserAuthentication
)
auth
).
getUser
()
:
null
;
if
(
user
==
null
||
user
.
getRoles
()
==
null
||
roles
==
null
||
roles
.
length
==
0
)
{
if
(
user
==
null
||
user
.
getRoles
()
==
null
||
roles
==
null
||
roles
.
length
==
0
)
{
return
false
;
return
false
;
}
}
...
...
src/main/java/com/jh/boot/security/model/AppUser.java
View file @
6fc24eab
...
@@ -4,8 +4,11 @@
...
@@ -4,8 +4,11 @@
*/
*/
package
com
.
jh
.
boot
.
security
.
model
;
package
com
.
jh
.
boot
.
security
.
model
;
import
java.util.Collections
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.Set
;
import
java.util.stream.Collectors
;
import
javax.persistence.Column
;
import
javax.persistence.Column
;
import
javax.persistence.Entity
;
import
javax.persistence.Entity
;
...
@@ -13,8 +16,10 @@ import javax.persistence.JoinColumn;
...
@@ -13,8 +16,10 @@ import javax.persistence.JoinColumn;
import
javax.persistence.JoinTable
;
import
javax.persistence.JoinTable
;
import
javax.persistence.ManyToMany
;
import
javax.persistence.ManyToMany
;
import
javax.persistence.Table
;
import
javax.persistence.Table
;
import
javax.persistence.Transient
;
import
javax.persistence.Version
;
import
javax.persistence.Version
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
import
com.jh.boot.jpa.AbstractIdEntity
;
import
com.jh.boot.jpa.AbstractIdEntity
;
...
@@ -65,6 +70,7 @@ public class AppUser extends AbstractIdEntity {
...
@@ -65,6 +70,7 @@ public class AppUser extends AbstractIdEntity {
inverseJoinColumns
=
inverseJoinColumns
=
@JoinColumn
(
name
=
"ROLE_ID"
,
referencedColumnName
=
"ID"
)
@JoinColumn
(
name
=
"ROLE_ID"
,
referencedColumnName
=
"ID"
)
)
)
@JsonIgnore
private
Set
<
AppRole
>
roles
;
private
Set
<
AppRole
>
roles
;
@Column
(
name
=
"LOCALE"
)
@Column
(
name
=
"LOCALE"
)
...
@@ -88,6 +94,18 @@ public class AppUser extends AbstractIdEntity {
...
@@ -88,6 +94,18 @@ public class AppUser extends AbstractIdEntity {
@Column
(
name
=
"PASSWORD_CHANGED"
)
@Column
(
name
=
"PASSWORD_CHANGED"
)
private
Date
passwordChanged
;
private
Date
passwordChanged
;
@Transient
private
Map
<
String
,
AppRole
>
rolesMap
;
public
void
initRolesMap
()
{
if
(
roles
==
null
)
{
rolesMap
=
Collections
.
emptyMap
();
return
;
}
rolesMap
=
roles
.
stream
().
collect
(
Collectors
.
toMap
(
AppRole:
:
getName
,
r
->
r
));
}
/**
/**
* Gets the email.
* Gets the email.
*
*
...
@@ -278,4 +296,12 @@ public class AppUser extends AbstractIdEntity {
...
@@ -278,4 +296,12 @@ public class AppUser extends AbstractIdEntity {
this
.
passwordChanged
=
passwordChanged
;
this
.
passwordChanged
=
passwordChanged
;
}
}
public
Map
<
String
,
AppRole
>
getRolesMap
()
{
return
rolesMap
;
}
public
void
setRolesMap
(
Map
<
String
,
AppRole
>
rolesMap
)
{
this
.
rolesMap
=
rolesMap
;
}
}
}
src/main/java/com/jh/boot/security/service/AppUserAuthService.java
View file @
6fc24eab
...
@@ -56,6 +56,9 @@ public class AppUserAuthService implements AuthService {
...
@@ -56,6 +56,9 @@ public class AppUserAuthService implements AuthService {
Set
<
GrantedRole
>
roles
=
new
HashSet
<>();
Set
<
GrantedRole
>
roles
=
new
HashSet
<>();
//initialize roles
user
.
initRolesMap
();
AppUserAuthentication
auth
=
new
AppUserAuthentication
(
user
,
roles
);
AppUserAuthentication
auth
=
new
AppUserAuthentication
(
user
,
roles
);
return
auth
;
return
auth
;
}
}
...
...
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