美文网首页
spring boot 静态资源访问及静态资源权限放开

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

作者: 虎纹鲨鱼保护协会 | 来源:发表于2018-08-22 11:16 被阅读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;
        }
       
    }
}

相关文章

网友评论

      本文标题:spring boot 静态资源访问及静态资源权限放开

      本文链接:https://www.haomeiwen.com/subject/cnmeiftx.html