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;
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ 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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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