Commit 88d3dbd3 authored by wujipeng's avatar wujipeng
Browse files

添加全局过滤器

parent 8475feb0
...@@ -2,6 +2,7 @@ package com.ruzhoukj.sms; ...@@ -2,6 +2,7 @@ package com.ruzhoukj.sms;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
/** /**
...@@ -9,6 +10,7 @@ import org.springframework.scheduling.annotation.EnableScheduling; ...@@ -9,6 +10,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
*/ */
@SpringBootApplication @SpringBootApplication
@EnableScheduling @EnableScheduling
@ServletComponentScan
public class SmsApplication { public class SmsApplication {
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -2,7 +2,6 @@ package com.ruzhoukj.sms.controller; ...@@ -2,7 +2,6 @@ package com.ruzhoukj.sms.controller;
import com.ruzhoukj.sms.common.PageResponse; import com.ruzhoukj.sms.common.PageResponse;
import com.ruzhoukj.sms.common.Result; import com.ruzhoukj.sms.common.Result;
import com.ruzhoukj.sms.entity.ScheduleEntity;
import com.ruzhoukj.sms.pojo.dto.ScheduleDTO; import com.ruzhoukj.sms.pojo.dto.ScheduleDTO;
import com.ruzhoukj.sms.pojo.vo.ScheduleVO; import com.ruzhoukj.sms.pojo.vo.ScheduleVO;
import com.ruzhoukj.sms.service.ScheduleService; import com.ruzhoukj.sms.service.ScheduleService;
......
package com.ruzhoukj.sms.filter;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
/**
* @author wujipeng
* @version 1.0
* @create 2024-10-28 10:28
*/
@Slf4j
@WebFilter(urlPatterns = "/*")
public class MyFilterOne implements Filter {
/**
* web应用启动时,web服务器将创建Filter的实例对象,并调用init方法,读取web.xml的配置,完成对象的初始化功能,
* 从而为后续的用户请求做好拦截的准备工作(filter对象只会创建一次,init方法也只会执行一次,开发人员通过init的参数,
* 可或得代表当前filter配置信息的FilterConfig对象)
*
* @param filterConfig filterConfig
*/
@Override
public void init(FilterConfig filterConfig) {
log.info("filterOne初始化");
}
/**
* 这个方法完成实际的过滤操作,当客户请求访问与过滤器相关联的URL的时候,Servlet过滤器将先执行doFilter方法,FilterChain参数用于访问后续过滤器
*
* @param servletRequest servletRequest
* @param servletResponse servletResponse
* @param filterChain filterChain
* @throws IOException IOException
* @throws ServletException ServletException
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
log.info("请求路径为:{}" ,httpRequest.getRequestURI());
log.info("请求方法为:{}",httpRequest.getMethod());
log.info("ip地址" + httpRequest.getRemoteAddr());
log.info("主机号" + httpRequest.getRemoteHost());
log.info("端口号" + httpRequest.getRemotePort());
filterChain.doFilter(servletRequest, servletResponse);
}
/**
* filter创建后会保存在内存中,当web应用移除或者服务器停止时才销毁,该方法在Filter的生命周期中仅执行一次,在这个方法中,可以释放过滤器使用的资源
*/
@Override
public void destroy() {
log.info("filterOne被销毁");
}
}
...@@ -80,15 +80,4 @@ public class DateUtils { ...@@ -80,15 +80,4 @@ public class DateUtils {
} }
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
} }
public static void main(String[] args) {
// 获取当前月份的开始和结束日期
Date[] startAndEndDates = getStartAndEndDateOfCurrentMonth();
Date startAndEndDate = startAndEndDates[0];
Date endAndEndDate = startAndEndDates[1];
long l = daysBetween(startAndEndDate, endAndEndDate);
System.out.println(l);
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment