且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 JSP/Servlet 中获取用户角色

更新时间:2023-02-12 19:41:20

读入所有可能的角色,或硬编码一个列表.然后运行 ​​isUserInRole 对其进行迭代并构建用户所在角色的列表,然后将该列表转换为数组.

Read in all the possible roles, or hardcode a list. Then iterate over it running the isUserInRole and build a list of roles the user is in and then convert the list to an array.

String[] allRoles = {"1","2","3"};
HttpServletRequest request = ... (or from method argument)
List userRoles = new ArrayList(allRoles.length);
for(String role : allRoles) {
 if(request.isUserInRole(role)) { 
  userRoles.add(role);
 }
}

// I forgot the exact syntax for list.toArray so this is prob wrong here
return userRoles.toArray(String[].class);