服务器之家

服务器之家 > 正文

spring boot 中设置默认网页的方法

时间:2021-04-19 14:02     来源/作者:pj小小码农

废话不多说,直接上代码,相信都能看的懂

一共两布,第一步,创建Interceptor拦截

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;
    }
  }
 
}

第二步,注册创建的拦截器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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);
  }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_20989105/article/details/78141009

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部