springboot 详解 (五)interceptor

  1. 云栖社区>
  2. 博客>
  3. 正文

springboot 详解 (五)interceptor

curiousby 2016-11-17 17:10:00 浏览1033
展开阅读全文

 

 ----------------------------------------------------------------------------------------------------------------

springboot 详解 (一) helloworld                                    下载demo          

springboot 详解 (二) crud                                             下载demo     

springboot 详解 (三) 多数据源                                        下载demo      

springboot 详解 (四)redis & filter                                    下载demo    

springboot 详解 (五)interceptor                                       下载demo     

springboot 详解 (六)servlet & scheduled & listener         下载demo     

springboot 详解(七) dubbox & zookeeper   下载(productor) 下载(constumser)

springboot 同步解耦 异步化 下载demo

springboot jenkins docker 部署 

springboot 详解(八) 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);
    }
    
    
    
    //优先级 :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

 会自动重定向到

http://localhost:8080/SpringBootWebDemo/web/static/forword

 

拦截器中添加了SpringBootWebDemo 这是我的工程名

server.context-path=/SpringBootWebDemo

 

也许默认更好点

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

 

 

 

 

网友评论

登录后评论
0/500
评论
curiousby
+ 关注