spring boot 中設(shè)置默認(rèn)網(wǎng)頁(yè)的方法
廢話不多說,直接上代碼,相信都能看的懂
一共兩布,第一步,創(chuàng)建Interceptor攔截
package com.cy.example.config; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class LoginInterceptor implements HandlerInterceptor { private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class); public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub } public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // TODO Auto-generated method stub //獲取session HttpSession session = request.getSession(true); logger.info("----進(jìn)入登錄攔截器--url:"+request.getServletPath()+"-----"); if(session.getAttribute(WebConfig.LOGIN_USER) == null){ logger.info("------跳轉(zhuǎn)到login頁(yè)面-----"); response.sendRedirect(request.getContextPath()+"/index"); return false; }else{ session.setAttribute(WebConfig.LOGIN_USER, session.getAttribute(WebConfig.LOGIN_USER)); return true; } } }
第二步,注冊(cè)創(chuàng)建的攔截器
package com.cy.example.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebConfig extends WebMvcConfigurerAdapter { public static String LOGIN_USER = "loginUser"; public WebConfig() { super(); } //因?yàn)樾录恿藬r截器,這里需要重新設(shè)置資源地址 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations( "classpath:/static/"); registry.addResourceHandler("/templates/**").addResourceLocations( "classpath:/templates/"); super.addResourceHandlers(registry); } @Override public void configureDefaultServletHandling( DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override public void addInterceptors(InterceptorRegistry registry) { // 攔截規(guī)則:除了login,其他都攔截判斷,excludePathPatterns是排除攔截的路徑,一個(gè)是登錄驗(yàn)證地址,一個(gè)是登錄頁(yè) registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/index","/system/user/validate"); super.addInterceptors(registry); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
這篇文章主要為大家詳細(xì)介紹了Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12maven繼承父工程統(tǒng)一版本號(hào)的實(shí)現(xiàn)
這篇文章主要介紹了maven繼承父工程統(tǒng)一版本號(hào)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Spring Boot實(shí)現(xiàn)文件上傳示例代碼
本篇文章主要介紹了Spring Boot實(shí)現(xiàn)文件上傳示例代碼,可以實(shí)現(xiàn)單文件和多文件的上傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03基于Spring MVC的文件上傳和下載實(shí)現(xiàn)方法
在Web應(yīng)用程序中,文件上傳和下載是常見的功能,Spring MVC框架提供了方便的方式來實(shí)現(xiàn)這些功能,本文將介紹如何使用Spring MVC實(shí)現(xiàn)文件上傳和下載,需要的朋友可以參考下2023-05-05MyBatis-Plus中如何使用ResultMap的方法示例
本文主要介紹了MyBatis-Plus中如何使用ResultMap,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Java 比較接口comparable與comparator區(qū)別解析
這篇文章主要介紹了Java 比較接口comparable與comparator區(qū)別解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10淺談Spring中@Transactional事務(wù)回滾及示例(附源碼)
本篇文章主要介紹了淺談Spring中@Transactional事務(wù)回滾及示例(附源碼),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12Java實(shí)戰(zhàn)網(wǎng)上電子書城的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+JSP+maven+Mysql實(shí)現(xiàn)一個(gè)網(wǎng)上電子書城,大家可以在過程中查缺補(bǔ)漏,提升水平2022-01-01解決idea中maven項(xiàng)目無端顯示404錯(cuò)誤的方法
這篇文章主要介紹了解決idea中maven項(xiàng)目無端顯示404錯(cuò)誤的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12