spring boot 静态资源访问及静态资源权限放开

2018-08-22  本文已影响0人  虎纹鲨鱼保护协会
package com.cms.interceptor;

import org.springframework.context.annotation.Configuration;

import org.springframework.util.ResourceUtils;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration

public class UserRoleAuthorizationInterceptorConfig extends WebMvcConfigurationSupport{

@Override

protected void addInterceptors(InterceptorRegistry registry) {

// TODO Auto-generated method stub

super.addInterceptors(registry);

registry.addInterceptor(new UserRoleAuthorizationInterceptor())

.addPathPatterns("/**")

.excludePathPatterns("/login","/logout","/**/*.html","/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg");

}

@Override

protected void addResourceHandlers(ResourceHandlerRegistry registry) {

// TODO Auto-generated method stub

registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");

super.addResourceHandlers(registry);

}

}

package com.cms.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class UserRoleAuthorizationInterceptor implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        
        String path=request.getServletPath();
        System.out.println(path);
        if(request.getSession().getAttribute("user")!=null) {
            return true;
        }else {
            return false;
        }
       
    }
}

上一篇 下一篇

猜你喜欢

热点阅读