21
21
import org .apache .commons .codec .binary .Base64 ;
22
22
23
23
import javax .servlet .*;
24
+ import com .google .common .base .Strings ;
24
25
import javax .servlet .http .HttpServletRequest ;
25
26
import javax .servlet .http .HttpServletResponse ;
26
27
import java .io .FileInputStream ;
31
32
public final class WwwAuthFilter implements Filter {
32
33
33
34
private static final String AUTH_PREFIX = "Basic " ;
35
+
36
+ private static final String GUEST = "guest" ;
37
+
38
+ private static final String ROOT = "root" ;
39
+
40
+ private String rootUsername ;
34
41
35
- private String root_username = "root" ;
36
-
37
- private String root_password = "root" ;
42
+ private String rootPassword ;
38
43
39
- private String guest_username = "guest" ;
44
+ private String guestUsername ;
40
45
41
- private String guest_password = "guest" ;
46
+ private String guestPassword ;
42
47
43
48
@ Override
44
49
public void init (final FilterConfig filterConfig ) throws ServletException {
@@ -50,10 +55,18 @@ public void init(final FilterConfig filterConfig) throws ServletException {
50
55
} catch (final IOException ex ) {
51
56
log .warn ("Cannot found auth config file, use default auth config." );
52
57
}
53
- root_username = props .getProperty ("root.username" , root_username );
54
- root_password = props .getProperty ("root.password" , root_password );
55
- guest_username = props .getProperty ("guset.username" , guest_username );
56
- guest_password = props .getProperty ("guset.password" , guest_password );
58
+ if (Strings .isNullOrEmpty (props .getProperty ("root.username" ))) {
59
+ rootUsername = "root" ;
60
+ } else {
61
+ rootUsername = props .getProperty ("root.username" );
62
+ }
63
+ if (Strings .isNullOrEmpty (props .getProperty ("guest.username" ))) {
64
+ guestUsername = "guest" ;
65
+ } else {
66
+ guestUsername = props .getProperty ("guest.username" );
67
+ }
68
+ rootPassword = props .getProperty ("root.password" , "root" );
69
+ guestPassword = props .getProperty ("guest.password" , "guest" );
57
70
}
58
71
59
72
@ Override
@@ -63,10 +76,10 @@ public void doFilter(final ServletRequest request, final ServletResponse respons
63
76
String authorization = httpRequest .getHeader ("authorization" );
64
77
if (null != authorization && authorization .length () > AUTH_PREFIX .length ()) {
65
78
authorization = authorization .substring (AUTH_PREFIX .length (), authorization .length ());
66
- if ((root_username + ":" + root_password ).equals (new String (Base64 .decodeBase64 (authorization )))) {
79
+ if ((rootUsername + ":" + rootPassword ).equals (new String (Base64 .decodeBase64 (authorization )))) {
67
80
authenticateSuccess (httpResponse , false );
68
81
chain .doFilter (httpRequest , httpResponse );
69
- } else if ((guest_username + ":" + guest_password ).equals (new String (Base64 .decodeBase64 (authorization )))) {
82
+ } else if ((guestUsername + ":" + guestPassword ).equals (new String (Base64 .decodeBase64 (authorization )))) {
70
83
authenticateSuccess (httpResponse , true );
71
84
chain .doFilter (httpRequest , httpResponse );
72
85
} else {
@@ -77,12 +90,12 @@ public void doFilter(final ServletRequest request, final ServletResponse respons
77
90
}
78
91
}
79
92
80
- private void authenticateSuccess (final HttpServletResponse response , boolean isGuset ) {
93
+ private void authenticateSuccess (final HttpServletResponse response , boolean isGuest ) {
81
94
response .setStatus (200 );
82
95
response .setHeader ("Pragma" , "No-cache" );
83
96
response .setHeader ("Cache-Control" , "no-store" );
84
97
response .setDateHeader ("Expires" , 0 );
85
- response .setHeader ("identify" , true == isGuset ? guest_username : root_username );
98
+ response .setHeader ("identify" , true == isGuest ? GUEST : ROOT );
86
99
}
87
100
88
101
private void needAuthenticate (final HttpServletRequest request , final HttpServletResponse response ) {
0 commit comments