【问题标题】:Spring security not working for additional post url [duplicate]Spring security不适用于其他帖子网址[重复]
【发布时间】:2021-09-23 20:04:50
【问题描述】:

我没有为这个问题找到满意的答案。

我的安全配置到目前为止一直运行良好。

我想再添加一个POST url,所有人都可以访问。

虽然其他排除的网址运行良好,但添加的额外添加的网址不起作用。

我的代码:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors()
        .and()
        .csrf().disable()
        .authorizeRequests()
        .antMatchers(HttpMethod.POST, "/ws/**").authenticated()
        .antMatchers(HttpMethod.DELETE, "/**").authenticated()
        .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
        .antMatchers(HttpMethod.GET, "/ws/getEvents").permitAll()// ---> While this is working
        .antMatchers(HttpMethod.POST, "/ws/persons/createNotificationsSubscriber*").permitAll()// -->this not working
        .anyRequest().authenticated()
        .and()
        .logout()
        .logoutSuccessUrl("http://localhost:3006/eventsMainView")
        .and()
        .csrf().disable()
        .httpBasic();
}

【问题讨论】:

    标签: java spring spring-boot spring-security


    【解决方案1】:

    这里的问题是

    .antMatchers(HttpMethod.POST, "/ws/**").authenticated()
    

    表示使用 POST 请求验证从 /ws 开始的所有 URL,但

    .antMatchers(HttpMethod.POST,"/ws/persons/createNotificationsSubscriber*").permitAll() // --> this not working
    

    这从相同的 /ws 开始,它是一个 POST 请求,所以 Spring 不允许这样做

    要完成你的工作,请使用这个-

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors()
            .and()
            .csrf().disable()
            .authorizeRequests()
            .antMatchers(HttpMethod.POST, "/ws/persons/createNotificationsSubscriber*").permitAll()// --> This will work
            .antMatchers(HttpMethod.POST, "/ws/**").authenticated()
            .antMatchers(HttpMethod.DELETE, "/**").authenticated()
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
            .antMatchers(HttpMethod.GET, "/ws/getEvents").permitAll()// ---> While this is working
            
            .anyRequest().authenticated()
            .and()
            .logout()
            .logoutSuccessUrl("http://localhost:3006/eventsMainView")
            .and()
            .csrf().disable()
            .httpBasic();
    }
    

    【讨论】:

    • 谢谢,它有效。我的主要逻辑是说最后一行将被排除,但现在我看到它正在使用第一个匹配项,就像路由一样。
    猜你喜欢
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 2015-11-03
    相关资源
    最近更新 更多