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

网友投稿 311 2023-02-10


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, Excepthttp://ion 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.addResourceHandleyVTXzIlr("/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);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:接口api管理工具(接口api管理工具怎么用)
下一篇:Angular2进阶之如何避免Dom误区
相关文章

 发表评论

暂时没有评论,来抢沙发吧~