您好,登錄后才能下訂單哦!
這篇文章主要介紹spring boot中如何設(shè)置默認(rèn)網(wǎng)頁,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
一共兩布,第一步,創(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頁面-----"); 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è)是登錄頁 registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/index","/system/user/validate"); super.addInterceptors(registry); } }
以上是“spring boot中如何設(shè)置默認(rèn)網(wǎng)頁”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。