在搞 Spring Security 的时候遇到了一个小坑,就是静态资源加载的问题。
当我们继承了 WebSecurityConfigurerAdapter的时候,会去重写几个方法。去设定我们自己要过滤的路径或者是权限的一些规则。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
CustomUserService customUserService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserService).passwordEncoder(new BCryptPasswordEncoder());
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(\"/global/**\");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// 开始请求权限配置
.authorizeRequests()
// 我们指定任何用户都可以访问多个URL的模式。
// 任何用户都可以访问以\"/resources/\",\"/signup\", 或者 \"/about\"开头的URL。
// .antMatchers(\"/global/**\",\"/static/**\").permitAll()
// 请求匹配 /admin/** 只拥有 ROLE_ADMIN 角色的用户可以访问
.antMatchers(\"/admin/**\").hasRole(\"ADMIN\")
// 请求匹配 /user/** 拥有 ROLE_ADMIN 和 ROLE_USER 的角色用户都可以访问
.antMatchers(\"/user/**\").hasAnyRole(\"ADMIN\", \"USER\")
// 任何以\"/db/\" 开头的URL需要同时具有 \"ROLE_ADMIN\" 和 \"ROLE_DBA\"权限的用户才可以访问。
// 和上面一样我们的 hasRole 方法也没有使用 \"ROLE_\" 前缀。
// .antMatchers(\"/db/**\").access(\"hasRole(\'ADMIN\') and hasRole(\'DBA\')\")
// 其余所有的请求都需要认证后才可以访问
.anyRequest().authenticated().and().formLogin()
// 登陆界面;默认登陆成功后的界面(不起作用);默认登陆失败的界面;表单提交地址
.loginPage(\"/login\").defaultSuccessUrl(\"/index.html\").failureUrl(\"/login?error=true\")
// 默认用户名键值,默认密码键值
.usernameParameter(\"username\").passwordParameter(\"password\").permitAll().and().rememberMe()
.tokenValiditySeconds(1209600).key(\"rememberme\");
// .and()
// .logout().logoutUrl(\"\").logoutSuccessUrl(\"/index.html\").permitAll();
}
}
在一般来看来,我设置了
// 任何用户都可以访问以\"/resources/\",\"/signup\", 或者 \"/about\"开头的URL。
.antMatchers(\"/global/**\",\"/static/**\").permitAll()
或者是
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(\"/global/**\");
}
之后应该没有什么问题,就应该可以访问到了我们的资源。可是当你运行起demo之后,你会发现,世界并不是你想象的那个样子。你还太年轻。
你所要的静态资源还是加载不出来。后来发现,我们还需要去配置一下 SpringMVC 里的 addResourceHandlers 方法。
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addViewControllers(ViewControllerRegistry registry) {
// TODO Auto-generated method stub
// 注册访问 /login 转向 page-login.html 页面
registry.addViewController(\"/login\").setViewName(\"page-login.html\");
super.addViewControllers(registry);
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
// TODO Auto-generated method stub
registry.addResourceHandler(\"/**\").addResourceLocations(\"classpath:/static/\");
super.addResourceHandlers(registry);
}
}
看起来,这次应该就可以了吧。 Run ...
可是还是太年轻。依旧没有加载到资源。
这个,这个就有点凌乱了。。。。
过了好久好久好久,睡了一觉起来。
原来是HTML出了问题。对,没有听错是 HTML 出了问题。
在加载 css 或者是 js 资源的时候,我们要写的更加标准一些。
< href=\"/global/css/style.css\" rel=\"stylesheet\" type=\"text/css\" />
< src=\"/global/js/custom.min.js\" type=\"text/ \"></ >
而不是
< href=\"/global/css/style.css\"/>
< src=\"/global/js/custom.min.js\"></ >
继续阅读与本文标签相同的文章
-
Tablestore:多元索引的统计聚合
2026-05-18栏目: 教程
-
Alibaba Arthas 3.1.2版本:增加logger/heapdump/vmoption命令,支持tunnel server
2026-05-18栏目: 教程
-
进击的Java - 云原生进化
2026-05-18栏目: 教程
-
云服务OpenAPI设计的7大挑战
2026-05-18栏目: 教程
-
MySQL 之 LEFT JOIN 避坑指南
2026-05-18栏目: 教程
