Spring Security 静态资源访问

在搞 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 资源的时候,我们要写的更加标准一些。


<link href="/global/css/style.css" rel="stylesheet" type="text/css" />

<script src="/global/js/custom.min.js" type="text/javascript"></script>

而不是


<link href="/global/css/style.css"/>

<script src="/global/js/custom.min.js"></script>

转载于:https://www.cnblogs.com/dowhile/p/One.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值