第一步:写一个登录页面 用来访问
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/ul/main2"method="post">
用户名:<input type="text" name="username"/>
密码:<input type="password" name="pwd"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
第二步:写一个首页的页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<h1>首页</h1>
${username}
<a href="${pageContext.request.contextPath}/ul/zhu">注销</a>
</body>
</html>
第三步:写一个controller控制层
package com.aaa.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/ul")
public class MyController {
//跳转到首页
@RequestMapping("/mains")
public String main(){
return "main";
}
//跳转到登录页面
@RequestMapping("/logins")
public String deng(){
return "login";
}
//登录页面之后的操作
@RequestMapping("/main2")
public String caoZuo(HttpSession session, String username, String pwd,Model model){
session.setAttribute("userInfo",username);
model.addAttribute("username",username);
return "main";
}
//注销操作返回登录页面
@RequestMapping("/zhu")
public String zhuXiao(HttpSession session){
session.removeAttribute("userInfo");
return "login";
}
}
第四步:自定义一个拦截器,实现他的方法
package com.aaa.Interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session=request.getSession();
//放行;判断在什么情况下登录
//登录页面放行
if (request.getRequestURI().contains("logins")){
return true;
}
if (request.getRequestURI().contains("main2")){
return true;
}
if( session.getAttribute("userInfo")!=null){
return true;
}
//判断在什么情况下没有登录
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
return false;
}
}
第五步:在spring中注册他的拦截器
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启静态资源过滤-->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<!--注册视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--扫描包-->
<context:component-scan base-package="com.aaa.controller"/>
<!--注册拦截器-->
<!--配置拦截器 运行了aop的思想,切入到这个类中-->
<mvc:interceptors>
<mvc:interceptor>
<!--
/** 拦截这个文件夹下的所有 /admin/ad/dd
/* 拦截当前文件夹 /admin
-->
<mvc:mapping path="/**"/>
<bean class="com.aaa.Interceptor.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<mvc:interceptors>
<mvc:interceptor>
<!--
/** 拦截这个文件夹下的所有 /admin/ad/dd
/* 拦截当前文件夹 /admin
-->
<mvc:mapping path="/ul/**"/>
<bean class="com.aaa.Interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>