我正在开发演示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);
}
Run Code Online (Sandbox Code Playgroud)
我最终得到了所有apis与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() …Run Code Online (Sandbox Code Playgroud)