Easy login interception (direct access to the former not logged in Home jump to the login page)

Layer Controller code:
@Controller
public class LoginController {
    @RequestMapping("/login")
    public String login(String username, String password, HttpServletRequest request){
        if("zhangsan".equals(username) && "123".equals(password)){
            System.out.println("登录成功");
            request.getSession().setAttribute("username",username);
            return "redirect:/account/findAll";
        }else {
            System.out.println("登录失败");
            return "redirect:/login.jsp";
        }
    }
}
 
 
Interceptor layer code:
public  class LoginInterceptor the implements HandlerInterceptor from {
     / ** 
     * login authentication 
     * If the session has login information, release 
     * If the session information is not logged in to intercept 
     * is determined whether a login request, if the login request, the direct release 
     * / 
    @Override 
    public  Boolean The preHandle (the HttpServletRequest request, the HttpServletResponse Response, Object Handler) throws Exception {
         // Get request path 
        String requestURI = Request.getRequestURI ();
         // determine whether the registration request is 
        IF (requestURI.contains ( "Login" )) {
 //             If the login request , the direct release 
            return  to true; 
        } 
        // obtaining registration information from the session 
        Object username = Request.getSession () the getAttribute ( "username." );
         IF (! Username = null ) {
             // the session with a login information, release 
            return  to true ; 
        } the else {
             / / the session information is not logged in, jump to the login page 
            response.sendRedirect ( "/ the login.jsp" );
             return  false ; 
        } 
    } 

}
<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <!-- 引入CSS样式 -->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css">
</head>
<body>
<form class="form-horizontal" role="form" method="post" action="${pageContext.request.contextPath}/login">
    <div class="form-group">
        <label for="username" class="col-sm-2 control-label">用户名</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="username" name="username"
                   placeholder="请输入用户名">
        </div>
    </div>
    <div class="form-group">
        <label for="money" class="col-sm-2 control-label">密码</label>
        <div class="col-sm-10">
            <input type="password" class="form-control" id="money" name="password"
                   placeholder="请输入密码">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">登录</button>
        </div>
    </div></form>
</body>
<!-- 引入JS文件 -->
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>

</html>
login.jsp
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!--扫描包,创建类对象-->
    <context:component-scan base-Package = "com.itheima.controller"> </ context: Component-Scan> 
    ! <- view resolver -> 
    <the bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <Property name = "prefix" value = "/ Pages /"> </ Property> 
        <Property name = "suffix" value = "JSP."> </ Property> 
    </ the bean> 
    <- annotations driving ->! 
    <MVC: annotation -driven> </ MVC: Annotation-Driven> 

    <- interceptor ->! 
    <MVC: interceptors> 
        <MVC: interceptor> 
            <MVC: Mapping path = "/ **" /> 
            ! <- if blocked static resources, you need to configure release ->
            <mvc:exclude-mapping path="/js/*"></mvc:exclude-mapping>
            <mvc:exclude-mapping path="/css/*"></mvc:exclude-mapping>
            <mvc:exclude-mapping path="/fonts/*"></mvc:exclude-mapping>

            <bean class="com.itheima.interceptor.LoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>



    <!--静态资源放行-->
    <!--<mvc:resources mapping="/js/*" location="/js/"></mvc:resources>-->
    <!--静态资源全部放行-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
spring-mvc.xml

 

Guess you like

Origin www.cnblogs.com/xiaomingVVV/p/11332390.html