如何从spring-security基本身份验证中排除路径?

时间:2017-11-09 11:48:22

标签: java spring spring-mvc spring-security

我使用spring-securitysecurity.basic.enabled=true)进行基本身份验证,并希望排除某个路径和所有子路径。

以下不起作用:

@Configuration
public class WildcardConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);

        //allow anonymous access to all sub paths
        http.authorizeRequests().antMatchers("/my/**").permitAll();
    }
}

@RestController
public class SubController {
    @GetMapping("/my/sub/{id}")
    public String test(@PathVariable id) {
        return "got: " + id;
    }
}

当我致电localhost:8080/my/sub/123时。

结果:401未经授权。

为什么?

1 个答案:

答案 0 :(得分:0)

使用其他方法:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers(/my/**");
}