真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

SpringBoot2.0設置攔截器-創(chuàng)新互聯(lián)

所有功能完成 配置登錄認證

配置攔截器

在spring boot2.0 之后 通過繼承這個WebMvcConfigurer類 就可以完成攔截
  • 新建包com.example.interceptor;

    成都創(chuàng)新互聯(lián)從2013年開始,先為旌德等服務建站,旌德等地企業(yè),進行企業(yè)商務咨詢服務。為旌德企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。
  • 創(chuàng)建login攔截類

package?com.example.interceptor;import?org.springframework.web.servlet.HandlerInterceptor;import?org.springframework.web.servlet.ModelAndView;import?javax.servlet.http.HttpServletRequest;import?javax.servlet.http.HttpServletResponse;import?javax.servlet.http.HttpSession;public?class?LoginInterceptor?implements?HandlerInterceptor?{????@Override
????public?boolean?preHandle(HttpServletRequest?request,?HttpServletResponse?response,?Object?handler)?throws?Exception?{???????//請求進入這個攔截器
????????HttpSession?session?=?request.getSession();????????if(session.getAttribute("user")?==?null){???????//判斷session中有沒有user信息//????????????System.out.println("進入攔截器");
????????????if("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))){
????????????????response.sendError(401);
????????????}
????????????response.sendRedirect("/");?????//沒有user信息的話進行路由重定向
????????????return?false;
????????}????????return?true;????????//有的話就繼續(xù)操作
????}????@Override
????public?void?postHandle(HttpServletRequest?request,?HttpServletResponse?response,?Object?handler,?ModelAndView?modelAndView)?throws?Exception?{

????}????@Override
????public?void?afterCompletion(HttpServletRequest?request,?HttpServletResponse?response,?Object?handler,?Exception?ex)?throws?Exception?{

????}
}
  • 在com.example包中添加攔截控制器

package?com.example;import?com.example.interceptor.LoginInterceptor;import?com.example.interceptor.RightsInterceptor;import?org.springframework.beans.factory.annotation.Autowired;import?org.springframework.context.annotation.Configuration;import?org.springframework.web.servlet.config.annotation.*;@Configuration??????????//使用注解?實現(xiàn)攔截public?class?WebAppConfigurer?implements?WebMvcConfigurer???{????@Autowired
????RightsInterceptor?rightsInterceptor;????@Override
????public?void?addInterceptors(InterceptorRegistry?registry)?{????????//登錄攔截的管理器
????????InterceptorRegistration?registration?=?registry.addInterceptor(new?LoginInterceptor());?????//攔截的對象會進入這個類中進行判斷
????????registration.addPathPatterns("/**");????????????????????//所有路徑都被攔截
????????registration.excludePathPatterns("/","/login","/error","/static/**","/logout");???????//添加不攔截路徑

????}

}
  • 在WebAppConfigurer.java中增加內容

package?com.example;import?com.example.interceptor.LoginInterceptor;import?com.example.interceptor.RightsInterceptor;import?org.springframework.beans.factory.annotation.Autowired;import?org.springframework.context.annotation.Configuration;import?org.springframework.web.servlet.config.annotation.*;@Configuration??????????//使用注解?實現(xiàn)攔截public?class?WebAppConfigurer?implements?WebMvcConfigurer???{????@Autowired
????RightsInterceptor?rightsInterceptor;????@Override
????public?void?addInterceptors(InterceptorRegistry?registry)?{????????//登錄攔截的管理器
????????InterceptorRegistration?registration?=?registry.addInterceptor(new?LoginInterceptor());?????//攔截的對象會進入這個類中進行判斷
????????registration.addPathPatterns("/**");????????????????????//所有路徑都被攔截
????????registration.excludePathPatterns("/","/login","/error","/static/**","/logout");???????//添加不攔截路徑//????????super.addInterceptors(registry);


????????//權限攔截的管理器
????????InterceptorRegistration?registration1?=?registry.addInterceptor(rightsInterceptor);
????????registration1.addPathPatterns("/**");????????????????????//所有路徑都被攔截
????????registration1.excludePathPatterns("/","/login","/error","/static/**","/logout");???????//添加不攔截路徑
????}

}

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡助力業(yè)務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調度,確保服務器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務器買多久送多久。


網(wǎng)站名稱:SpringBoot2.0設置攔截器-創(chuàng)新互聯(lián)
鏈接URL:http://weahome.cn/article/hgios.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部