登陆拦截案例
登陆拦截器小案例,判断登陆的用户名及密码是否正确;:
1.创建一个maven项目,导入相关的坐标:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!--插入tomcat7插件,内置tomcat-->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
将打包方式设置为war:
<packaging>war</packaging>
2.设置打包方式之后打开项目的模块设置,添加webapp和WEB-INF
得出项目结构:
3.在resources文件中添加配置文件spring-mvc.xml
4.在web.xml中加载spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--加载springmvc的配置文件-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--过滤器:设置从浏览器过滤的数据,使用utf-8编码-->
<filter>
<filter-name>filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5.添加页面index.jsp文件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/com/login" method="post">
用户名:<input name="userName"><br>
密码:<input name="passWorld"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
6.在main/java包下创建com包,在com包下创建controller包在controller包下创建loginController类
@Controller
@RequestMapping("/com")
public class loginController {
@RequestMapping("/login")
@ResponseBody
public String login(String passWorld,String userName) {
System.out.println(passWorld);
System.out.println(userName);
return "登陆成功";
}
}
7.在spring-mvc.xml文件下配置注解扫描,和开启注解扫描
<!--配置注解扫描-->
<context:component-scan base-package="com"/>
<!-- 开启注解支持 -->
<mvc:annotation-driven>
<mvc:message-converters>
<!-- 配置编码字符集 -->
<!-- 配置响应编码字符集 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<value>application/xml;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- <!– 配置Jackson消息转换器 –>-->
<!-- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">-->
<!-- <property name="objectMapper">-->
<!-- <bean class="com.fasterxml.jackson.databind.ObjectMapper">-->
<!-- <!– 格式化日期 –>-->
<!-- <property name="dateFormat">-->
<!-- <bean class="java.text.SimpleDateFormat">-->
<!-- <constructor-arg type="java.lang.String" value="yyyy-MM-dd" />-->
<!-- </bean>-->
<!-- </property>-->
<!-- </bean>-->
<!-- </property>-->
<!-- </bean>-->
</mvc:message-converters>
</mvc:annotation-driven>
8.在com/interceptor包下创建拦截器,
自定义拦截器有两种:
- 实现HandlerInterceptor接口
- 继承HandlerInterceptorAdapter类
这里我两种都用了:一种用来验证用户名,一种用来验证用户密码;
实现HandlerInterceptor接口:
public class MyIterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if ("123".equals(request.getParameter("passWorld"))){
System.out.println("密码验证正确,放行2");
return true;
}
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("方法执行后2");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("页面渲染后2");
}
}
继承HandlerInterceptorAdapter类:
public class MyIterceptor2 extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if ("张三".equals(request.getParameter("userName"))){
request.setAttribute("messege","登陆成功");
System.out.println("姓名验证成功放行1");
return true;
}
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("方法执行后1");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("页面渲染后1");
}
}
9.创建完拦截器之后再spring-mvc.xml文件中配置拦截配置
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.interceptor.MyIterceptor2"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.interceptor.MyIterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
10:查看效果:
提交前:
提交后控制台输出:
提交后页面:
上面是成功的下面我来测试一组失败的数据:
控制台输出:
页面放回的为空白;