【问题标题】:Do some check before every page is loaded and redirect to another page在加载每个页面并重定向到另一个页面之前进行一些检查
【发布时间】:2013-08-02 19:00:43
【问题描述】:

我需要在加载每个页面之前进行一些检查,看看是否需要将用户重定向到另一个页面(出于安全原因)。

当我使用 JSF 2.0 时,我使用了一个阶段监听器来完成这项工作。现在我使用的是 JSF 2.2,而且我所有的 bean 都不再是 JSF bean,而是 CDI bean,我认为我提供了更好的选择来执行此操作(或不执行此操作?)。

我听说过viewAction 事件,但我不想在每个页面上都重复元数据(除非没有其他选择)。

那么在 JSF 2.2 中使用 CDI 实现此场景的最佳方法是什么?

更新(在@skuntsel 建议之后)

这是我现在使用的过滤器。我只想在身份验证后使用它来简化它的代码。顺便说一句,如果您发现其中有任何错误,请告诉我。

@WebFilter("/*")
public class SolicitacoesFilter implements Filter
{
    // I can't just use @Inject private User _user, because it needs to be initialized
    // only when the user is authenticated. Otherwise an exception is thrown. If this
    // filter was called only after the authentication I could use the mentioned code.
    private User _user;

    @Inject
    private Instance<User> _userGetter;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {
        if (initializeUser(request))
        {
            if (_user.isProvisoryPassword())
            {
                // Redirect to another page...
                return;
            }
            if (_user.getStatus() != Status.ACTIVE)
            {
                // Redirect to another page...
                return;
            }
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy()
    {
    }

    private boolean initializeUser(ServletRequest request)
    {
        boolean userAuthenticated = ((HttpServletRequest) request).getUserPrincipal() != null;
        if (userAuthenticated)
        {
            if (_user == null)
            {
                _user = _userGetter.get();
            }
        }
        else
        {
            _user = null;
        }
        return _user != null;
    }
}

【问题讨论】:

  • 在您提到的两个 JSF 版本中,Web 过滤器是首选方式!
  • 我有一个代表用户的会话范围 CDI bean。我需要访问它才能进行检查。那么,我可以从过滤器中访问这个 CDI bean 吗?过滤器是否允许注入?
  • @WebFilter 是通过@Inject 对 CDI bean 的有效注入目标,但如果不是,数据当然可以通过 HTTP 会话的属性映射访问。相关:stackoverflow.com/questions/7815308/….

标签: java jsf jsf-2.2


【解决方案1】:

好的,你需要重定向的目的是什么?

  • 如果是关于检查会话用户以进行身份​​验证,请使用过滤器:

假设在有登录表单:http://www.domain.com/login.jsf。 一旦用户触发连接按钮,我们希望将他重定向到http://www.domain.com/member/welcome.jsf,并避免其他人不访问member/welcome.jsf 域,我是指http://www.domain.com/member/ 中的所有页面....

这里是一个简单的设计:

@WebFilter("/member/*")  
public class SecurityCheck implements Filter {

public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest req, ServletResponse res,
    FilterChain chain) throws ServletException, IOException {

  HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    HttpSession session = request.getSession();

    if (session == null || session.getAttribute("User") == null) {
        response.sendRedirect(request.getContextPath() + "/index.xhtml"); // No logged-in user found, so redirect to login page.
    } else {
        chain.doFilter(req, res); // Logged-in user found, so just continue request.
    }

}

@Override
public void destroy() {
// Cleanup global variables if necessary.
}
  • 其他情况,使用:

    <h:link></h:link>,or <h:commandLink></h:commandLink> // Checking in the managed Beans method
    
  • 你也可以xml文件,进行重定向。

【讨论】:

  • 不,它是用于授权 porpuses,而不是身份验证。因此,过滤器检查仅在用户通过身份验证后才有意义(我正在使用基于 FORM 的身份验证)。所以我不得不在过滤器中使用这样的代码来查看用户是否经过身份验证:boolean authenticated = ((HttpServletRequest) request).getUserPrincipal() != null。顺便说一句,有没有办法仅在用户已经通过身份验证时才激活过滤器?如果我们也可以为过滤器指定一个 excluded url 模式,那就太好了。
  • -“所以过滤器检查只有在用户通过身份验证后才有意义”,过滤器还可以避免未经身份验证的人访问某些特定页面。 - “有没有办法仅在用户已经通过身份验证时才激活过滤器?”,对于哪些用户角色?您重定向需要的目的是什么?
  • 我不必担心未经身份验证的人访问某些页面,因为正如我所说,我的 Web 应用程序配置为使用 基于 FORM 的身份验证,因此 Web 容器会自动为我处理这个问题。这就是为什么我想将过滤器配置为仅在用户通过身份验证时调用。这样我就不需要检查用户是否在过滤器中通过了身份验证。
  • 好的,我的重定向需要(使用经过身份验证的用户):(1) 检查用户是否已经更改了他的临时密码(第一次为他生成的密码) .如果没有,请重定向到另一个页面以允许他更改密码。 (2) 检查用户状态。如果他的状态不正常,请重定向到另一个页面。所有这些仅对经过身份验证的用户才有意义。
  • 我刚刚告诉你我的重定向需求。
猜你喜欢
  • 2017-06-08
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 2016-12-14
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
相关资源
最近更新 更多