在 Swagger 中记录 Spring 的登录/注销 API
2022-09-03 00:58:16
我正在开发演示REST服务,使用用户必须登录才能执行某些操作子集的地方。使用该简单配置添加(使用库)后:Spring Boot
Swagger UI
springfox
@Bean
public Docket docApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(any())
.paths(PathSelectors.ant("/api/**"))
.build()
.pathMapping("/")
.apiInfo(apiInfo())
.directModelSubstitute(LocalDate.class, String.class)
.useDefaultResponseMessages(true)
.enableUrlTemplating(true);
}
我最终得到了所有api,所有操作都列在页面上。不幸的是,我没有列出登录/注销端点。Swagger UI
问题是部分操作无法通过内置表单执行(我发现它非常好的功能,并希望使其工作),因为用户未登录。这个问题有什么解决办法吗?我可以在 中手动定义一些端点吗?Swagger UI
Swagger
如果有提交凭据的表单(即登录/注销端点),我可以在使用该受保护的端点之前执行授权。然后,用户可以从响应中提取并将其粘贴到通过 定义的自定义查询参数中。Swagger
token/sessionid
@ApiImplicitParams
您可以在下面找到我的安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginProcessingUrl("/api/login")
.usernameParameter("username")
.passwordParameter("password")
.successHandler(new CustomAuthenticationSuccessHandler())
.failureHandler(new CustomAuthenticationFailureHandler())
.permitAll()
.and()
.logout()
.logoutUrl("/api/logout")
.logoutSuccessHandler(new CustomLogoutSuccessHandler())
.deleteCookies("JSESSIONID")
.permitAll()
.and()
.csrf()
.disable()
.exceptionHandling()
.authenticationEntryPoint(new CustomAuthenticationEntryPoint())
.and()
.authorizeRequests()
.and()
.headers()
.frameOptions()
.disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}