如何在 j_security_check 登录后重定向到 url

How to redirect to url after j_security_check login

我使用 j_security_check 验证我的 Java EE 网络应用程序。

我有2组;用户和管理员。这两个组在网页中都有自己的文件夹;用户有“/secure”,管理员有“/admin”。在一个人通过 j_security_check 身份验证后,我想根据他们的组重定向到他们关联的 Web 文件夹。现在他们只停留在登录页面。我的应用程序的身份验证部分工作正常,如果我以用户身份登录,我可以访问 /secure 但不能访问 /admin,反之亦然。

这可能吗?如何实现?我在网上找不到任何信息或解决方案。

编辑:也许是一些有用的解决方案信息,我也在我的应用程序中使用了 JSF。但我不使用任何 JSF 登录。登录表单是纯粹的 html,带有 j_security_check 内容。

编辑 2:我的主 Web 文件夹的根文件是登录页面。

您可以拥有一个纯 Web Servlet,它将处理一个通用的安全 /login 路径 (@WebServlet) between your different roles and redirect to the page based on the current authenticated user's role. You can use the HttpRequest.isUserInRole() method and then redirect your user to the page you want (HttpResponse.sendRedirect())

对于任何想知道我是如何解决它的人,我忘记在 web.xml 中指定角色,如下所示:

<security-role>
    <role-name>admin</role-name>
</security-role>
<security-role>
    <role-name>skipper</role-name>
</security-role>

感谢@Franck 花时间通过聊天解决这个问题!

怎么样: 使用以下内容创建 redirect.jsp 文件:

<%response.sendRedirect(request.getContextPath() + "/redirect");%>

然后将 redirect.jsp 设为 web.xml 中的欢迎文件:

<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>

最后一步是创建检查角色并适当重定向的 servlet