Commit 39d253af by Jan Hrabal

xwip

parent 6cfa95a8
package com.jh.boot.security; package com.jh.boot.security;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import com.jh.boot.security.model.AppRole;
import com.jh.boot.security.model.AppUser; import com.jh.boot.security.model.AppUser;
public class SecurityHelper { public class SecurityHelper {
public static final String ADMIN_ROLE = "ROLE_ADMIM";
private SecurityHelper() { private SecurityHelper() {
} }
public boolean hasAnyRole(Authentication auth, String...roles) {
public static AppUser getUser() {
return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
public static AppUser getUser(Authentication auth) {
if (auth == null) {
return null;
}
return (AppUser) auth.getPrincipal();
}
public static boolean hasAnyRole(Authentication auth, String...roles) {
AppUser user = auth instanceof AppUserAuthentication ? ((AppUserAuthentication) auth).getUser() : null; 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;
} }
Set<String> set = user.getRoles().stream().map(AppRole::getName).collect(Collectors.toSet());
if (set.contains(ADMIN_ROLE)) {
return true;
}
for (String role : roles) { for (String role : roles) {
if (set.contains(role)) { if (user.getRolesMap().containsKey(role)) {
return true; return true;
} }
} }
......
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