如何重定向到另一页的时候就已经验证的用户访问登陆页面 [英] How to redirect to another page when already authenticated user accesses login page

查看:191
本文介绍了如何重定向到另一页的时候就已经验证的用户访问登陆页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能将用户重定向如果某 C:?如果 clausule是真正

  c为C:如果测试=#{} loginController.authenticated>
 //重定向到索引页
< / C:如果>


解决方案

是的,它是可能的。

不过,我建议你为/login.jsp和正向过滤到其他网页应用过滤器,如果用户已经登录。

下面是告诉你如何做到这一点使用过滤器的例子:

 公共类LoginPageFilter实现过滤器
{
   公共无效的init(一个FilterConfig一个FilterConfig)抛出ServletException异常
   {   }   公共无效的doFilter(ServletRequest中的servletRequest,ServletResponse的ServletResponse的,FilterChain filterChain)抛出IOException异常,ServletException异常
   {
       HttpServletRequest的请求=(HttpServletRequest的)的servletRequest;
       HttpServletResponse的响应=(HttpServletResponse的)ServletResponse的;       如果(request.getUserPrincipal()!= NULL){//如果用户已通过身份验证
           response.sendRedirect(/ index.jsp的); //或者使用转发的RequestDispatcher
       }其他{
           filterChain.doFilter(的servletRequest,ServletResponse的);
       }
   }   公共无效的destroy()
   {   }
}

在web.xml中添加此过滤器enty

 <滤光器>
    <过滤器名称>&LoginPageFilter LT; /过滤器名称>
    <滤波级>
        com.sample.LoginPageFilter
    < /过滤器类>
    <初始化参数>
       <参数-名称>测试参数< /参数 - 名称>
       <参数值>这参数用于测试和LT; /参数值>
    < /初始化参数>
< /滤光器><过滤器映射>
    <过滤器名称>&LoginPageFilter LT; /过滤器名称>
    &LT; URL模式&GT; /login.jsp< / URL模式&GT;
&LT; /过滤器映射&GT;

I was wondering if it was possible to redirect users if a certain c:if clausule is true?

<c:if test="#{loginController.authenticated}">
 //redirect to index page
</c:if>

解决方案

Yes it is possible.

But, I would suggest you to apply filter for /login.jsp and in the filter forward to the other page if the user has already logged in.

Here is the example which tells how to do this using filter:

public class LoginPageFilter implements Filter
{
   public void init(FilterConfig filterConfig) throws ServletException
   {

   }

   public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,   FilterChain filterChain) throws IOException, ServletException
   {
       HttpServletRequest request = (HttpServletRequest) servletRequest;
       HttpServletResponse response = (HttpServletResponse) servletResponse;

       if(request.getUserPrincipal() != null){ //If user is already authenticated
           response.sendRedirect("/index.jsp");// or, forward using RequestDispatcher
       } else{
           filterChain.doFilter(servletRequest, servletResponse);
       }
   }

   public void destroy()
   {

   }
}

Add this filter enty in the web.xml

<filter>
    <filter-name>LoginPageFilter</filter-name>
    <filter-class>
        com.sample.LoginPageFilter
    </filter-class>
    <init-param>
       <param-name>test-param</param-name>
       <param-value>This parameter is for testing.</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>LoginPageFilter</filter-name>
    <url-pattern>/login.jsp</url-pattern>
</filter-mapping>

这篇关于如何重定向到另一页的时候就已经验证的用户访问登陆页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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