Spring Security JavaConfig:配置所需的频道(安全,不安全,任何)

时间:2014-11-07 11:26:39

标签: spring spring-security spring-java-config

我试图通过任何渠道提供所有静态资源(css,javascript和图片),但却无法与.anyRequest().requiresInsecure()结合使用:

@Configuration
@EnableWebMvcSecurity
@PropertySource("classpath:security.properties")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Value("${security.auth_urls}")
    private String[] authUrls;
    @Value("${security.secured_urls}")
    private String[] securedUrls;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers(authUrls).authenticated()
                .and()
            .formLogin()
                .loginPage("/login.html")
                .permitAll()
                .and()
            .logout()
                .logoutUrl("/logout.html")
                .permitAll()
                .and()
            .requiresChannel()
                .antMatchers("/resources/**,/res/**").requires(ChannelDecisionManagerImpl.ANY_CHANNEL)
                .antMatchers(securedUrls).requiresSecure()
                .anyRequest().requiresInsecure();
    }

    // ...
}

评论.anyRequest.requiresInsecure()时,它有效。

我想使用HTTPS提供特定页面,所有其他页面都使用HTTP和静态资源同时提供。

2 个答案:

答案 0 :(得分:4)

在我的应用程序中,我需要主页网址不安全(需要http),其他需要保护(仅限https)。我设法通过遵循下一个顺序来做到这一点:

...
.and().requiresChannel().antMatchers(homeUrls).requiresInsecure()
.and().requiresChannel().anyRequest().requiresSecure()
...

即。首先是规则,允许(不安全|两者),然后去规则,禁止(仅限安全)。

HTH

答案 1 :(得分:0)

使用

.antMatchers("/resources/**", "/res/**").requires(ChannelDecisionManagerImpl.ANY_CHANNEL)

而不是

.antMatchers("/resources/**,/res/**").requires(ChannelDecisionManagerImpl.ANY_CHANNEL)

做了这个伎俩。