如何限制用户访问 JSF 中的某些页面?

How do I limit a user from accesing sertain pages in JSF?

如标题所说,如何限制用户访问JSF中的某些页面?我有两种不同类型的页面,我想限制访问。第一个是需要加载参数的页面,如果用户试图在没有任何参数的情况下重定向访问该页面,是否可以重定向?第二个是只有特定用户才能访问的页面。在我的应用程序中,您可以创建和编辑比赛,但是,我只希望活动的主持人能够访问该活动的编辑页面——目前任何人只要知道正确的参数就可以访问。 JSF 中有什么东西可以让我这样做吗?

一般页面访问

看看 @WebFilter 及其 doFilter 方法。在里面你可以检查你的用户是否登录从 HttpSession.

检索你的会话作用域 bean
@WebFilter(filterName = "UserAuthenticationFilter", urlPatterns =
{
    "/sites/user/account.xhtml"
}   , dispatcherTypes =
{
    DispatcherType.FORWARD, DispatcherType.REQUEST, DispatcherType.ERROR
})
public class UserAuthenticationFilter extends HttpFilter
{
    @Override
    public void doProductionFilter(final HttpServletRequest request, final HttpServletResponse response, final HttpSession session, final FilterChain chain) throws IOException, ServletException
    {
        final UserBean userBean = session.getAttribute("userBean");


        // check if logged in and redirect to login page if not
        if (userBean.isLoggedIn()
            chain.doFilter(request, response);
        else
            response.sendRedirect(request.getContextPath() + "/login.xhtml");
    }
}

特定页面访问

@PostConstructviewActioninitPreRenderView 方法中检查您的请求参数,因为在后两种方法中您可以访问注入的视图参数。

如果用户没有足够的权限访问数据重定向or/and显示面孔消息或执行其他操作。