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
相关产品推荐
相关产品推荐

