Http 基本身份验证在 Spring Security 中不起作用

Woo*_*ods 5 java spring soap spring-security

我试图了解如何使用 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

小智 0

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

@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);
}
Run Code Online (Sandbox Code Playgroud)