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

时间:2019-08-13 14:59:41

标签: java spring soap spring-security

我试图了解如何使用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);
    }
}

UPD::昨天我没有注意到这一点,只是查看了应用程序和Web服务的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/...

Web服务:http://localhost:8080/services/customer

1 个答案:

答案 0 :(得分: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);
}