springmvc登录拦截器

本文介绍了一个登录拦截器的实现方式,该拦截器用于检查用户的登录状态,并定义了一系列不需要拦截的URL路径。通过Spring的OncePerRequestFilter实现了高效的过滤机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 拦截类

package com.pro.huanbao.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

/**
 * @Title   NotLoginedFilter.java
 * @Package com.wanpu.filter
 * @author  wanpu_ly
 * @dade    2017年12月26日 上午11:33:37
 * @version V1.0
 * 类说明:	
 */
public class NotLoginedFilter extends OncePerRequestFilter{

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		// 不过滤的uri
		System.err.println("请求过滤器启动,开始过滤请求...");
		List<String> notFilter = new ArrayList<>();
		notFilter.add("/");
		notFilter.add("/login");
		
        notFilter.add("/sadr/**");
        notFilter.add("/search/**");
        notFilter.add("/shop/**");
        notFilter.add("/product/**");
        notFilter.add("/information/**");
        
        notFilter.add("/company/thirdlogin/*");
        notFilter.add("/company/thirdnone/*");
        notFilter.add("/company/updatePasswordPage/*");
        notFilter.add("/company/updatePassword/*");
        
        notFilter.add("/company/loginByQQIndex");
        notFilter.add("/company/loginByQQ");
        notFilter.add("/company/loginByWeixin");
        notFilter.add("/company/loginByPassword");
        notFilter.add("/company/loginBycode");
        notFilter.add("/company/confirmPasswordExistence");
        notFilter.add("/company/sendRegisterCode");
        notFilter.add("/company/sendLoginCode");
        notFilter.add("/company/selectByName");
        notFilter.add("/company/selectByPhone");
        notFilter.add("/register");
        notFilter.add("/register.html");
        notFilter.add("/company/saveReferUrl");
        notFilter.add("/company/selectByEmail");
        notFilter.add("/company/sendValidateMail");
        notFilter.add("/getImageBase");
        notFilter.add("/updateProduceBrandAndAddress");
        notFilter.add("/index_img/transfer/*");
        notFilter.add("/deleteIndexCache");
        notFilter.add("/deleteCategoryCache");
        notFilter.add("/myhb/toCipherback");
		
		boolean doFilter = true;
		// 请求的uri
		String uri = request.getRequestURI();
		// 排除请求
		if (notFilter.indexOf(uri) != -1) {
			doFilter = false;
		}else if (uri.indexOf("sadr") != -1) {
			doFilter = false;
		}else if (uri.indexOf("search") != -1) {
			doFilter = false;
		}else if (uri.indexOf("shop") != -1) {
			doFilter = false;
		}else if (uri.indexOf("product") != -1) {
			doFilter = false;
		}else if (uri.indexOf("information") != -1) {
			doFilter = false;
		}else if (uri.indexOf("/company/thirdlogin/") != -1) {
			doFilter = false;
		}else if (uri.indexOf("/company/thirdnone/") != -1) {
			doFilter = false;
		}else if (uri.indexOf("/company/updatePasswordPage/") != -1) {
			doFilter = false;
		// 不拦截css,js,图片等
		}else if (uri.indexOf("js") != -1) {
			doFilter = false;
		}else if (uri.indexOf("style") != -1) {
			doFilter = false;
		}
		else if (uri.indexOf("assets") != -1) {
			doFilter = false;
		}
		else if (uri.indexOf("im") != -1) {
			doFilter = false;
		}
		
		
		if (doFilter) {
			// 执行过滤
			// 从session中获取登录实体
			Object obj = request.getSession().getAttribute("name");
			if (obj == null) {
				// 如果session中不存在登录者实体,则弹出框提示需要登录
				response.sendRedirect("/login");
			}else {
				filterChain.doFilter(request, response);
			}
			
		}else {
			filterChain.doFilter(request, response);
		}
	}

}

 

2. web.xml 配置

<!-- 登录拦截器 -->
	<filter>
		<filter-name>NotLoginedFilter</filter-name>
		<filter-class>com.pro.huanbao.filter.NotLoginedFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>NotLoginedFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 
SpringMVC登录拦截器是一个拦截器,用于在用户登录前或登录后对请求进行拦截和验证。它可以用于保护应用程序的安全性,避免未授权的用户访问敏感资源或执行敏感操作。 下面是一个示例,展示了如何使用SpringMVC登录拦截器: 1. 创建一个实现HandlerInterceptor接口的拦截器类,例如LoginInterceptor。 ```java public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); Object user = session.getAttribute("user"); if (user == null) { response.sendRedirect("/login"); return false; } return true; } } ``` 2. 在SpringMVC配置文件中注册该拦截器。 ```xml <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.example.LoginInterceptor"/> </mvc:interceptor> </mvc:interceptors> ``` 这将拦截所有请求,然后调用LoginInterceptor的preHandle方法进行验证。如果用户未登录,则重定向到登录页面。 3. 在控制器中设置登录信息。 ```java @RequestMapping("/login") public String login(HttpServletRequest request) { String username = request.getParameter("username"); String password = request.getParameter("password"); if ("admin".equals(username) && "admin".equals(password)) { HttpSession session = request.getSession(); session.setAttribute("user", username); return "redirect:/home"; } else { return "redirect:/login"; } } ``` 这将检查用户名和密码是否正确,如果正确则将用户信息保存在会话中,并将用户重定向到主页。如果不正确,则将用户重定向回登录页面。 通过这些步骤,我们可以实现一个简单的SpringMVC登录拦截器。它将对所有请求进行拦截,并检查用户是否已登录。如果用户未登录,则将其重定向回登录页面。这样可以保护应用程序的安全性,避免未授权的用户访问敏感资源或执行敏感操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值