Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Swagger UI的基本身份验证_Java_Spring_Rest_Spring Boot_Swagger Ui - Fatal编程技术网

Java 使用Swagger UI的基本身份验证

Java 使用Swagger UI的基本身份验证,java,spring,rest,spring-boot,swagger-ui,Java,Spring,Rest,Spring Boot,Swagger Ui,我正试图通过SwiggerUI开发一个基于SpringBoot的RESTAPI服务,其中包含API文档。我想通过swagger UI启用基本身份验证,这样用户只能在使用swagger UI上的授权按钮进行身份验证后运行API的身份验证(通过该按钮,授权:基本XYZ头被添加到API调用中) 在前端(在Swagger UI的.json文件中,我使用以下代码为所有API添加了基本身份验证(根据文档): 我应该如何实现上述用例的后端逻辑(用户只能在使用swagger UI上的Authorization按

我正试图通过SwiggerUI开发一个基于SpringBoot的RESTAPI服务,其中包含API文档。我想通过swagger UI启用基本身份验证,这样用户只能在使用swagger UI上的授权按钮进行身份验证后运行API的身份验证(通过该按钮,授权:基本XYZ头被添加到API调用中)

在前端(在Swagger UI的.json文件中,我使用以下代码为所有API添加了基本身份验证(根据文档):

我应该如何实现上述用例的后端逻辑(用户只能在使用swagger UI上的Authorization按钮进行身份验证后运行API,否则在运行API时会显示401错误)


某些文档或示例代码可能会有所帮助

build.gradle中使用以下依赖项来启用安全性:

"org.springframework.boot:spring-boot-starter-security"

在application.properties中,您可以使用以下命令定义自己的用户名和密码:

spring.security.user.name=user
spring.security.user.password=password

一个选项是使用浏览器弹出授权

  • 当您为spring boot应用程序启用基本身份验证时,swagger ui将自动使用浏览器的弹出窗口进行基本身份验证。这意味着浏览器将保留发出请求的凭据,就像您尝试访问安全GET端点时一样,直到您关闭它
  • 现在,假设您不想使用上述内容,并且希望使用swagger ui进行基本身份验证,正如您所说,您必须在swagger ui上启用身份验证功能,并在访问swagger ui url时选择性地添加安全例外

  • 要启用基本的身份验证功能来招摇过市UI(使用UI中的“授权按钮”),您必须将安全上下文和方案设置为招摇过市摘要(这是一个简化版本):

  • 这将启用ui中的授权按钮。

    现在,您可能希望您的用户能够自由地访问swagger ui,并使用此按钮进行授权。为此,您必须为应用程序的基本身份验证豁免swagger。此配置的一部分是安全配置,您必须添加以下代码:

    public class SecurityConfig extends WebSecurityConfigurerAdapter{
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
                http
                    .httpBasic()
                    .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)   
                    .and().authorizeRequests()
                    .antMatchers(
                            "/", "/csrf", 
                            "/v2/api-docs", 
                            "/swagger-resources/**",
                            "/swagger-ui.html",
                            "/webjars/**"
                            ).permitAll()
                    .anyRequest().authenticated();
    
        }
    }
    

    那些只想为端点进行基本身份验证的人应该按照@Sifis编写的所有操作,但需要将antMatchers更改为:

    public class SecurityConfig extends WebSecurityConfigurerAdapter{
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            http
                .httpBasic()            
                .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)   
                .and().authorizeRequests()
                .antMatchers(
                        "/",
                        "/v2/api-docs/**",
                        "/v3/api-docs/**",
                        "/swagger-resources/**",
                        "/swagger-ui/**",
                        "/swagger-ui.html").permitAll()
                .anyRequest().authenticated();
    
        }
    }
    

    如果您使用的是HTTP Basic,则没有“授权”步骤。浏览器会向用户请求凭据并将其传入。是的,在应用程序的后端,我可以通过扩展
    websecurityConfigureAdapter
    类并覆盖
    configureGlobal来引入访问控制(AuthenticationManagerBuilder auth)
    通过
    jdbcAuthentication
    检查用户及其权限,并同样覆盖
    配置(HttpSecurity)
    只允许经过身份验证和授权的用户访问URL,对吗?是的,您可以。请注意,基本身份验证有许多缺点,因此可以用于测试和学习,但您确实应该在实际应用程序中使用类似OAuth2的功能。(另外,在引导应用程序中,整个应用程序都是100%受保护的REST服务是很常见的,在这种情况下,默认引导配置是保护所有内容。)是的,这是目前用于测试和学习的。目前,我想做的是让所有人都能够访问我的API UI,但只允许经过身份验证的用户能够调用API。在我实现上面指定的逻辑时,正如您所说,spring security正在保护整个应用程序,这不是我所需要的。我已经尝试过了使用
    配置(HttpSecurity)
    参数,但据我所知,它似乎不起作用,这使得它仅可供单个用户访问。如果我需要为多个用户扩展此功能,该怎么办?您可以使用Key斗篷来管理用户。Key斗篷有一个spring启动程序,使用起来很简单。以下是一个网站:这是一个关于Key斗篷与spring boot:t的良好教程hanks@Sifls。我确实已经关注了第二部分。我支持您关于在
    configure(HttpSecurity-http)中进行更改的说法
    并配置antMatchers以保护我需要保护的API。另外,您提到的另一个重要问题,我认为重点是使用无状态会话创建策略。感谢您的帮助
    public class SecurityConfig extends WebSecurityConfigurerAdapter{
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
                http
                    .httpBasic()
                    .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)   
                    .and().authorizeRequests()
                    .antMatchers(
                            "/", "/csrf", 
                            "/v2/api-docs", 
                            "/swagger-resources/**",
                            "/swagger-ui.html",
                            "/webjars/**"
                            ).permitAll()
                    .anyRequest().authenticated();
    
        }
    }
    
    public class SecurityConfig extends WebSecurityConfigurerAdapter{
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
            http
                .httpBasic()            
                .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)   
                .and().authorizeRequests()
                .antMatchers(
                        "/",
                        "/v2/api-docs/**",
                        "/v3/api-docs/**",
                        "/swagger-resources/**",
                        "/swagger-ui/**",
                        "/swagger-ui.html").permitAll()
                .anyRequest().authenticated();
    
        }
    }