【笔记】springboot-security放行静态资源

刚搞完oauth2认证 开始尝试自己搞个认证界面 然后发现有部分静态资源就是无法加载 按以下步骤检查

  1. 检查自己写的WebSecurityConfigurerAdapter
    检查方法 public void configure(WebSecurity web) 该方法与形参HttpSecurity http的区别是前者不走过滤链
@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/css/**", "/js/**", "/img/**");
}
  1. 检查静态资源有没有放对位置 thymeleaf默认会在classpath:/static/,classpath:/template/下搜索目标文件
  2. 以上步骤都没问题 但问题依然存在 开日志 在application.yml加入如下内容
logging:
  level:
    org.springframework: debug

再次运行 日志会打印一堆东西 直接搜索你缺少的文件名称 我缺少的是bootstrap的css和js文件 部分日志如下

2023-06-19 02:18:31.624 DEBUG 574542 --- [nio-8086-exec-3] o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/css/login.css'; against '/css/**'
2023-06-19 02:18:31.624 DEBUG 574542 --- [nio-8086-exec-2] o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/css/bootstrap/bootstrap.css'; against '/css/**'
2023-06-19 02:18:31.624 DEBUG 574542 --- [nio-8086-exec-2] o.s.security.web.FilterChainProxy        : /css/bootstrap/bootstrap.css has an empty filter list
2023-06-19 02:18:31.624 DEBUG 574542 --- [nio-8086-exec-3] o.s.security.web.FilterChainProxy        : /css/login.css has an empty filter list
2023-06-19 02:18:31.624 DEBUG 574542 --- [nio-8086-exec-2] o.s.web.servlet.DispatcherServlet        : GET "/css/bootstrap/bootstrap.css", parameters={}
2023-06-19 02:18:31.624 DEBUG 574542 --- [nio-8086-exec-3] o.s.web.servlet.DispatcherServlet        : GET "/css/login.css", parameters={}
2023-06-19 02:18:31.627 DEBUG 574542 --- [nio-8086-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
2023-06-19 02:18:31.627 DEBUG 574542 --- [nio-8086-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
2023-06-19 02:18:31.628 DEBUG 574542 --- [nio-8086-exec-2] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2023-06-19 02:18:31.628 DEBUG 574542 --- [nio-8086-exec-2] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2023-06-19 02:18:31.630 DEBUG 574542 --- [nio-8086-exec-2] o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/error'; against '/css/**'
2023-06-19 02:18:31.630 DEBUG 574542 --- [nio-8086-exec-2] o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/error'; against '/js/**'
2023-06-19 02:18:31.630 DEBUG 574542 --- [nio-8086-exec-2] o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/error'; against '/img/**'

根据日志可以看到是404 没找到该资源文件 但我明明放进去了啊 遂检查IDE的输出文件

太艹了 他怎么没编译到输出里去 然后mvn clean再mvn compile 看到文件有了 再运行 正常了 不知道这是IDE的bug还是啥

posted on 2023-06-19 02:29  绝对密位  阅读(725)  评论(0编辑  收藏  举报

导航