Ну, похоже, никто не отвечает, поэтому я начну отвечать на свой вопрос шаг за шагом.
Фильтры - правильный подход к моей проблеме. Это не то, что характерно для tomcat или jetty, вместо этого они стандартизованы спецификацией Java Servlet версии 2.3. Таким образом, как tomcat, так и причал или любой другой веб-сервер, который реализует спецификацию Java Servlet, должны запускать фильтры.
В качестве примера для использования для фильтров, эти статьи оракула/java статьи :
«Запросы на аутентификацию-блокировка на основе идентификации пользователя».
это именно то, что я хочу сделать.
Я попытаюсь использовать фильтры и обновить этот ответ с моим прогрессом, пока не найду, что вопрос достаточно отвечен.
Step 1, preparations for Filter development
before you can start writing your own filter you have to setup your IDE for JavaEE development or write the required build files for your build tool. There are a lot of tutorials out there already explaining this.
for Example:
Eclipse + Tomcat
Maven + Jetty
Шаг 2, напишите самый простой фильтр hello world
package hello.world.filter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class HelloWorldFilter implements Filter {
public void init(FilterConfig fConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
PrintWriter out = response.getWriter();
out.print("hello World");
}
public void destroy() {
}
}
Step 3, deploy your filter on your running solr server
Now this step is not as easy as you might think it is. I thought i could just package my authentication filter into a war file and then use it for just any application i have running on any servlet server. Sadly it doesn't seem to work like that. Every webapp gets deployed on your server in its own context an cannot access the context of other webapps (at least on jetty as far as i know). Solr itself for example is a webapp, and has the context http://localhost:8983/solr/
. Even if you would deploy you webapp in the context http://localhost:8983/*
or http://localhost:8983/solr/*
this would not affect solr at all and everyone would still be able to access your solr server.
The solution is to integrate your filter into the solr webapp.
To do that you have to first package your HelloWorldFilter.class into a jar file. Then go to the directory where your solr server is located and unpack the solr.war file. (In your standard installation this would normally be solr-4.1.0/example/webapps). Now you have to change the context file for the solr webapp, because its no longer packaged in solr.war but instead consists of a directory. Got to solr-4.1.0/example/contexts and open solr.xml. Change the line /webapps/solr.war
into /webapps/solr
. Now you can actually deploy your filter: Copy your jar file into the folder solr-4.1.0/example/webapps/solr/WEB-INF/lib.
As a last step we have to let solr know that our filter should be deployed while solr starts. To do that open solr-4.1.0/example/webapps/solr/WEB-INF/web.xml and add the following lines:
HelloWorldFilter
hello.world.filter.HelloWorldFilter
перед линиями
SolrRequestFilter
...
А также объявление:
HelloWorldFilter
/*
перед любым другим отображением фильтра в файле.
Сэкономьте все и перезапустите свой solr-сервер. Вместо веб-интерфейса solr вы должны увидеть «hello world» в любом контексте после http://localhost: 8983/solr/
.
Теперь вы можете начать замену
PrintWriter out = response.getWriter();
out.print("hello World");
с вашей собственной аутентификацией внутри метода doFilter .