您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用web過濾器增加solr后臺登錄驗證”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用web過濾器增加solr后臺登錄驗證”吧!
solr后臺自帶是沒有登錄功能的,默認訪問地址是:http://localhost:8983/solr/#/(內置jetty運行)。
要給sorl后臺增加登錄驗證方法:
1、使用web服務器的登錄驗證(例如jetty、tomcat)
這種方式好處是不需要另外寫代碼,只需配置一下對應的web服務器就行了。
2、使用web過濾器過濾地址驗證
這種方法是更加靈活,缺點是需要點代碼開發。
本文介紹的是第二種方法:
SolrLoginFilter.java package com.penngo.solr; 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; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.lang3.StringUtils; public class SolrLoginFilter implements Filter { public void init(FilterConfig config) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest)request; HttpServletResponse httpResponse = (HttpServletResponse)response; HttpSession session = httpRequest.getSession(); String url = httpRequest.getRequestURL().toString(); String userAgent = httpRequest.getHeader("user-agent"); String query = httpRequest.getQueryString(); String servletPath = httpRequest.getServletPath(); String contextPath = httpRequest.getContextPath(); System.out.println("url=======" + ", url:" + url + ", servletPath:" + servletPath + ", contextPath:" + contextPath + ", query:" + query + ", userAgent:" + userAgent); Object isLogin = session.getAttribute("isLogin"); if(isLogin == null || ((boolean)isLogin) != true) { String user = StringUtils.isEmpty(request.getParameter("user")) == false ? request.getParameter("user") : ""; String password = StringUtils.isEmpty(request.getParameter("password")) == false ? request.getParameter("password") : ""; if(user.equals("admin") == true && password.equals("123") == true) { session.setAttribute("isLogin", true); httpResponse.sendRedirect(contextPath); return; } else { httpRequest.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println(loginPage()); out.close(); return; } } chain.doFilter(request, response); } public void destroy() { } private String loginPage() { String page = "<html>\n" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n" + "<body>\n" + "<form action=\"/solr/login\">\n" + "用戶名:<input type=\"text\" name=\"user\" value=\"\"></br></br>\n" + "密  碼:<input type=\"password\" name=\"password\"></br></br>\n" + "<button type=\"submit\" >登錄 </button>\n" + "</form>\n" + "</body\n" + "</html>"; return page; } }
在原來的web.xml中增加過濾器配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5" metadata-complete="true" > <!-- 配置登錄過濾器 --> <filter> <filter-name>SolrLoginFilter</filter-name> <filter-class>com.penngo.solr.SolrLoginFilter</filter-class> </filter> <filter-mapping> <filter-name>SolrLoginFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <!-- 配置登錄過濾器 --> <!-- Any path (name) registered in solrconfig.xml will be sent to that filter --> <filter> <filter-name>SolrRequestFilter</filter-name> <filter-class>org.apache.solr.servlet.SolrDispatchFilter</filter-class> <!-- Exclude patterns is a list of directories that would be short circuited by the SolrDispatchFilter. It includes all Admin UI related static content. NOTE: It is NOT a pattern but only matches the start of the HTTP ServletPath. --> <init-param> <param-name>excludePatterns</param-name> <param-value>/partials/.+,/libs/.+,/css/.+,/js/.+,/img/.+,/templates/.+</param-value> </init-param> </filter> <filter-mapping> <filter-name>SolrRequestFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>LoadAdminUI</servlet-name> <servlet-class>org.apache.solr.servlet.LoadAdminUiServlet</servlet-class> </servlet> <servlet> <servlet-name>SolrRestApi</servlet-name> <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class> <init-param> <param-name>org.restlet.application</param-name> <param-value>org.apache.solr.rest.SolrSchemaRestApi</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>LoadAdminUI</servlet-name> <url-pattern>/index.html</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>SolrRestApi</servlet-name> <url-pattern>/schema/*</url-pattern> </servlet-mapping> <mime-mapping> <extension>.xsl</extension> <!-- per http://www.w3.org/TR/2006/PR-xslt20-20061121/ --> <mime-type>application/xslt+xml</mime-type> </mime-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- Get rid of error message --> <security-constraint> <web-resource-collection> <web-resource-name>Disable TRACE</web-resource-name> <url-pattern>/</url-pattern> <http-method>TRACE</http-method> </web-resource-collection> <auth-constraint/> </security-constraint> <security-constraint> <web-resource-collection> <web-resource-name>Enable everything but TRACE</web-resource-name> <url-pattern>/</url-pattern> <http-method-omission>TRACE</http-method-omission> </web-resource-collection> </security-constraint> </web-app>
運行結果:
感謝各位的閱讀,以上就是“怎么用web過濾器增加solr后臺登錄驗證”的內容了,經過本文的學習后,相信大家對怎么用web過濾器增加solr后臺登錄驗證這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。