欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > SpringDoc OpenApi学习笔记

SpringDoc OpenApi学习笔记

2024/10/25 8:24:59 来源:https://blog.csdn.net/gyfghh/article/details/142443504  浏览:    关键词:SpringDoc OpenApi学习笔记

SpringDoc OpenApi学习笔记

springboot 2.7.6+springdoc openapi1.7.0

1、依赖

<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-ui</artifactId><version>${springdoc.version}</version>
</dependency>

2、application.yaml

默认地址是http://localhost:8080/swagger-ui/swagger-ui/index.html、http://localhost:8080/v3/api-docs,可以在application.yaml中自定义

springdoc:api-docs:enabled: truepath: /v3/api-docsswagger-ui:path: /swagger-ui/index.htmlpersist-authorization: true

3、SpringDocConfig

package cn.darkiris.doc;import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SpringDocConfig {// swagger: http://localhost:10001/swagger-ui/swagger-ui/index.html// openapi: http://localhost:10001/v3/api-docs@Beanpublic OpenAPI customSpringDoc() {return new OpenAPI().info(new Info().title("User API").version("1.0.0").description("This is the api for user management."));}
}

4、与spring security整合

如果使用spring security记得放行接口文档

package cn.darkiris.security;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;import java.security.Provider;@Configuration
@EnableWebSecurity
@EnableMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class SecurityConfig {@Autowiredprivate CustomAuthenticationEntryPoint customAuthenticationEntryPoint;@Autowiredprivate CustomAccessDeniedHandler customAccessDeniedHandler;@Autowiredprivate JwtAuthenticationFilter jwtAuthenticationFilter;@Beanpublic AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {// 1、AuthenticationManager 委托给适当的 AuthenticationProvider(CustomAuthenticationProvider)// 2、AuthenticationProvider 调用 UserDetailsService(CustomUserDetailService) 的 loadUserByUsername 获取用户信息// 3、AuthenticationProvider 使用 PasswordEncoder(CustomPasswordEncoder) 对比 上一步获取到的用户密码// 4、如果验证成功,AuthenticationProvider(CustomAuthenticationProvider) 返回一个已认证的 Authentication 对象;如果失败,抛出异常return authenticationConfiguration.getAuthenticationManager();}@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.csrf().disable().exceptionHandling().authenticationEntryPoint(customAuthenticationEntryPoint).accessDeniedHandler(customAccessDeniedHandler).and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests().antMatchers("/api/user/login", "/api/user/register").permitAll().antMatchers("/v3/api-docs/**", "/swagger-ui/**", "/swagger-resources/**", "/webjars/**").permitAll().anyRequest().authenticated().and().addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);return http.build();}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com