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
c9746623
Commit
c9746623
authored
Oct 03, 2019
by
Jan Hrabal
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
ssh://git@git.janhrabal.com:4022/janh683/jh-boot.git
Conflicts: sql/data.sql
parents
bfafa441
6a278d16
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
61 additions
and
7 deletions
+61
-7
data.sql
sql/data.sql
+1
-2
JhSecurityConfig.java
src/main/java/com/jh/boot/security/JhSecurityConfig.java
+5
-0
SecurityHelper.java
src/main/java/com/jh/boot/security/SecurityHelper.java
+5
-1
AppRole.java
src/main/java/com/jh/boot/security/model/AppRole.java
+11
-0
AppUser.java
src/main/java/com/jh/boot/security/model/AppUser.java
+28
-0
AppUserAuthService.java
...java/com/jh/boot/security/service/AppUserAuthService.java
+11
-4
No files found.
sql/data.sql
View file @
c9746623
-- default admin user
insert
into
APP_ROLE
(
ID
,
NAME
)
values
(
1
,
'ADMIN'
);
insert
into
APP_ROLE
(
ID
,
NAME
)
values
(
1
,
'ROOT'
);
insert
into
APP_USER
(
ID
,
EMAIL
,
FIRST_NAME
,
LAST_NAME
,
PASSWORD
,
PASSWORD_SALT
,
LOCALE
,
TIMEZONE_ID
)
VAlUES
(
1
,
'janhrabal@seznam.cz'
,
'Jan'
,
'Hrabal'
,
'NKz1rz7kSEBXFGbolvEhdomvcDQYmD0IKGADVpuoxL1ztsE1NAnOMvbiSkvc3vwLQvBdPHw449XzgRderNJc9MMnnMTEgJdl3S6dtBGiEYDRHK19toXdwttfaDrXewjyZBJkzy7CxE/BOad4XkiTreIFAUGRedK9TGZ+RWbrJ2KIRrkSX3H1J2eT7HLF8bblkxz2qhjsF5s0k37e3sFI0xAdyCy6qAYS4/MW4WYQ3o0YyZc4krGE2k3y9kfPxWEh/favQKoFIX92ZkRh6ZIXNF7i4oUBl1pcg6r5ykCT83IAWm9avM768NEitEVOx0V8P0PQ2WxGA3n7nicKmwYjow=='
,
'GACR2Rea1kIhZAlImqK8HauZwTah5eMyKiTzr9HDriryN92YkE5UkWe3Gn7oRLkKEftaNfEfa2Ujj18Rrsed2a6QN69UZCkpRHnwgoBp5ckOOaC6s4undHSjYZW5rJx8CuKXTJpO1TS1LlsjwCyir8oA2gGm480jgGwOefm+r2s='
,
'en'
,
'UTC'
);
insert
into
APP_USER_ROLE
(
USER_ID
,
ROLE_ID
)
values
(
1
,
1
);
...
...
src/main/java/com/jh/boot/security/JhSecurityConfig.java
View file @
c9746623
...
...
@@ -94,4 +94,9 @@ public class JhSecurityConfig extends WebSecurityConfigurerAdapter {
return
new
AppUserRepository
();
}
// @Bean
// public GrantedAuthorityDefaults grantedAuthorityDefaults() {
// return new GrantedAuthorityDefaults(""); // Remove the ROLE_ prefix
// }
}
src/main/java/com/jh/boot/security/SecurityHelper.java
View file @
c9746623
...
...
@@ -3,6 +3,8 @@ package com.jh.boot.security;
import
java.util.Set
;
import
java.util.stream.Collectors
;
import
org.springframework.security.core.Authentication
;
import
com.jh.boot.security.model.AppRole
;
import
com.jh.boot.security.model.AppUser
;
...
...
@@ -13,7 +15,9 @@ public class 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
)
{
return
false
;
}
...
...
src/main/java/com/jh/boot/security/model/AppRole.java
View file @
c9746623
...
...
@@ -26,6 +26,17 @@ public class AppRole extends AbstractIdEntity {
public
AppRole
()
{
super
();
}
public
AppRole
(
String
name
)
{
super
();
this
.
name
=
name
;
}
public
String
getName
()
{
return
name
;
}
...
...
src/main/java/com/jh/boot/security/model/AppUser.java
View file @
c9746623
...
...
@@ -4,8 +4,11 @@
*/
package
com
.
jh
.
boot
.
security
.
model
;
import
java.util.Collections
;
import
java.util.Date
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.stream.Collectors
;
import
javax.persistence.Column
;
import
javax.persistence.Entity
;
...
...
@@ -13,8 +16,11 @@ import javax.persistence.JoinColumn;
import
javax.persistence.JoinTable
;
import
javax.persistence.ManyToMany
;
import
javax.persistence.Table
;
import
javax.persistence.Transient
;
import
javax.persistence.Version
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
import
com.fasterxml.jackson.annotation.JsonProperty
;
import
com.jh.boot.jpa.AbstractIdEntity
;
...
...
@@ -65,6 +71,7 @@ public class AppUser extends AbstractIdEntity {
inverseJoinColumns
=
@JoinColumn
(
name
=
"ROLE_ID"
,
referencedColumnName
=
"ID"
)
)
@JsonIgnore
private
Set
<
AppRole
>
roles
;
@Column
(
name
=
"LOCALE"
)
...
...
@@ -88,6 +95,19 @@ public class AppUser extends AbstractIdEntity {
@Column
(
name
=
"PASSWORD_CHANGED"
)
private
Date
passwordChanged
;
@Transient
@JsonProperty
(
"roles"
)
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.
*
...
...
@@ -278,4 +298,12 @@ public class AppUser extends AbstractIdEntity {
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 @
c9746623
...
...
@@ -2,10 +2,10 @@ package com.jh.boot.security.service;
import
java.util.Collection
;
import
java.util.Date
;
import
java.util.HashSet
;
import
java.util.Locale
;
import
java.util.Set
;
import
java.util.UUID
;
import
java.util.stream.Collectors
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -13,15 +13,16 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.security.authentication.BadCredentialsException
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.AuthenticationException
;
import
org.springframework.security.core.authority.SimpleGrantedAuthority
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.util.StringUtils
;
import
com.jh.boot.security.AppUserAuthentication
;
import
com.jh.boot.security.AuthService
;
import
com.jh.boot.security.AuthServiceListener
;
import
com.jh.boot.security.GrantedRole
;
import
com.jh.boot.security.PasswordHash
;
import
com.jh.boot.security.PasswordUtils
;
import
com.jh.boot.security.model.AppRole
;
import
com.jh.boot.security.model.AppUser
;
import
com.jh.boot.security.model.ResetPasswordToken
;
import
com.jh.boot.security.repository.AppUserRepository
;
...
...
@@ -54,9 +55,15 @@ public class AppUserAuthService implements AuthService {
user
.
setPasswordSalt
(
null
);
user
.
setVersion
(
null
);
Set
<
GrantedRole
>
roles
=
new
HashSet
<>();
//ADD implicit user
user
.
getRoles
().
add
(
new
AppRole
(
"USER"
));
//initialize roles
user
.
initRolesMap
();
Set
<
SimpleGrantedAuthority
>
authorities
=
user
.
getRoles
().
stream
().
map
(
r
->
new
SimpleGrantedAuthority
(
"ROLE_"
+
r
.
getName
())).
collect
(
Collectors
.
toSet
());
AppUserAuthentication
auth
=
new
AppUserAuthentication
(
user
,
authorities
);
AppUserAuthentication
auth
=
new
AppUserAuthentication
(
user
,
roles
);
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