// 注入spring容器
@Component
// 定义filterName 和过滤的url
@WebFilter(filterName = "my2Filter" ,urlPatterns = "/*")
public class My2Filter implements Filter {@Override
br/>@Override
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("filter2");
}
@Override
public void destroy() {
}
}
接下来实现一个springboot里面的拦截器的例子。
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new GlobalInterceptor()).addPathPatterns("/**");
}
}
import cn.vcinema.tool.security.session.SessionValidateResultEnum;
import cn.vcinema.tool.security.session.UserSessionInfo;
import cn.vcinema.tool.security.session.UserSessionUtils;
import com.pumpkin.conf.MallConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GlobalInterceptor implements HandlerInterceptor {
private final static Logger LOGGER = LoggerFactory.getLogger(GlobalInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String userId = request.getHeader("user_id");
String sessionId = request.getHeader("session_id");
String deviceId = request.getHeader("device_id");
LOGGER.info("headers:user_id=" + userId + ",session_id=" + sessionId + ",device_id=" + deviceId);
UserSessionInfo info = new UserSessionInfo(userId, sessionId, deviceId);
SessionValidateResultEnum resultEnum = UserSessionUtils.validateSession(MallConfig.getRunMode(), info);
return true;
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。