authentication - Spring Boot Swagger UI - Protect UI Access -
i added simple swagger ui existing springboot rest api adding following class code:
@enableswagger2 @configuration public class swaggerconfig { @bean public docket api() { return new docket(documentationtype.swagger_2) .select() .paths(pathselectors.regex("/v1.*")) .build() .pathmapping("/") .apiinfo(metadata()); } private apiinfo metadata() { return new apiinfobuilder() .title("my awesome api") .description("some description") .version("1.0") .build(); } } my problem api should public, swagger docs should not. way of requesting authentication swagger documentation, knows simple way of achieving this?
i tried google find oath stuff, authentication endpoints not swagger documentation...
swagger docs available @ /v2/api-docs endpoint when swagger integrated spring boot application.
inorder protect resource , make use of spring security , restrict endpoint accessing docs
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-security</artifactid> </dependency> security configuration : restricting access endpoint users
@configuration @enablewebsecurity public class websecurityconfig extends websecurityconfigureradapter { @override protected void configure(httpsecurity http) throws exception { http .authorizerequests() .antmatchers("/v2/api-docs").authenticated() .and() .httpbasic(); } @autowired public void configureglobal(authenticationmanagerbuilder auth) throws exception { auth .inmemoryauthentication() .withuser("user").password("password").roles("user"); } } additionally, swagger-ui.html can secured based on requirement.
Comments
Post a Comment