就像标题中所说的那样,如何限制用户访问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");
}
}
特定页面访问
[在@PostConstruct
中或在viewAction
或initPreRenderView
方法中检查请求参数,因为在后两个方法中,您可以访问注入的视图参数。
如果用户没有足够的权限来访问数据重定向或/和显示面孔消息或执行其他操作。