开发者

How to work with Java Filters to redirect the user if is logued

I have in my application a few url:

/

/SignIn

/SignUp

In "/" is my homepage where the user can log in into my app.

I want like Gmail and other services that "/", "/SignUp", "/SignIn" will be available to the users not loged in. And when the user logs in into my application that if he goes to "/" or "/SignUp" or "/SignIn" redirect him into my app backend page "/Backend".

How can I deal with this with Java Filters?

Show I create one filter and for this 开发者_如何学Cfilter many mappings in this way?

<filter-mapping>
 <filter-name>AuthorizationFilter</filter-name>
 <url-pattern>/</url-pattern>

<filter-mapping>
     <filter-name>AuthorizationFilter</filter-name>
     <url-pattern>/SignIn</url-pattern>
  </filter-mapping>

etc?


A single filter which is mapped on /* is sufficient. Just check request URI in the filter itself.

Basic kickoff example:

if (path.equals("/") || path.equals("/SignIn") || path.equals("/SignUp")) {
    if (user == null) {
        chain.doFilter(request, response);
    } else {
        response.sendRedirect("/Backend");
    }
} else if (user != null) {
    chain.doFilter(request, response);
} else {
    response.sendRedirect("/SignIn"); // Or "/" or "/SignUp".
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜