0

我在 tomcat 中使用基于表单的身份验证,并且实际上正在使用 j_security_check。代码如下

<form method="POST" action="j_security_check">
<table>
    <tr>
        <td colspan="2">Login to the Tomcat-Demo application:</td>
    </tr>
    <tr>
        <td>Name:</td>
        <td><input type="text" name="j_username" /></td>
    </tr>
    <tr>
        <td>Password:</td>
        <td><input type="password" name="j_password"/ ></td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit" value="Go" /></td>
    </tr>
</table>
</form>

我想使用已登录报告的用户。我想从会话中捕获已登录的用户。我的想法是使用下面的jsp语法来设置变量

<%
        session.setAttribute("myName",request.getParameter("user"));
%>

但是,我不确定如何设置这样的会话变量,因为在身份验证后不久,它就会被重定向到请求的 URL

有人可以帮忙吗

问候阿里夫

4

2 回答 2

0

通常通过过滤器解决。检查 tomcat 文档中的过滤器,并应用您的过滤器(可能匹配每个请求,因为您无法应用它。google 关键字 session、filter、principal。

dofilter() {
    getSession;
    if(session->user is not set) {
        session->user = request->getPrincipal->getUsername() //something like that
    }
}
于 2014-06-25T11:19:53.373 回答
0

一旦通过身份验证,您可以通过调用从请求中获取相应的用户名:

request.getRemoteUser()

您还可以通过调用来测试用户是否具有特定角色

request.isUserInRole()

这可以在您可以访问 HttpServletRequest 的任何地方完成

有关更多详细信息,请参阅 servlet API。

于 2014-06-25T12:24:13.137 回答