当前位置: 代码迷 >> 综合 >> 使用springmvc拦截器实现登录验证
  详细解决方案

使用springmvc拦截器实现登录验证

热度:84   发布时间:2023-09-05 20:07:37.0

使用springmvc拦截器实现登录验证
把页面放到web-inf中,放入到web-inf中后必须通过控制器转发到页,springmvc拦截器拦截的是控制器,不能拦截jsp,通过拦截器拦截全部控制器,需要在拦截器内部放行login控制器

控制器

package com.ning.controller;
import com.ning.pojo.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpSession;@Controller
public class DemoController {
// @RequestMapping("main")
// public String main(){
// return "/WEB-INF/page/main.jsp";
// }//优先找最匹配的@RequestMapping("{page}")public String main(@PathVariable String page){System.out.println("restful");return page;}
// @RequestMapping("login")
// public String login(){
// System.out.println("login");
// return "/login.jsp";
// }@RequestMapping("login")public String login(Users users, HttpSession session){if (users.getUsername().equalsIgnoreCase("admin")&&users.getPassword().equals("123")) {session.setAttribute("users",users);return "main";}else {return "redirect:/login,jsp";}}
}

拦截器

package com.ning.interceptor;import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class loginInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {Object obj = httpServletRequest.getSession().getAttribute("users");String uri = httpServletRequest.getRequestURI();if (uri.endsWith("login")) {return true;}else {if (obj != null) {return true;}httpServletResponse.sendRedirect("/springMVC07/login.jsp");return false;}}@Overridepublic void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {}@Overridepublic void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {}
}

springmvc.xml

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--扫描注解--><context:component-scan base-package="com.ning.controller"/><!--注解驱动--><mvc:annotation-driven/><!--静态资源--><mvc:resources mapping="/js/**" location="/js/"/><!--视图解析器--><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/page/"/><property name="suffix" value=".jsp"/></bean><!--配置拦截器--><mvc:interceptors><bean class="com.ning.interceptor.loginInterceptor"/></mvc:interceptors>
</beans>

web.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"><!--配置前端控制器--><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:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--字符过滤器--><filter><filter-name>encoding</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></filter><filter-mapping><filter-name>encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping> </web-app> 
  相关解决方案