如何使用Java配置在Spring 4安全性中配置静态资源

时间:2015-02-23 19:56:26

标签: spring security static-resource

我认为这在xml配置中很容易,但是当我现在使用java代码配置时,我迷路了,所以有人能告诉我如何配置spring security以允许对安全性资源目录进行非安全性检查吗?

@Override
protected void configure(HttpSecurity http) throws Exception {

    http
    .csrf().disable()

    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    .and()
    .authorizeRequests().antMatchers(actuatorEndpoints()).hasRole(backendAdminRole)
    .and()
    .authorizeRequests().antMatchers(apiEndpoints()).hasRole(frontendUserRole)
    ***//what code can I add here to make static directory/recourse not checked by spring security?***
    .anyRequest().authenticated()
    .and().anonymous().disable()
    .exceptionHandling().authenticationEntryPoint(unauthorizedEntryPoint());
    ;
}

非常感谢您的善意发言

1 个答案:

答案 0 :(得分:0)

我猜你的意思是How do I define http "security = 'none' in JavaConfig?

  

很好,但只是不知道为什么只有WebSecurity,而不是HttpSecurity,所以如果我想忽略任何东西,我必须覆盖WebSecurity签名并放在那里,这也意味着我们必须拥有两个用于忽略和验证目的的配置函数?

不是真的。 configure(AuthenticationManagerBuilder auth)上还有一个WebSecurityConfigurerAdapter,因此,我没有看到在不同域对象之间分配这些责任的任何问题。从另一端ignore是更常见的安全功能,但http仅限于HTTP。

从另一方面,当您不注意ignored配置中的http时,您的代码应该更清晰。