You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Security 5从XML迁到Java配置时如何配置intercept-url的requires-channel

Spring Security XML转Java配置通道规则实现方案

通道匹配规则和权限匹配规则是独立生效的两套校验逻辑,XML中intercept-url的requires-channel属性对应的Java配置方法如下:

  • requires-channel="any" 对应anyChannel(),表示该路径允许http/https任意通道访问
  • requires-channel="https" 对应requiresSecure(),表示该路径必须走https通道
  • requires-channel="http" 对应requiresInsecure(),表示该路径必须走http通道

修改后的配置代码示例如下:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.requiresChannel()
            .channelProcessors(channelProcessors())
            // 匹配错误路径,允许任意通道
            .antMatchers("/error/**").anyChannel()
            // 私有路径要求必须走https
            .antMatchers("/private/**").requiresSecure()
            .and()
            .authorizeRequests()
            // 权限配置按顺序定义即可,不需要重复调用authorizeRequests()
            .antMatchers("/error/**").permitAll()
            .antMatchers("/private/**").authenticated();
}

额外说明:通道规则和权限规则都遵循从上到下先匹配生效的原则,写配置的时候要把更精确的路径规则放在前面,宽泛的规则放在后面,避免出现精确规则被宽泛规则覆盖的问题。

内容的提问来源于stack exchange,提问作者Marc Tousignant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 19:06:05