The previous version was v0.2.1, the keep-on-login version. This time, V0.2.2 is an update to the validating login process, using interceptors and filters, and found that this approach omitted some logical implementation

Registration-login-interface2

Version 0.2.2

The new functions are modified on the basis of V0.2.1, delete some code

You can view the previous code by clicking on tag:

1. New functions

  • Use MVC interceptors to intercept specific pages and jump to the login screen

  • Use filters to intercept the same functionality

2. Deleted code

Delete the statusException method in ExceptionService, delete the corresponding implementation, and then delete the code calling it in UserServiceImpl and Controller

In this way, it becomes not checking whether the user is logged in

3. The interceptor

First, create a new package: Interceptor

Create a new class: LoginInterceptor, which implements the HandlerInterceptor interface:

The use of the HandlerInterceptor is not covered here. If you don’t get the username attribute in the session, you jump to the login page

Blocking is scoped, and you configure the pages you want to block in the configuration file below

    public class LoginInterceptor implements org.springframework.web.servlet.HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest,
                             HttpServletResponse httpServletResponse, Object o) throws Exception {
        HttpSession session = httpServletRequest.getSession();
        if (session.getAttribute("username") == null){
            httpServletResponse.sendRedirect("preLogin.action");
        } else {
            return true;
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                           Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
Copy the code

Add the interceptor configuration to spring-mVC.xml:

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/userStatus.action"/>
            <mvc:mapping path="/showInfo.action"/>
            <mvc:mapping path="/setUserInfo.action"/>
            <bean class="interceptor.LoginInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
Copy the code

The LoginInterceptor provides a mapping for the specified path, and then specifies the interceptor location

The effect

After running, type in the browser address bar

  • localhost:8080/userStatus.action
  • localhost:8080/showInfo.action
  • localhost:8080/setUserInfo.action

Will jump to http://localhost:8080/preLogin.action

The interceptor part is over

4. The filter

Javax.servlet.filter: javax.servlet.filter: javax.servlet.filter: javax.servlet.filter: javax.servlet.filter: javax.servlet.filter: javax.servlet.filter: javax.servlet.filter: javax.servlet.filter: javax.servlet.filter

public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, 
                         FilterChain filterChain) throws IOException, ServletException {
        if (((HttpServletRequest) servletRequest).getSession().
                getAttribute("username") == null){
            ((HttpServletResponse)servletResponse).sendRedirect("preLogin.action");
        } else{ filterChain.doFilter(servletRequest, servletResponse); }}}Copy the code

Filterchain.dofilter (servletRequest, servletResponse); filterchain.dofilter (servletRequest, servletResponse); Is must write, if you do not write, after login, you can not access those who need to login to access the page, equivalent to the request has been interrupted filter, if detected has logged in, will add this line of code to position personal information page

Then add a filter configuration to web.xml:

    <filter>
        <filter-name>loginFilter</filter-name>
        <filter-class>filter.LoginFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>loginFilter</filter-name>
        <url-pattern>/showInfo.action</url-pattern>
        <url-pattern>/userStatus.action</url-pattern>
        <url-pattern>/setUserInfo.action</url-pattern>
    </filter-mapping>
Copy the code

At this point, we are done authenticating login methods that are different from V0.2.1