码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Security 静态资源访问

时间:2018-08-23 14:18:42      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:rmi   ror   ddr   fail   tps   ati   https   静态   java   

在搞 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>

Spring Security 静态资源访问

标签:rmi   ror   ddr   fail   tps   ati   https   静态   java   

原文地址:https://www.cnblogs.com/dowhile/p/One.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!