您好,登錄后才能下訂單哦!
// 注入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() {
}
}
接下來實現(xiàn)一個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;
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。