Commit c9746623 by Jan Hrabal

Merge branch 'master' of

parents bfafa441 6a278d16
-- 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);
......
......@@ -94,4 +94,9 @@ public class JhSecurityConfig extends WebSecurityConfigurerAdapter {
return new AppUserRepository();
}
// @Bean
// public GrantedAuthorityDefaults grantedAuthorityDefaults() {
// return new GrantedAuthorityDefaults(""); // Remove the ROLE_ prefix
// }
}
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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;
}
}
......@@ -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;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment