17

Spring Boot特定の操作のサブセットを実行するためにユーザーがログインする必要がある場所を使用して、デモ REST サービスを開発しています。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());
}
4

4 に答える 4