【问题标题】:Using http-basic-authentication with spring webservices将 http-basic-authentication 与 spring webservices 一起使用
【发布时间】:2016-02-12 18:40:39
【问题描述】:

我跟着https://spring.io/guides/gs/producing-web-service/ 使用 Spring 实现了简单的 web 服务。一切都按预期进行。现在我正在尝试使用 javaconfig 向它添加 http 基本身份验证。我应该如何进行?我发现了一些使用 @EnableWebSecurity 的链接,但似乎没有任何效果...服务响应无需身份验证...

【问题讨论】:

    标签: spring spring-security spring-boot spring-ws basic-authentication


    【解决方案1】:

    只需像这样创建一个 Spring Security 配置文件:

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Autowired
        private void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth
                .inMemoryAuthentication()
                .withUser("user")
                .password("password")
                .roles("ROLE_USER");
        }
    
        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                .formLogin()
                .and()
            .authorizeRequests()
                .anyRequest()
                .authenticated();
        }
    

    这里有一个通知教程来跟进:http://www.mkyong.com/spring-security/spring-security-hello-world-annotation-example/

    【讨论】:

    • 感谢您的回答,不幸的是这没有帮助。服务无需身份验证即可响应...
    • 此时我们需要一些代码和项目结构来帮助您。
    • 我犯了一些基本错误...现在对我来说唯一可行的解​​决方案是将自定义 authenticationEntryPoint 添加到 httpBasic() ,它会关闭导致 java.lang.IllegalStateException: Committed... Anythig 的响应输出流其他,甚至 authenticationEntryPoint 中的 response.sendError(SC_UNAUTHORIZED) 导致成功执行请求......所以这意味着应用程序“知道”用户未通过身份验证(因为它调用 authenticationentrypoint)但它只是忽略它......
    • 感谢@Lukehey,有些教程有点臃肿,这简化了很多。
    【解决方案2】:

    根据您添加到问题中的标签,我看到您正在使用 Spring Boot 公开 SOAP 服务。在这种情况下,只需添加 spring-boot-starter-security Spring Boot starter project 作为依赖项。这将包括 Spring Security 和 by default ‘basic’ authentication is added on all HTTP endpoints(包括您的 SOAP 服务)。

    默认情况下,启动时会生成一个随机密码,但您可以使用 Spring Boot security properties 配置自己的用户名/密码。

    如果想了解更多信息,我创建了一篇说明 how to setup Spring WS basic authentication using Spring Boot 的博文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-24
      • 2011-02-23
      • 1970-01-01
      • 2020-09-10
      • 2019-01-17
      • 2013-06-12
      • 2011-11-01
      • 2018-10-08
      相关资源
      最近更新 更多