Skip to content

Commit

Permalink
Add back missing mereged codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangxinguo committed May 26, 2017
1 parent 5fb55c2 commit 1a0b797
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
root.username=root
root.password=root
guest.password=guest
guest.username=guest
guest.password=guest
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.commons.codec.binary.Base64;

import javax.servlet.*;
import com.google.common.base.Strings;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
Expand All @@ -31,14 +32,18 @@
public final class WwwAuthFilter implements Filter {

private static final String AUTH_PREFIX = "Basic ";

private static final String GUEST = "guest";

private static final String ROOT = "root";

private String rootUsername;

private String root_username = "root";

private String root_password = "root";
private String rootPassword;

private String guest_username = "guest";
private String guestUsername;

private String guest_password = "guest";
private String guestPassword;

@Override
public void init(final FilterConfig filterConfig) throws ServletException {
Expand All @@ -50,10 +55,18 @@ public void init(final FilterConfig filterConfig) throws ServletException {
} catch (final IOException ex) {
log.warn("Cannot found auth config file, use default auth config.");
}
root_username = props.getProperty("root.username", root_username);
root_password = props.getProperty("root.password", root_password);
guest_username = props.getProperty("guset.username", guest_username);
guest_password = props.getProperty("guset.password", guest_password);
if (Strings.isNullOrEmpty(props.getProperty("root.username"))) {
rootUsername = "root";
} else {
rootUsername = props.getProperty("root.username");
}
if (Strings.isNullOrEmpty(props.getProperty("guest.username"))) {
guestUsername = "guest";
} else {
guestUsername = props.getProperty("guest.username");
}
rootPassword = props.getProperty("root.password", "root");
guestPassword = props.getProperty("guest.password", "guest");
}

@Override
Expand All @@ -63,10 +76,10 @@ public void doFilter(final ServletRequest request, final ServletResponse respons
String authorization = httpRequest.getHeader("authorization");
if (null != authorization && authorization.length() > AUTH_PREFIX.length()) {
authorization = authorization.substring(AUTH_PREFIX.length(), authorization.length());
if ((root_username + ":" + root_password).equals(new String(Base64.decodeBase64(authorization)))) {
if ((rootUsername + ":" + rootPassword).equals(new String(Base64.decodeBase64(authorization)))) {
authenticateSuccess(httpResponse, false);
chain.doFilter(httpRequest, httpResponse);
} else if ((guest_username + ":" + guest_password).equals(new String(Base64.decodeBase64(authorization)))) {
} else if ((guestUsername + ":" + guestPassword).equals(new String(Base64.decodeBase64(authorization)))) {
authenticateSuccess(httpResponse, true);
chain.doFilter(httpRequest, httpResponse);
} else {
Expand All @@ -82,7 +95,7 @@ private void authenticateSuccess(final HttpServletResponse response, boolean isG
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);
response.setHeader("identify", true == isGuset ? guest_username : root_username);
response.setHeader("identify", true == isGuset ? GUEST : ROOT);
}

private void needAuthenticate(final HttpServletRequest request, final HttpServletResponse response) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
root.username=root
root.password=root
guest.password=guest
guest.username=guest
guest.password=guest

0 comments on commit 1a0b797

Please sign in to comment.