📄 loginservlet.java
字号:
throw new LoginRequiredException(sr.getURL().getProtocol(), sr.getURL().getHost(), sr.getURL().getPort(), "TestRealm"); } catch (IOException e) { throw new SecurityException( "" + e ); } } } /** * An <code>AccessGuard</code> which will try to guess passwords. * <p> * If unable to access a result, this <code>AccessGuard</code> will * use a list of <code>user:password</code> pairs to try and guess. * If a correct guess is made, the site / user:password pairing * is added to the <code>AccessGuard</code>'s authorization list. */ protected class GuessingAccessGuard extends RandomAccessGuard // Change to RandomAccessGuard for testing { private Set guesses; private Set filteredSites; public GuessingAccessGuard(Set guesses, Set filteredSites) { super(); if ((guesses==null) || (filteredSites==null)) throw new IllegalArgumentException("null set not allowed"); this.guesses = guesses; this.filteredSites = filteredSites; } public void checkGuardUncached(Object o) throws SecurityException { try { super.checkGuardUncached(o); return; } catch (LoginRequiredException e) { Iterator it = guesses.iterator(); boolean madeGuess = false; while (it.hasNext()) { String userpass = (String)it.next(); int colon = userpass.indexOf(':'); String user = userpass.substring(0,colon); String pass = userpass.substring(colon+1); super.addAuthorization( e, user, pass ); madeGuess = true; try { super.checkGuardUncached(o); return; } catch (LoginRequiredException e2) { // Bad Guess }; } // We tried all the guesses without success, clear out the last guess if (madeGuess) super.addAuthorization( e, "", "" ); filteredSites.add(e); throw e; } } /** * Add an authorization entry. * * @param protocol a protocol (http, https, etc.) * @param host a hostname * @param port a port number * @param realm a realm * @param username a username * @param password a password */ public synchronized void addAuthorization(String protocol, String host, int port, String realm, String username, String password) { super.addAuthorization(protocol, host, port, realm, username, password); // Since we've had a new login, clear the entire cache // of allowed and forbidden search results. // This is more strict than necessary, but safe, considering // a) User may change username/password for already authorized // results. // b) We may be able to use the new username/password to guess // for currently un-authorized results. super.cacheMap().clear(); } } /** * This is a useful class for testing. This class will "randomly" * throw <code>SecurityException</code> or * <code>LoginRequiredException</code> when checking security * access rights. */ class RandomAccessGuard extends AccessGuard { int counter = 0; public void checkGuardUncached(Object o) throws SecurityException { if (o==null || !(o instanceof SearchResult)) return; SearchResult sr = (SearchResult) o; counter++; if (counter % 3 == 0) throw new SecurityException( this.getClass().getName() + ": " + o.toString() ); if (counter % 3 == 1) try { throw new LoginRequiredException(sr.getURL().getProtocol(), sr.getURL().getHost(), sr.getURL().getPort(), "TestRealm"); } catch (IOException e) { throw new SecurityException( "" + e ); } return; } public String toString() { return this.getClass().getName() + "(counter=" + counter + ")"; } } /** * Display a form for the search user to login to a site. * When the form is submitted, we'll wind up at doLogin. */ private void doLoginForm(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { HttpSession session = req.getSession(false); String la = req.getParameter("la"); Locale locale = la_locale(la); String col = req.getParameter("col"); if (col==null) { Iterator it = searchMenu.entrySet().iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry)it.next(); String id = (String)e.getKey(); SearchMenuItem sm = (SearchMenuItem) e.getValue(); Searchable s = sm.searchable; if (locale==null || s.getLocales().contains(locale)) { col = id; break; } } } SearchMenuItem sm = (SearchMenuItem)searchMenu.get(col); SearchServer server = sm.server; Searchable searchable = sm.searchable; if (session!=null) { Map m = (Map)session.getAttribute("searchables"); if (m!=null) { Searchable s = (Searchable)m.get(col); if (s!=null) searchable = s; } } if (locale==null) locale = searchable.getLocale(); if (la==null) la = locale_la(locale); String charset = la_charset(la); Map textMap = la_textMap(la); resp.setContentType("text/html; charset="+charset); PrintWriter out = resp.getWriter(); out.print("<!DOCTYPE HTML PUBLIC"); out.println(" \"-//W3C//DTD HTML 4.01 Transitional//EN\""); out.println(" \"http://www.w3.org/TR/html4/loose.dtd\">"); out.println("<html>"); out.println("<head>"); out.print("<meta http-equiv=\"Content-type\" content=\""); out.print("text/html; charset="); out.print(charset); out.println("\">"); out.print("<title>"); out.print(text(textMap,"Login")); out.println("</title>"); out.println("<meta name=robots content=none>"); out.println("</head>"); out.println("<body bgcolor=\"#ffffff\">"); DEBUG(resp,"doLoginForm"); String protocol = req.getParameter("protocol"); String host = req.getParameter("host"); int port = getIntParameter(req,"port",80); String realm = req.getParameter("realm"); String site = new URL(protocol,host,(port!=80)?port:-1,"/").toString(); out.print("<form name=login method=POST accept-charset=\""); out.print(charset); out.print("\" action=\""); printHTMLEncoded(out,req.getRequestURI()); out.println("\">"); out.println("<table cellspacing=3 cellpadding=0 width=\"100%\">"); out.println("<tr valign=top>"); out.println("<td width=\"67%\">"); out.println("<table cellspacing=0 cellpadding=6 border=1 width=\"100%\" class=query>"); out.println("<tr>"); out.println("<td>"); out.println("<table cellspacing=0 cellpadding=0 width=\"100%\">"); out.println("<tr>"); out.print("<td align=right>"); out.print(text(textMap,"Site")); out.println(": </td>"); out.print("<td width=\"75%\">"); out.print(site); out.println("</td>"); out.println("</tr>"); out.println("<tr>"); out.print("<td align=right>"); out.print(text(textMap,"Realm")); out.println(": </td>"); out.print("<td width=\"75%\">"); out.print(realm); out.println("</td>"); out.println("</tr>"); out.println("<tr>"); out.print("<td align=right>"); out.print(text(textMap,"Username")); out.println(": </td>"); out.print("<td width=\"75%\">"); out.print("<input type=text name=username size=16>"); out.println("</td>"); out.println("</tr>"); out.println("<tr>"); out.print("<td align=right>"); out.print(text(textMap,"Password")); out.println(": </td>"); out.print("<td width=\"75%\">"); out.print("<input type=password name=password size=16>"); out.println("</td>"); out.println("</tr>"); out.println("<tr>"); out.print("<td align=right>"); out.print("<input type=checkbox name=othersites checked>"); out.println("</td width=\"75%\">"); out.print("<td>"); out.print(text(textMap, "Automatically try to use this password "+ "for other sites, too.")); out.println("</td>"); out.println("</tr>"); out.println("<tr>"); out.print("<td align=right></td>"); out.print("<td width=\"75%\">"); out.print("<input type=submit value=\" login \">"); out.println("</td>"); out.println("</tr>"); out.println("</table>"); out.println("</td>"); out.println("</tr>"); out.println("</table>"); out.println("</td>"); out.println("<td width=\"33%\" bgcolor=\"#c0c0c0\" class=tip>"); out.println("<table width=\"100%\">"); out.println("<tr>"); out.println("<td>"); out.println(" "); out.println("</td>"); out.println("</tr>"); out.println("</table>"); out.println("</td>"); out.println("</tr>"); out.println("</table>"); Enumeration enumeration = req.getParameterNames(); while (enumeration.hasMoreElements()) { String name = (String)enumeration.nextElement(); if (name.equals("username")) continue; if (name.equals("password")) continue; boolean decode = (name.equals("qp") || name.equals("oq") || name.equals("qt") || name.startsWith("tx")); String[] values = req.getParameterValues(name); for (int i=0; i<values.length; i++) { String value = values[i]; out.print("<input type=hidden name=\""); printHTMLEncoded(out,name); out.print("\" value=\""); printHTMLEncoded(out,value); out.println("\">"); } } out.println("</form>"); DEBUG(resp,"/doLoginForm"); out.println("</body>"); out.println("</html>"); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { try { doLogin(req,resp); } catch (Exception e) { resp.setStatus(500);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -