spring boot前后端分离登录拦截以及静态资源路径映射

拦截实体类
import com.kol.constant.SessioNameCons;
import com.kol.domain.UserDomain;
import com.kol.msg.Msg;
import com.kol.utils.JsonUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

import com.kol.constant.SessioNameCons;
import com.kol.domain.UserDomain;
import com.kol.msg.Msg;
import com.kol.utils.JsonUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession httpSession=request.getSession();
        //这个是自己定义的用户实体类
        UserDomain userDomain=null;
        //在session中取出用户数据
        userDomain= (UserDomain) httpSession.getAttribute(SessioNameCons.USER_NAME);
        if (userDomain==null){
            Msg msg=new Msg(0,"No access rights, please log in first!!!");
            System.out.print(JsonUtils.toString(msg));
            //未登录或者session过期在这里执行跳转登录页面
            //response.sendRedirect("/login");
            response.getWriter().print(JsonUtils.toString(msg));
            response.getWriter().flush();
            response.getWriter().close();
            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 {

    }
}

静态资源配置以及拦截路由配置

import com.kol.constant.AppConstant;
import com.kol.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
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.WebMvcConfigurer;


@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;


    /**
     * 重写添加拦截器方法并添加配置拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.print("addd=================");
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/testServlet/**")
                .excludePathPatterns("/login")
                //放行静态资源 ,"/file/**","/css/**","/fonts/**","/js/**"
                .excludePathPatterns("/file/**");
    }

    //配置静态资源的位置
//注意:AppConstant.FILE_MAP="/**"; 表示可以直接访问ip:端口/文件夹名字/图片名字访问
//AppConstant.FILE_MAP="/demo/**"; 表示可以直接访问ip:端口/demo/文件夹名字/图片名字访问
//addResourceLocations("file:" + AppConstant.FILE_PATH); 注意 一定要添加"file:",AppConstant.FILE_PATH是图片存放路径

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(AppConstant.FILE_MAP)
                .addResourceLocations("file:" + AppConstant.FILE_PATH);

       // super.addResourceHandlers(registry);
    }
}

你可能感兴趣的:(Spring,boot,登录拦截,静态资源配置)