# Using RestHandlers for custom Authentication Plugin

**URL:** <https://discuss.elastic.co/t/using-resthandlers-for-custom-authentication-plugin/90709>\
**Category:** Elasticsearch\
**Created:** [June 24, 2017, 10:11am UTC](https://discuss.elastic.co/t/using-resthandlers-for-custom-authentication-plugin/90709 "2017-06-24T10:11:32Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![chanchal](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@chanchal](https://discuss.elastic.co/u/chanchal)\
**Post date:** [July 4, 2017, 4:18pm UTC](https://discuss.elastic.co/t/using-resthandlers-for-custom-authentication-plugin/90709/3 "2017-07-04T16:18:28Z")

</div>

Thanks for the reply. I was able to create a basic plugin for the same in the following way..  
My plugin class:  
public class ESPlugin extends Plugin implements ActionPlugin {

```
public List<RestHandler> getRestHandlers(Settings settings, RestController restController,
		ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter,
		IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
	return Arrays.asList(new AuthenticationHandler(settings, restController));
}

@Override
public List<Class<? extends RestHandler>> getRestHandlers() {
	return Arrays.asList(AuthenticationHandler.class);
}

```

}

My Handler:  
public class AuthenticationHandler extends BaseRestHandler {

```
@Inject
public AuthenticationHandler(Settings settings, RestController controller) {
	super(settings);
	controller.registerFilter(new AuthenticationFilter());
}

@Override
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
	return channel -> {
		XContentBuilder builder = channel.newBuilder();
		builder.startObject();
		builder.endObject();
		channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
	};
}

```

}

My authentication filter that does the actual work:  
public class AuthenticationFilter extends RestFilter {

```
@Override
public void process(RestRequest request, RestChannel channel, NodeClient client, RestFilterChain filterChain)
		throws Exception {
	try {
		if (authenticate(request)) {
			filterChain.continueProcessing(request, channel, client);
		} else {
			final ElasticsearchException exception = new ElasticsearchException("Did not find authentication");
			XContentBuilder s = XContentFactory.jsonBuilder().startObject().field(request.uri())
					.field(request.rawPath()).endObject();
			channel.sendResponse(new BytesRestResponse(channel, RestStatus.FORBIDDEN, exception));
		}
	} catch (Exception e) {
		channel.sendResponse(new BytesRestResponse(channel, RestStatus.FORBIDDEN, e));
		return;
	}

}

```

}

---

_[View the full topic](https://discuss.elastic.co/t/using-resthandlers-for-custom-authentication-plugin/90709)._
