Spring Security中的HTTP基本身份验证无法正常工作。

3
我正在尝试了解如何使用Spring Security保护SOAP Web服务。我看到DefaultSecurityFilterChain是用antMatcher()创建的,但我的Web服务仍然不需要任何凭据。如何使它变得安全?
我猜测我没有清楚地理解模式,只是做了错误的配置,但我找不到问题所在。
Web服务URL: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);
    }
}
更新:我昨天没有注意到这一点,我刚刚查看了应用程序和Web服务的URL,它们是不同的。这是因为Apache CXF Web服务是单独启动的。
@Bean
public Endpoint endpoint() {
    EndpointImpl endpoint = new EndpointImpl(springBus(), customerWebService );
    endpoint.publish("http://localhost:8080/services/customer");
    return endpoint;
}

我还没有找到任何可能的方法来启动应用程序内部的网络服务并发布端点。看起来这就是关键点。
应用程序:http://localhost:8082/AppName/... 网络服务:http://localhost:8080/services/customer

你的CustomFilter是做什么的? - NatFar
请参阅https://www.baeldung.com/spring-security-basic-authentication,其中包含与编程相关的内容。 - Kedar Joshi
CustomFilter只是一个记录过滤器。我暂时将其移除了。 - Woods
我用这篇文章作为例子,但是我在我的应用程序的特定问题上遇到了困难。谢谢。 - Woods
1个回答

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);
}

1
谢谢,我猜这是一个解决方案。也许我找到了我没想到的问题所在。我已经对帖子进行了更新。 - Woods

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接