Java 获取wso2中用户的角色

Java 获取wso2中用户的角色,java,jsp,wso2,Java,Jsp,Wso2,我使用wso2im创建了用户并为他们分配了不同的角色。使用这些工具,我成功地限制了对.jsp文件的访问,因此角色似乎工作正常 问题在于,当我需要在同一个JSP中向不同的角色显示不同的内容时(例如,角色AAA可以执行xxx和yyy,角色BBB可以执行zzz),我试图使用request.isUserInRole(“角色”)检查角色,但它总是返回null,在尝试从.jsp本身和处理身份验证的servlet进行验证时。最终成功地使其工作。使用servlet获取角色并将其存储在cookie中。既不安全也不

我使用wso2im创建了用户并为他们分配了不同的角色。使用这些工具,我成功地限制了对.jsp文件的访问,因此角色似乎工作正常


问题在于,当我需要在同一个JSP中向不同的角色显示不同的内容时(例如,角色AAA可以执行xxx和yyy,角色BBB可以执行zzz),我试图使用request.isUserInRole(“角色”)检查角色,但它总是返回null,在尝试从.jsp本身和处理身份验证的servlet进行验证时。

最终成功地使其工作。使用servlet获取角色并将其存储在cookie中。既不安全也不漂亮,但工作:

package-foo;
导入java.io.IOException;
导入javax.servlet.ServletException;
导入javax.servlet.annotation.WebServlet;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入java.io.PrintWriter;
导入javax.servlet.RequestDispatcher;
导入javax.servlet.http.Cookie;
导入javax.servlet.http.HttpSession;
导入org.apache.axis2.transport.http.HttpTransportProperties;
导入org.apache.axis2.client.Options;
导入org.apache.axis2.transport.http.HTTPConstants;
导入org.wso2.carbon.um.ws.api.stub.RemoteUserStoreManagerServiceStub;
/**
*Servlet实现类LoginServlet
*/
@WebServlet(“/LoginServlet”)
公共类LoginServlet扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
私有最终字符串basicAuthUserID=“admin”;
私有最终字符串basicAuthPassword=“admin”;
私有最终字符串serverUrl=”https://localhost:9444/services/";
私有RemoteUserStoreManagerServiceStub存根=null;
受保护的void doPost(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
//获取用户ID和密码的请求参数
字符串user=request.getParameter(“用户”);
字符串pwd=request.getParameter(“pwd”);
试一试{
if(验证(用户,pwd)){
HttpSession session=request.getSession();
session.setAttribute(“用户”,user);
//将会话设置为30分钟后到期
session.setMaxInactivativeInterval(30*60);
Cookie用户名=新Cookie(“用户”,用户);
userName.setMaxAge(30*60);
字符串[]角色=GetRoleListFuser(用户);
字符串rolesTodos=null;
for(字符串s:角色){
如果(!s.equals(“内部/所有人”)){
if(rolesTodos==null){
Rolestos=s;
}否则{
//System.out.println(“Rol:+s”);
rolesTodos=rolesTodos+“,”+s;
}
}
}
//System.out.println(“角色:“+rolesTodos”);
Cookie rolesCookie=新Cookie(“角色”,rolesTodos);
rolesCookie.setMaxAge(30*60);
addCookie(用户名);
回答:addCookie(rolesCookie);
sendRedirect(“index.jsp”);
}否则{
RequestDispatcher rd=getServletContext().getRequestDispatcher(“/login.html”);
PrintWriter out=response.getWriter();
println(“用户名或密码错误。”);
包括(请求、响应);
}
}捕获(例外e){
e、 printStackTrace();
}
}
私有布尔身份验证(字符串用户名、对象凭据)引发异常{
如果(!(凭证实例字符串)){
抛出新异常(“不支持的密码类型”);
}
试一试{
if(存根==null){
存根=新的RemoteUserStoreManagerServiceStub(空,服务器URL
+“RemoteUserStoreManagerService”);
HttpTransportProperties.Authenticator basicAuth=新的HttpTransportProperties.Authenticator();
basicAuth.setUsername(basicAuthUserID);
basicAuth.setPassword(basicAuthPassword);
basicAuth.setPreemptiveAuthentication(真);
最终选项clientOptions=stub._getServiceClient().getOptions();
setProperty(HTTPConstants.AUTHENTICATE,basicAuth);
存根。\u getServiceClient().setOptions(clientOptions);
}
返回存根。身份验证(用户名,(字符串)凭证);
}捕获(例外e){
handleException(e.getMessage(),e);
}
返回false;
}
私有字符串[]handleException(字符串消息,异常e)引发异常{
System.out.println(e.getMessage()+e);
抛出新异常(msg,e);
}
公共字符串[]GetRoleListFuser(字符串用户名)引发异常{
试一试{
返回stub.getRoleListOfUser(用户名);
}捕获(例外e){
System.out.println(e.getMessage()+e);
}
返回null;
}
}