Example Configuration in Swagger Secutiry with Spring Boot

Asked

Viewed 249 times

1

I am making a documented API in Swagger, when I enter the url of my api, it appears to make an authentication, because it has a Websecurityconfig class that does this for me, but I wanted to make an authentication for the methods of my API, someone knows how to do this?

Docket config:

@Bean
public Docket userApi() {
    return new Docket(DocumentationType.SWAGGER_2)
    .select()
    .apis(RequestHandlerSelectors.any())
    .paths(Predicates.not(PathSelectors.regex("/error.*")))
    .paths(PathSelectors.any())
    .build()
    .apiInfo(metaData());

}

Websecutiryconfig:

    package com.restfulproject.crud;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth)
      throws Exception {
        auth
          .inMemoryAuthentication()
          .withUser("user")
            .password("password")
            .roles("USER")
            .and()
          .withUser("admin")
            .password("admin")
            .roles("USER", "ADMIN");

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
          .authorizeRequests()
          .anyRequest()
          .authenticated()
          .and()
          .httpBasic()
          .and()
          .csrf().disable();
    }
}
  • when I enter the url of my api, it appears to make an authentication, ... however I wanted to make an authentication for the methods of my API - I don’t get it, you want authentication in the API, and you said you’re already authenticating in the API, what’s the problem?

  • I want to authenticate API methods

  • right, you have a user authentication method and password?

  • That’s right, but I wanted to use Swagger’s security

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.