您好,登錄后才能下訂單哦!
這篇文章主要介紹spring boot中如何設置默認網頁,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
一共兩布,第一步,創建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("----進入登錄攔截器--url:"+request.getServletPath()+"-----"); if(session.getAttribute(WebConfig.LOGIN_USER) == null){ logger.info("------跳轉到login頁面-----"); response.sendRedirect(request.getContextPath()+"/index"); return false; }else{ session.setAttribute(WebConfig.LOGIN_USER, session.getAttribute(WebConfig.LOGIN_USER)); return true; } } }
第二步,注冊創建的攔截器
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(); } //因為新加了攔截器,這里需要重新設置資源地址 @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) { // 攔截規則:除了login,其他都攔截判斷,excludePathPatterns是排除攔截的路徑,一個是登錄驗證地址,一個是登錄頁 registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/index","/system/user/validate"); super.addInterceptors(registry); } }
以上是“spring boot中如何設置默認網頁”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。