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

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;
        }
       
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容