如果没有登录,如何通过URL阻止页面访问?

时间:2014-01-25 10:24:43

标签: java html spring-mvc

我已经用Java和Spring mvc实现了一个登录页面。 “的index.html” 现在的问题是你可以通过调用/person.html轻松绕过它,然后进入应用程序。

如何在不使用Spring Security的情况下防止这种情况发生?

1 个答案:

答案 0 :(得分:1)

非常基本的答案。

创建一个扩展HandlerInterceptorAdapter的拦截器类。在preHandle方法中,检查用户是否存在于会话中,并且仅当用户存在于会话中时才返回true。

public class AuthenticationCheckInterceptor extends HandlerInterceptorAdapter {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //Check if user exists in session. 
        //If no, redirect to login page using response.sendRedirect() and return false
        //If yes, return true
    }
}

将此拦截器应用于要求您登录的所有网址。

一个简单的例子是http://www.mkyong.com/spring-mvc/spring-mvc-handler-interceptors-example/