本文介绍了如何在spring mvc拦截器中验证会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是spring framework的新手。在我的代码中,我使用拦截器检查会话是否存在。如果会话存在,我允许调用控制器,否则我重定向登录页面。
下面是我的代码。

  @Override 
public boolean preHandle(HttpServletRequest request,HttpServletResponse response,



用户user =(用户)session.getAttribute(user);
if(user == null)
{
System.err.println(Request Path:);
response.sendRedirect(index);
return false;
}
else
{
返回true;
}
}

但此代码未成功重定向。我收到以下错误,

 在Mozilla中我得到以下错误
页面没有正确重定向
在chorme我得到以下错误?
这个网页有重定向循环

如何修复这个问题?任何帮助将不胜感激!!!

解决方案

只是一个疯狂的猜测,因为你忘了说你的拦截器已配置。我认为这可能是由拦截器应用于登录页面索引



如果这是真的任何页面将要求浏览器重定向到 index 页面,但 index 页面本身将向浏览器发送重定向请求。 / p>

正确的方法是配置拦截器以忽略登录页面

  @Override 
public boolean preHandle(HttpServletRequest request,HttpServletResponse response,

// ignore login page
if(request.getServletPath()==/ index){/ / BEWARE:适应您的实际登录页面
返回true;
}

用户user =(用户)session.getAttribute(user);
if(user == null)
{
System.err.println(Request Path:);
response.sendRedirect(index);
return false;
}
其他
{
返回true;
}
}

您还可以使用SpringMVC配置来获取拦截器应用于登录页面



但无论如何,如果你想构建一个认真的应用程序,我的建议是看看很好地集成在一个Spring MVC应用程序中,并带有一堆示例来避免上述问题(以及其他......)


I am newbie to spring framework.In my code,i use the interceptor for checking the session exists or not.If session exists i allow to call the controller otherwise i redirect the login page.Below is my code.

@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,



        Users user=(Users) session.getAttribute("user");
        if(user == null)
        {
            System.err.println("Request Path : ");
            response.sendRedirect("index");
            return false;
        }
        else
        {
            return true;
        }
    }

but this code not redirect successfully.I am getting the below error,

In Mozilla i get below error
    The page is not redirecting properly
In chorme i get below error?
    This web page has redirect loop

How to fix this issue?Any help will be greatly appreciated!!!

解决方案

Just a wild guess, because you forgot to say how your interceptor is configured. I think it could be caused by the interceptor being applied to the login page index.

If this is true any page will ask the browser to redirect to index page, but index page itself will send a redirect request to browser.

The correct way is to configure the interceptor to ignore the login page

@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,

        // ignore login page
        if (request.getServletPath() == "/index") { // BEWARE : to be adapted to your actual login page
            return true;
        }

        Users user=(Users) session.getAttribute("user");
        if(user == null)
        {
            System.err.println("Request Path : ");
            response.sendRedirect("index");
            return false;
        }
        else
        {
            return true;
        }
    }

You could also use SpringMVC configuration to have the interceptor not applied to the login page

But anyway, if you want to build a serious application, my advice is to have a look to Spring Security that nicely integrates in a Spring MVC application and comes with a bunch of examples to avoid above problem (and others ...)

这篇关于如何在spring mvc拦截器中验证会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:05
查看更多