91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring Boot2.0 設置攔截器

發布時間:2020-07-21 12:21:28 來源:網絡 閱讀:698 作者:沙漏半杯 欄目:編程語言

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

配置攔截器

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

  • 創建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;????????//有的話就繼續操作
????}????@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??????????//使用注解?實現攔截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??????????//使用注解?實現攔截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");???????//添加不攔截路徑
????}

}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

万宁市| 许昌县| 邮箱| 忻城县| 澄迈县| 略阳县| 万荣县| 建昌县| 梁山县| 绥滨县| 六枝特区| 陆川县| 德安县| 恭城| 双鸭山市| 新龙县| 宁武县| 淮滨县| 花莲县| 东光县| 营口市| 嘉定区| 阿城市| 普陀区| 喀喇沁旗| 葫芦岛市| 康保县| 顺平县| 纳雍县| 南江县| 三明市| 通辽市| 武邑县| 耒阳市| 临西县| 淳化县| 南靖县| 阿瓦提县| 汝南县| 元朗区| 青川县|