成功登录后如何设置重定向页面(没有Spring Security)

时间:2022-10-20 10:46:37

First, my loginInterceptor looks like this:

首先,我的loginInterceptor看起来像这样:

public class LoginInterceptor extends HandlerInterceptorAdapter{
private static final Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);

@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception{
        if (req.getSession().getAttribute("loginId") == null) {
            res.sendRedirect("signin");
            return false;
        }
    return true;
  }
}

This only redirects to sign in page if the user is not signed in. Now I want to redirect to a different page if someone successfully signs in, without using Spring Security. I first googled and it only showed me Spring Security but I need to do this without using Spring Security. I tried else statement but nothing new happened. Is there anything I can do to achieve what I want? Thanks in advance.

如果用户未登录,则仅重定向到登录页面。现在,如果有人成功登录,我想重定向到其他页面,而不使用Spring Security。我第一次使用Google搜索,它只向我展示了Spring Security,但我需要在不使用Spring Security的情况下执行此操作。我尝试了其他声明,但没有发生新的事情有什么我可以做到实现我想要的东西吗?提前致谢。

I'm using Spring MVC + Java.

我正在使用Spring MVC + Java。

2 个解决方案

#1


1  

As per the javadocs

按照javadocs

Returns: true if the execution chain should proceed with the next interceptor or the handler itself. Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself.

返回:如果执行链应继续执行下一个拦截器或处理程序本身,则返回true。否则,DispatcherServlet假定这个拦截器已经处理了响应本身。

So in you else code it should return false

所以在你的其他代码中它应该返回false

#2


0  

Return true - "OK proceed"

返回true - “确定继续”

Return false - "Stop proceeding" (Then you have to have a redirect, otherwise it will just stop I think)

返回false - “停止继续”(然后你必须有一个重定向,否则它会停止我认为)

If you need to redirect always from your LoginInterceptor:preHandle the try something like below

如果您需要始终从LoginInterceptor重定向:preHandle尝试类似下面的内容

  public boolean preHandle(...){
        if (req.getSession().getAttribute("loginId") == null) {
            res.sendRedirect("signin");
        }else{
            res.sendRedirect("home");//You have already logged in
        }

     return false; //Spring stops interceptor channing
  }

There is a problem with above code which is it will always redirect to "home" for a logged in user even for a different URL pattern which intercept LoginInterceptor URL pattern.

上面的代码存在一个问题,即使对于拦截LoginInterceptor URL模式的不同URL模式,它总是会重定向到登录用户的“home”。

I think your posted code is fine and there is something wrong somewhere else.

我认为你发布的代码很好,其他地方也有问题。

#1


1  

As per the javadocs

按照javadocs

Returns: true if the execution chain should proceed with the next interceptor or the handler itself. Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself.

返回:如果执行链应继续执行下一个拦截器或处理程序本身,则返回true。否则,DispatcherServlet假定这个拦截器已经处理了响应本身。

So in you else code it should return false

所以在你的其他代码中它应该返回false

#2


0  

Return true - "OK proceed"

返回true - “确定继续”

Return false - "Stop proceeding" (Then you have to have a redirect, otherwise it will just stop I think)

返回false - “停止继续”(然后你必须有一个重定向,否则它会停止我认为)

If you need to redirect always from your LoginInterceptor:preHandle the try something like below

如果您需要始终从LoginInterceptor重定向:preHandle尝试类似下面的内容

  public boolean preHandle(...){
        if (req.getSession().getAttribute("loginId") == null) {
            res.sendRedirect("signin");
        }else{
            res.sendRedirect("home");//You have already logged in
        }

     return false; //Spring stops interceptor channing
  }

There is a problem with above code which is it will always redirect to "home" for a logged in user even for a different URL pattern which intercept LoginInterceptor URL pattern.

上面的代码存在一个问题,即使对于拦截LoginInterceptor URL模式的不同URL模式,它总是会重定向到登录用户的“home”。

I think your posted code is fine and there is something wrong somewhere else.

我认为你发布的代码很好,其他地方也有问题。