这周在调试代码的过程中遇到了一个问题

我们用浏览器访问后端接口时采用域名+URI的方式

http://localhost:9090/back/user/find?page=1

当我们需要从后台获取图片或者其他静态资源的时候,我们应当怎么做

不妨先直接这样访问

http://localhost:9090/static/upload/A.jpg

当我们输入一下格式URL的时候,后端会误认为当前访问的是一个不存在的接口

可能会需要token验证,或者404(一般都是路径不对)报错找不到资源

springboot中静态资源处理 springboot静态资源放行_spring boot

springboot中静态资源处理 springboot静态资源放行_spring_02

为此我们需要解决两个问题:

1 怎么让框架知道,我访问的是资源文件,而不是接口

2 访问资源文件的时候,和访问接口一样,都会被过滤器或者拦截器拦截,使用了jwt安全验证,需要带上token请求头,但是我们访问的时,不想验证,因此需要放行。

对症下药

一、创建配置类

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

///**没有生效的静态资源放行/
@Configuration
public class ResourceConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }
}

看到了这里有两个需要配置的路径:

注意

springboot中静态资源处理 springboot静态资源放行_springboot中静态资源处理_03

 

这一步完成了,标志着这个文件夹下的所有文件是静态资源,可以作为网络资源进行访问。

但是还有一个问题:

我们的jwt此时依然生效,需要验证登录的token,为了方便,我们直接将这些资源免密放行。

二、匹配器放行

这是我的WebSecurityConfigure配置类。

@EnableGlobalMethodSecurity(prePostEnabled = true) //至关重要的注解,缺失会导致验证不起效
@EnableWebSecurity
public class WebSecurityConfigure extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    //过滤器
    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    //入口
    @Autowired
    private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
    //访问拒绝处理器
    @Autowired
    private RestfulAccessDeniedHandler restfulAccessDeniedHandler;


    // 参数: HttpSecurity http
    //**http.authorizeRequests()**
    // 下添加了多个匹配器,每个匹配器用来控制不同的URL接受不同的用户访问。
    // 简单讲,http.authorizeRequests()就是在进行请求的权限配置。
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //第二步:我们用我们自己的数据库数据来完成权限验证
        // 开启跨域和关闭csrf保护
        http.cors().and().csrf().disable()
                .sessionManagement()//允许配置会话管理
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)//Spring Security不会创建HttpSession,也不会使用它获取SecurityContext
                .and()
                .authorizeRequests()
                .antMatchers(
                        "/index",
                        "/login",
                        "/upload/**",
                        "/back/product/upload",//文件上传
                        "/front/**",//公司详情  +  页面导航
                        "/back/**/latest" //最新产品+新闻

                        ).permitAll()// 首页 和 登录页面 放行
                .antMatchers(HttpMethod.OPTIONS).permitAll()    //options 方法的请求放行
                .anyRequest().authenticated()               // 其它请求要认证
                .and()
                // 这一步,告诉Security 框架,我们要用自己的UserDetailsService实现类
                // 来传递UserDetails对象给框架,框架会把这些信息生成Authorization对象使用
                .userDetailsService(userDetailsService);

        // 过滤前,我们使用jwt进行过滤
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
//        http.authorizeRequests()
//                .antMatchers("/index").permitAll()//放行
//                .anyRequest().authenticated()
//                .and()
//                .formLogin()//访问security自带的login接口
//                .and()
//                // 这一步,告诉Security 框架,我们要用自己的UserDetailsService实现类
//                // 来传递UserDetails对象给框架,框架会把这些信息生成Authorization对象使用
//                .userDetailsService(userDetailsService);
        //添加自定义未授权和未登录结果返回
        http.exceptionHandling()
                .accessDeniedHandler(restfulAccessDeniedHandler)
                .authenticationEntryPoint(restAuthenticationEntryPoint);

    }

实际应用中只需要在匹配器中添加:

springboot中静态资源处理 springboot静态资源放行_java_04

便实现了静态资源放行。

 

当我们能通过浏览器拿到图片,意味着这个静态资源时暴露到本地网络上的,说明配置是成功的

springboot中静态资源处理 springboot静态资源放行_java_05