/** * This is an example of HTTP basic Authentication. * It will require visitor to input a user name (xfeep) and password (hello!) * otherwise it will return 401 Unauthorized or BAD USER & PASSWORD */ publicclassBasicAuthHandlerimplementsNginxJavaRingHandler{
@Override public Object[] invoke(Map<String, Object> request) { String auth = (String) ((Map)request.get(HEADERS)).get("authorization"); if (auth == null) { returnnew Object[] { 401, ArrayMap.create("www-authenticate", "Basic realm=\"Secure Area\""), "<HTML><BODY><H1>401 Unauthorized.</H1></BODY></HTML>" }; } String[] up = new String(DatatypeConverter.parseBase64Binary(auth.substring("Basic ".length())), DEFAULT_ENCODING).split(":"); if (up[0].equals("xfeep") && up[1].equals("hello!")) { return PHASE_DONE; } returnnew Object[] { 401, ArrayMap.create("www-authenticate", "Basic realm=\"Secure Area\""), "<HTML><BODY><H1>401 Unauthorized BAD USER & PASSWORD.</H1></BODY></HTML>" }; } }