spring boot 中設(shè)置默認網(wǎng)頁的方法
廢話不多說,直接上代碼,相信都能看的懂
一共兩布,第一步,創(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("----進入登錄攔截器--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;
}
}
}
第二步,注冊創(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();
}
//因為新加了攔截器,這里需要重新設(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是排除攔截的路徑,一個是登錄驗證地址,一個是登錄頁
registry.addInterceptor(new
LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/index","/system/user/validate");
super.addInterceptors(registry);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Cloud OAuth2中/oauth/token的返回內(nèi)容格式
Spring Cloud OAuth2 生成access token的請求/oauth/token的返回內(nèi)容就需要自定義,本文就詳細介紹一下,感興趣的可以了解一下2021-07-07
java工具類SendEmailUtil實現(xiàn)發(fā)送郵件
這篇文章主要為大家詳細介紹了java工具類SendEmailUtil實現(xiàn)發(fā)送郵件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02
詳解Java8?CompletableFuture的并行處理用法
Java8中有一個工具非常有用,那就是CompletableFuture,本章主要講解CompletableFuture的并行處理用法,感興趣的小伙伴可以了解一下2022-04-04
springboot部署linux訪問服務(wù)器資源的方法
這篇文章主要介紹了springboot部署linux訪問服務(wù)器資源,部署springboot項目至服務(wù)器用了幾種不同方法,文中給大家詳細介紹,需要的朋友可以參考下2019-12-12
javaweb Servlet開發(fā)總結(jié)(一)
Servlet是sun公司提供的一門用于開發(fā)動態(tài)web資源的技術(shù)。這篇文章主要介紹了javaweb Servlet開發(fā)的第一篇,感興趣的小伙伴們可以參考一下2016-05-05
IDEA使用maven創(chuàng)建hibernate項目的實現(xiàn)步驟(圖文)
本文主要介紹了IDEA使用maven創(chuàng)建hibernate項目的實現(xiàn)步驟,包括創(chuàng)建Maven項目,配置Hibernate,以及創(chuàng)建實體類映射到數(shù)據(jù)庫等步驟,具有一定的參考價值,感興趣的可以了解一下2023-08-08
Java實現(xiàn)根據(jù)sql動態(tài)查詢并下載數(shù)據(jù)到excel
這篇文章主要為大家詳細介紹了如何使用Java實現(xiàn)根據(jù)sql動態(tài)查詢并下載數(shù)據(jù)到excel的功能,文中的示例代碼講解詳細,有需要的可以參考下2024-04-04

