如何获得在JSP / Servlet的用户角色 [英] How to get user roles in a JSP / Servlet

查看:130
本文介绍了如何获得在JSP / Servlet的用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法让一个String []与用户在JSP或Servlet的角色?

Is there any way to get a String[] with the roles a user has in the JSP or Servlet?

我知道request.isUserInRole(基于role1),但我也想知道用户的所有角色。

I know about request.isUserInRole("role1") but I also want to know all the roles of the user.

我搜索了servlet源,似乎这是不可能的,但是这似乎有些奇怪了我。

I searched the servlet source and it seems this is not possible, but this seems odd to me.

所以...任何想法?

推荐答案

阅读所有可能的角色,或硬code列表。然后迭代它运行的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);

这篇关于如何获得在JSP / Servlet的用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆