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?
– nullptr
I want to authenticate API methods
– A. Carlos
right, you have a user authentication method and password?
– nullptr
That’s right, but I wanted to use Swagger’s security
– A. Carlos