11public class InsecureBasicAuth {
22 /**
33 * Test basic authentication with Apache HTTP request.
4- */
4+ */
55 public void testApacheHttpRequest (String username , String password ) {
6- {
6+
77 // BAD: basic authentication over HTTP
88 String url = "http://www.example.com/rest/getuser.do?uid=abcdx" ;
9- }
109
11- {
1210 // GOOD: basic authentication over HTTPS
13- String url = "https://www.example.com/rest/getuser.do?uid=abcdx" ;
14- }
11+ url = "https://www.example.com/rest/getuser.do?uid=abcdx" ;
1512
1613 HttpPost post = new HttpPost (url );
1714 post .setHeader ("Accept" , "application/json" );
1815 post .setHeader ("Content-type" , "application/json" );
19-
16+
2017 String authString = username + ":" + password ;
2118 byte [] authEncBytes = Base64 .getEncoder ().encode (authString .getBytes ());
2219 String authStringEnc = new String (authEncBytes );
@@ -28,15 +25,12 @@ public void testApacheHttpRequest(String username, String password) {
2825 * Test basic authentication with Java HTTP URL connection.
2926 */
3027 public void testHttpUrlConnection (String username , String password ) {
31- {
28+
3229 // BAD: basic authentication over HTTP
3330 String urlStr = "http://www.example.com/rest/getuser.do?uid=abcdx" ;
34- }
3531
36- {
3732 // GOOD: basic authentication over HTTPS
38- String urlStr = "https://www.example.com/rest/getuser.do?uid=abcdx" ;
39- }
33+ urlStr = "https://www.example.com/rest/getuser.do?uid=abcdx" ;
4034
4135 String authString = username + ":" + password ;
4236 String encoding = Base64 .getEncoder ().encodeToString (authString .getBytes ("UTF-8" ));
0 commit comments