如何检查使用过滤器授权的用户 [英] How to check user authorized with filter

查看:53
本文介绍了如何检查使用过滤器授权的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个过滤器和登录 Servlet.我如何检查 - 授权用户与否?如果没有授权 - 将他重定向到登录 Servlet.

I have a filter and Login Servlet. How i can check - authorized user or not? and if not authorized - to redirect him to Login Servlet.

谢谢.

推荐答案

这样做:

  1. 当用户登录时,在 HttpSession 中为该用户设置 User 对象.这样,httpRequest.getSession().setAttribute("LOGGED_USER", userObject)

现在,每次您点击过滤器/安全过滤器.您要做的第一件事是检查此属性.

Now, every time you hit the filter/security filter. The first thing you do is check for this attribute.

如果该属性不存在,则将请求重定向/转发到登录 servlet.

If the attribute is not there, redirect/forward the request to login servlet.

伪代码如下所示:

//in your login servlet, on successful login
request.getSession().setAttribute("LOGGED_USER", userObject);

//in your security filter
if(request.getSession().getAttribute("LOGGED_USER") == null){
//optionally, you may like to check if that attribute has a valid userId as well
     RequestDispatcher rd = request.getRequestDispatcher("relative/path/to/login/servlet")
     rd.forward(request, response);
     return;
}

<小时>

编辑 1: 看到这个 http://download.oracle.com/javaee/5/tutorial/doc/bncbx.html

这篇关于如何检查使用过滤器授权的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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