'Swagger - KeyCloak Restrict Particular Endpoint with Bearer Only

Below code prevents loading swagger UI , I want to restrict only sepecific endpoints like /api/milk , api/pen (https://localhost:8080/mapi/v2/api/milk)

I'm using JAX-RS-Jersey, embeeded jetty with keycloak-jetty adapter

ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();

String webappLocation = "../../apiv2.war";
if (!new File(webappLocation).exists())
  webappLocation = "./UI/apiv2-webapp/";

WebAppContext apiv2WebAppContext = new WebAppContext(webappLocation, "/mapi/v2/");

ContextHandlerCollection secureContexts = new ContextHandlerCollection();
secureContexts.setHandlers(new Handler[] {apiv2WebAppContext});

SecurityHandler securityHandler = createBasicAuthSecurityHandler();
securityHandler.setHandler(secureContexts);

KeycloakJettyAuthenticator keycloakJettyAuthenticator = new KeycloakJettyAuthenticator();

AdapterConfig adapterConfig = new AdapterConfig();
adapterConfig.setRealm(realm);
adapterConfig.setAuthServerUrl(AuthURL);
adapterConfig.setResource(clientID);    
adapterConfig.setSslRequired(String.valueOf(SslRequired.NONE));
adapterConfig.setBearerOnly(true);
keycloakJettyAuthenticator.setAdapterConfig(adapterConfig);
securityHandler.setAuthenticator(keycloakJettyAuthenticator);        
contextHandlerCollection.addHandler(securityHandler);        


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source