Spring boot + Spring Security CSS静态资源拦截问题
2017-08-31 21:17
627 查看
问题描述
在使用Spring boot + Spring Security整合的时候,Spring Security对登陆进行了响应的处理操作,但是在进入登陆页的时候,出现页面报错,页面布局全部错乱的问题,查看原因发现是CSS与JS等静态文件没有被加载成功导致问题原因
Spring Security默认会对静态文件进行拦截,这个问题在Spring MVC中也出现过,Spring MVC的解决办法是在配置文件中加入静态资源的引用配置,但是Spring boot + Spring Security整合中采用全注解方式,没有配置文件,因此需要进行如下改动:@Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) //开启security注解 public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ @Bean @Override protected AuthenticationManager authenticationManager() throws Exception { return super.authenticationManager(); } @Override protected void configure(HttpSecurity http) throws Exception { //允许所有用户访问"/"和"/home" http.authorizeRequests() .antMatchers("/home").permitAll() //其他地址的访问均需验证权限 .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") //指定登录页是"/login" .defaultSuccessUrl("/list") //登录成功后默认跳转到"list" .permitAll() .and() .logout() .logoutSuccessUrl("/home") //退出登录后的默认url是"/home" .permitAll(); } @Override public void configure(WebSecurity web) throws Exception { //解决静态资源被拦截的问题 web.ignoring().antMatchers("/global/**"); } }
Spring boot的默认静态资源放置位置是在resource/static下,可以在static下新建一个文件夹,然后在上述方法中指定跳过拦截的文件路径即可。
相关文章推荐
- SpringBoot+SpringSecurity误拦截静态资源问题调研
- 解决SpringBoot 整合 FreeMaker,不能js,css静态资源的问题
- Spring Boot无法访问css,js等静态资源的问题
- spring boot 加入 spring-security配置 角色前缀 静态资源访问
- Spring-MVC 登录拦截与静态资源冲突的问题
- Spring-Boot整合freemarker引入静态资源css、js等(转)
- spring MVC js css图片等静态资源无法加载问题
- springboot静态资源加载路径问题
- 解决SpringBoot整合shiro之后无法加载静态资源文件问题
- SpringMVC添加spring-security静态资源加载问题
- springboot 静态资源html,jsp,css
- 关于使用security和静态资源被拦截的问题
- 解决springboot项目打war 包docker包找不到resource 下的静态资源问题
- Spring Boot+thymeleaf 静态资源上下文的问题
- springboot静态资源加载css出现404错误
- 96. Spring Boot之静态资源版本映射(解决js/css缓存问题)
- Spring boot + Spring Security + thymeleaf表单提交被拦截问题
- springboot security CSRF问题
- IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404
- Spring MVC程序中怎么得到静态资源文件css,js,图片文件的路径问题