Skip to content

Swagger Basic Authorization #3491

@alabotski

Description

@alabotski

SwagerUI still displays all the services and types

How to properly configure it?

Where to register the default logins and password for logging in?

And whether it is necessary to change SwaggerUI for authorization

Swagger - 1.5.16
SwaggerUI - 3.0.21

public class Bootstrap extends HttpServlet {
	@Override
	public void init(ServletConfig config) throws ServletException {
		Info info = new Info().title(DBO_REST).version(DBO_REST_VERSION);

		Swagger swagger = new Swagger().info(info);
		swagger.securityDefinition(BASIC_AUTH_SCHEME, new BasicAuthDefinition());
		swagger.tag(new Tag().name(REST_TAG_TRANSFER).description(REST_TAG_TRANSFER_DESCRIPTION).externalDocs(new ExternalDocs("Find out more", "http://swagger.io")));

		new SwaggerContextService().withServletConfig(config).updateSwagger(swagger);
	}
}
.................
@ApplicationPath("/dbo/rest")
public class Application extends Application {
	HashSet<Object> singletons = new HashSet<>();

	public Application() {
		String packages = ClassUtils.getPackageName(RestRoot.class);
		String filter = ApiAuthorizationFilterImpl.class.getCanonicalName();

		BeanConfig beanConfig = new BeanConfig();
		beanConfig.setFilterClass("by.swagger.ApiAuthorizationFilterImpl");
		beanConfig.setVersion(DBO_REST_VERSION);
		beanConfig.setSchemes(new String[] { DBO_REST_SCHEMES });
		beanConfig.setHost(DBO_REST_HOST);
		beanConfig.setBasePath(DBO_REST_PATH);
		beanConfig.setResourcePackage(packages);
		beanConfig.setTitle(DBO_REST);
		beanConfig.setPrettyPrint(true);
		beanConfig.setScan(true);
	}

	@Override
	public Set<Class<?>> getClasses() {
		HashSet<Class<?>> set = new HashSet<Class<?>>();
		set.add(io.swagger.jaxrs.listing.ApiListingResource.class);
		set.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);

		return set;
	}

	@Override
	public Set<Object> getSingletons() {
		return singletons;
	}
}
.................

@Path("/transfer")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Api(value = "/transfer", produces = "application/json, application/xml", consumes = "application/json, application/xml", authorizations = {
								@Authorization(value = BASIC_AUTH_SCHEME) }, tags = REST_TAG_TRANSFER)
public class TransferMoneyService {...
}

And another question

For what you need to set these values?

		beanConfig.setHost(DBO_REST_HOST);
		beanConfig.setBasePath(DBO_REST_PATH);

If the url is taken from index.html SwaggerUI
url: "https://localhost:8443/dbo/rest/swagger.json",

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions