----------------------------------------------------------------------------------------------------------------
Detailed explanation of springboot (1) helloworld download demo
Springboot detailed explanation (2) crud download demo
Detailed explanation of springboot (3) Download demo from multiple data sources
Springboot detailed explanation (4) redis & filter download demo
Springboot detailed explanation (5) interceptor download demo
Springboot detailed explanation (6) servlet & scheduled & listener download demo
Springboot detailed explanation (7) dubbox & zookeeper download (productor) download (constumser)
springboot synchronous decoupling asynchronous download demo
Detailed explanation of springboot (8) springboot & springcloud
----------------------------------------------------------------------------------------------------------------
package com.curiousby.cn.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import com.curiousby.cn.constants.Constants; import com.curiousby.cn.one.entity.User; public class LoginAuthInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { User user = (User)request.getSession().getAttribute(Constants.USER); if (null == user) { response.sendRedirect("/SpringBootWebDemo/web/static/forward"); return false; } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
package com.curiousby.cn.configurer; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.curiousby.cn.interceptor.LoginAuthInterceptor; @Configuration public class WebAppConfigurer extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginAuthInterceptor()).addPathPatterns("/web/**").excludePathPatterns("/web/static/**"); super.addInterceptors(registry); } //Priority: META-INFO/resources > resources > static > public @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/img/**").addResourceLocations("classpath:/img/"); super.addResourceHandlers(registry); } }
访问 http://localhost:8080/SpringBootWebDemo/web/users/7
will automatically redirect to
http://localhost:8080/SpringBootWebDemo/web/static/forword
SpringBootWebDemo is added to the interceptor This is my project name
server.context-path=/SpringBootWebDemo
Maybe the default is better
Donate to developers
Driven by interest, I write 免费
something with joy and sweat. I hope you like my work and can support it at the same time. Of course, if you have money to support a money field (the love sign in the upper right corner, support Alipay and PayPal donations), if you have no money to support a personal field, thank you.
Thank you for your sponsorship, I will do better!