簡易登錄攔截(沒有登錄前直接訪問主頁則跳轉到登錄頁)


Controller層中代碼:
@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層中代碼:
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 登錄驗證
     *      如果session有登錄信息,放行
     *      如果session沒有登錄信息,攔截
     * 判斷是否是登錄請求,如果登錄請求,直接放行
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //獲取請求路徑
        String requestURI = request.getRequestURI();
        //判斷是否是登錄請求
        if(requestURI.contains("login")){
//            如果是登錄請求,直接放行
            return true;
        }
        //從session中獲取登錄信息
        Object username = request.getSession().getAttribute("username");
        if(username != null){
            //session中有登錄信息,放行
            return true;
        }else{
            //session沒有登錄信息,跳轉到登錄頁面
            response.sendRedirect("/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>
    <!--視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!--注解驅動-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--攔截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <!--如果攔截了靜態資源,需要配置放行-->
            <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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM