Java 使用Swagger UI的基本身份验证
我正试图通过SwiggerUI开发一个基于SpringBoot的RESTAPI服务,其中包含API文档。我想通过swagger UI启用基本身份验证,这样用户只能在使用swagger UI上的授权按钮进行身份验证后运行API的身份验证(通过该按钮,授权:基本XYZ头被添加到API调用中) 在前端(在Swagger UI的.json文件中,我使用以下代码为所有API添加了基本身份验证(根据文档): 我应该如何实现上述用例的后端逻辑(用户只能在使用swagger UI上的Authorization按钮进行身份验证后运行API,否则在运行API时会显示401错误)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按
某些文档或示例代码可能会有所帮助在build.gradle中使用以下依赖项来启用安全性:
"org.springframework.boot:spring-boot-starter-security"
在application.properties中,您可以使用以下命令定义自己的用户名和密码:
spring.security.user.name=user
spring.security.user.password=password
一个选项是使用浏览器弹出授权
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();
}
}