案例:使用拦截器实现用户登陆认证
JavaBean对象(User.java):
package com.zm.bean;
public class User {
private String username;
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
创建拦截器:实现HandlerInterceptor接口
package com.zm.interceptor;
import com.zm.bean.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginInterceptor implements HandlerInterceptor {
/*在处理业务之前被调用*/
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object obj) throws Exception {
System.out.println("preHandle……");
String uri = req.getRequestURI();
if(uri.indexOf("login")>0){//如果是访问登陆页面的请求,放行
return true;
}
HttpSession session = req.getSession();
User user = (User)session.getAttribute("user");
if(user!=null){//登陆成功
return true;
}
resp.sendRedirect("/login");
return false;
}
/*在业务处理器处理请求完成之后、视图生成之前执行*/
@Override
public void postHandle(HttpServletRequest req, HttpServletResponse resp, Object o, ModelAndView mav) throws Exception {
System.out.println("postHandle……");
}
/*在前端控制器完全处理完请求之后被调用,用于清理资源*/
@Override
public void afterCompletion(HttpServletRequest req, HttpServletResponse resp, Object obj, Exception ex) throws Exception {
System.out.println("afterCompletion……");
}
}
在spring mvc配置文件中注册拦截器
<!--注册拦截器-->
<mvc:interceptors>
<!-- 可以注册多个拦截器-->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<!--在拦截器中配置需要放行的资源文件路径
lib/js/jquery-3.3.1.js
-->
<mvc:exclude-mapping path="/lib/**" />
<bean class="com.zm.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
controller层(UserLoginController ):
package com.zm.controller;
import com.zm.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/*使用拦截器实现用户登陆验证的controller*/
@Controller
public class UserLoginController {
/*访问User用户登陆页面*/
@RequestMapping(value = "/login",method = RequestMethod.GET)
public String loginUserForm(){
return "loginUser";
}
/*访问用户登陆成功后要跳转的页面*/
@RequestMapping(value = "/okUser",method = RequestMethod.GET)
public String loginSuccess(){
return "success";
}
/*接收从jsp页面提交过来的表单数据*/
@RequestMapping(value = "/loginUser",method = RequestMethod.POST)
public String login(User user,HttpServletRequest req){
HttpSession session = req.getSession();
if("admin".equals(user.getUsername())&&"123".equals(user.getPassword())){
System.out.println("登陆成功,保存session……");
session.setAttribute("user",user);
return "redirect:/okUser";
}else{
System.out.println("登陆失败!");
return "/login";
}
}
}
web页面
登陆页面(loginUser.jsp):
<form method="post" action="loginUser">
<p>
用户名:<input type="text" name="username">
</p>
<p>
密  码:<input type="password" name="password">
</p>
<p>
<input type="submit" value="登陆">
<input type="reset" value="取消">
</p>
</form>
登陆成功后跳转的页面(success.jsp):
<h3>success!welcime:${user.username}</h3>
来源:CSDN
作者:zmt0104
链接:https://blog.csdn.net/qq_40845019/article/details/100046036