javascript
java+路径拦截实现_SpringBoot实现拦截器
首先,你的項目要能跑起來,且基于springboot的
boot的攔截器不用配置web.xml,按照特定名字書寫(后綴+注解),它會自動識別(位置隨便放,只要在代碼目錄下就可以)
1.攔截器 SessionInterceptor.java
package com.hfanss.blog.utils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
* 功能描述:攔截器實現(xiàn)類
* @author: hfanss
* @date: 2019年6月25日 下午9:23:32
*/
public class SessionInterceptor implements HandlerInterceptor
{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
System.err.println("攔截器進來了,路徑為:"+request.getRequestURI());
if (request.getRequestURI().equals("/admin/**"))
{
//TODO 驗證登錄token
return true;
}else {
return true;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
{
// TODO Auto-generated method stub
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception
{
// TODO Auto-generated method stub
}
}
2.路徑 SessionConfiguration.java
package com.hfanss.blog.utils;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 功能描述:配置攔截器路徑 /admin/
* @author: hfanss
* @date: 2019年6月25日 下午9:37:21
*/
@Configuration
public class SessionConfiguration implements WebMvcConfigurer
{
@Override
public void addInterceptors(InterceptorRegistry registry)
{
registry.addInterceptor(new SessionInterceptor()).addPathPatterns("/admin/**");
}
.....
}
運行項目,攔截器實現(xiàn)類方法內(nèi)打個斷點,會發(fā)現(xiàn)所有帶 admin的路徑都會被攔截到
可以根據(jù)需要定義攔截的路徑,攔截器常用的功能也就用戶登錄控制
總結(jié)
以上是生活随笔為你收集整理的java+路径拦截实现_SpringBoot实现拦截器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python里面print是什么意思_p
- 下一篇: telegr怎么连接不上_无线网密码正确