【问题标题】:Http basic authentication not working in Spring SecurityHttp基本身份验证在Spring Security中不起作用
【发布时间】:2025-12-06 14:30:02
【问题描述】:

我正在尝试了解如何使用 Spring Security 保护 SOAP Web 服务。我看到DefaultSecurityFilterChain 是用antMatcher() 创建的,但我的网络服务仍然不需要任何凭据。如何使其安全?

我猜测我没有清楚地理解模式,我只是配置错误,但我找不到。

网络服务网址:http://localhost:8080/services/customer

我的配置:

@Configuration
@EnableWebSecurity(debug = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyBasicAuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user1").password(passwordEncoder().encode("user1Pass"))
                .authorities("ROLE_USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .antMatcher("/services/**")
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .httpBasic()
                .authenticationEntryPoint(authenticationEntryPoint);


        http.addFilterAfter(new CustomFilter(),
                BasicAuthenticationFilter.class);
    }
}

UPD:我昨天没有注意到这个,我只是查看了应用程序和网络服务的 URL,它们是不同的。这是因为 Apache CXF Web 服务是单独启动的。

@Bean
public Endpoint endpoint() {
    EndpointImpl endpoint = new EndpointImpl(springBus(), customerWebService );
    endpoint.publish("http://localhost:8080/services/customer");
    return endpoint;
}

而且我还没有找到任何可能的方法来启动 Web 服务并在应用程序内发布端点。看来这才是重点。

应用程序:http://localhost:8082/AppName/...

网络服务:http://localhost:8080/services/customer

【问题讨论】:

标签: java spring soap spring-security


【解决方案1】:

你能试试这个配置吗?如果您对每个用户都有特定的权限,那么您必须在给定的路径上要求它。

@Override
protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/services/**").hasAuthority("ROLE_USER")
                .anyRequest().authenticated()
                .and()
                .httpBasic()
                .authenticationEntryPoint(authenticationEntryPoint).and()
        .csrf().disable();


    http.addFilterAfter(new CustomFilter(),
            BasicAuthenticationFilter.class);
}

【讨论】:

  • 谢谢,我想这是一个解决方案。或许是我没想到的地方发现了问题。我更新了帖子。