public static boolean hasRoleAuth(String roleName) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
GrantedAuthority[] ga=auth.getAuthorities();
for (int i = 0; i < ga.length; i++) {
GrantedAuthority authority = ga[i];
if(authority.getAuthority().equals(roleName))
return true;
}
return false;
}
public static boolean hasRoleAuth(String roleName) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
for (GrantedAuthority ga : auth.getAuthorities()) {
if (ga.getAuthority().equals(roleName)) {
return true;
}
}
return false;
}
---------------------------------------------------------------------------------------
public static Role getRole(){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
GrantedAuthority[] ga=auth.getAuthorities();
if(ga.length>0){
Role role=roleManager.getRoleByName(ga[0].getAuthority());
return role;
}
return null;
}
public static Role getRole() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Collection<? extends GrantedAuthority> ga = auth.getAuthorities();
if (ga.iterator().hasNext()) {
Role role = roleManager.getRoleByName(ga.iterator().next().getAuthority());
return role;
}
return null;
}
---------------------------------------------------------------------------------------
public static String[] getRoleNames(){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
GrantedAuthority[] ga=auth.getAuthorities();
String[] roleNames=new String[ga.length];
for (int i = 0; i < ga.length; i++) {
GrantedAuthority authority = ga[i];
roleNames[i]=authority.getAuthority();
}
return roleNames;
}
public static String[] getRoleNames() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Collection<? extends GrantedAuthority> collection = auth.getAuthorities();
String[] roleNames = new String[collection.size()];
int i = 0;
for (GrantedAuthority g : collection) {
roleNames[i++] = g.getAuthority();
}
return roleNames;
}
Java集合容器优化
最新推荐文章于 2023-03-09 13:55:37 发布