1 17 package org.apache.geronimo.tomcat; 18 19 import java.io.BufferedReader ; 20 import java.io.InputStreamReader ; 21 import java.net.HttpURLConnection ; 22 import java.net.URL ; 23 import java.util.Map ; 24 import java.util.HashMap ; 25 import java.security.PermissionCollection ; 26 import java.security.Permissions ; 27 28 import javax.management.ObjectName ; 29 30 import org.apache.geronimo.tomcat.util.SecurityHolder; 31 import org.apache.geronimo.security.jacc.ComponentPermissions; 32 33 38 public class JAASSecurityTest extends AbstractWebModuleTest { 39 40 ObjectName appName = null; 41 42 public void testNotAuthorized() throws Exception { 43 44 startWebApp(); 45 46 HttpURLConnection connection = (HttpURLConnection ) new URL ("http://localhost:8181/test/protected/hello.txt").openConnection(); 48 connection.setInstanceFollowRedirects(false); 49 assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode()); 50 BufferedReader reader = new BufferedReader (new InputStreamReader (connection.getInputStream())); 52 assertEquals("<!-- Login Page -->", reader.readLine()); 53 reader.close(); 54 55 String cookie = connection.getHeaderField("Set-Cookie"); 56 cookie = cookie.substring(0, cookie.lastIndexOf(';')); 57 String location = "http://localhost:8181/test/protected/j_security_check?j_username=alan&j_password=starcraft"; 58 connection = (HttpURLConnection ) new URL (location).openConnection(); 59 connection.setRequestMethod("POST"); 60 connection.setRequestProperty("Cookie", cookie); 61 connection.setInstanceFollowRedirects(false); 62 assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, connection.getResponseCode()); 63 64 location = connection.getHeaderField("Location"); 65 connection = (HttpURLConnection ) new URL (location).openConnection(); 66 connection.setRequestProperty("Cookie", cookie); 67 connection.setInstanceFollowRedirects(true); 68 assertEquals(HttpURLConnection.HTTP_FORBIDDEN, connection.getResponseCode()); 69 connection.disconnect(); 70 71 stopWebApp(); 72 } 73 74 public void testBadAuthentication() throws Exception { 75 76 startWebApp(); 77 78 HttpURLConnection connection = (HttpURLConnection ) new URL ("http://localhost:8181/test/protected/hello.txt").openConnection(); 80 connection.setInstanceFollowRedirects(false); 81 assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode()); 82 83 BufferedReader reader = new BufferedReader (new InputStreamReader (connection.getInputStream())); 85 assertEquals("<!-- Login Page -->", reader.readLine()); 86 reader.close(); 87 88 String cookie = connection.getHeaderField("Set-Cookie"); 89 cookie = cookie.substring(0, cookie.lastIndexOf(';')); 90 String location = "http://localhost:8181/test/protected/j_security_check?j_username=alan&j_password=basspassword"; 91 92 connection = (HttpURLConnection ) new URL (location).openConnection(); 93 connection.setRequestMethod("POST"); 94 connection.setRequestProperty("Cookie", cookie); 95 connection.setInstanceFollowRedirects(true); 96 97 reader = new BufferedReader (new InputStreamReader (connection.getInputStream())); 99 assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode()); 100 101 location = connection.getHeaderField("Location"); 102 assertEquals("<!-- Not Authorized -->", reader.readLine()); 103 reader.close(); 104 105 connection.disconnect(); 106 107 stopWebApp(); 108 } 109 110 public void testGoodAuthentication() throws Exception { 111 115 } 153 154 protected void startWebApp() throws Exception { 155 Map initParams = new HashMap (); 158 initParams.put("userClassNames", "org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal"); 159 initParams.put("roleClassNames", "org.apache.geronimo.security.realm.providers.GeronimoGroupPrincipal"); 160 161 RealmGBean realm = new RealmGBean("org.apache.geronimo.tomcat.realm.TomcatJAASRealm", initParams); 162 realm.doStart(); 163 164 PermissionCollection excludedPermissions = new Permissions (); 165 PermissionCollection uncheckedPermissions = new Permissions (); 166 ComponentPermissions componentPermissions = new ComponentPermissions(excludedPermissions, uncheckedPermissions, new HashMap ()); 167 SecurityHolder securityHolder = new SecurityHolder(); 169 securityHolder.setSecurityRealm(securityRealmName); 170 setUpSecureAppContext(new HashMap (), 171 new HashMap (), 172 componentPermissions, 173 realm, 174 securityHolder); 175 } 176 177 protected void stopWebApp() throws Exception { 178 } 179 180 protected void setUp() throws Exception { 181 super.setUp(); 182 super.init("org.apache.geronimo.tomcat.realm.TomcatJAASRealm"); 183 setUpSecurity(); 184 } 185 186 protected void tearDown() throws Exception { 187 tearDownSecurity(); 188 super.tearDown(); 189 } 190 191 } 192 | Popular Tags |