2
0
Эх сурвалжийг харах

Improve client application by moving username/password specification to command-line.

Ben Alex 21 жил өмнө
parent
commit
7ae1844130

+ 11 - 10
docs/reference/src/index.xml

@@ -1564,7 +1564,7 @@ public boolean supports(Class clazz);</programlisting></para>
         be used instead of Container Adapters.</para>
       </sect2>
 
-      <sect2 id="security-ui-http-session">
+      <sect2 id="security-ui-http-basic">
         <title>HTTP Basic Authentication</title>
 
         <para>Primarily to cater for the needs of remoting protocols such as
@@ -2180,15 +2180,16 @@ $CATALINA_HOME/bin/startup.sh</programlisting></para>
       and Burlap protocols. This demonstrates how to use the Acegi Security
       System for Spring for authentication with Spring remoting protocols. To
       try this client, ensure your servlet container is still running the
-      Contacts sample application, and then execute <literal>client
-      marissa</literal>. This will use the remoting protocols to obtain the
-      list of contacts with the owner specified (in this case
-      <literal>marissa</literal>). Note you will be need to edit
-      <literal>client.properties</literal> to use a different username,
-      password, or target URL. To see that security does indeed work, try
-      running <literal>client scott</literal> before changing
-      <literal>client.properties</literal> to use <literal>scott</literal>'s
-      authentication details.</para>
+      Contacts sample application, and then execute <literal>client marissa
+      marissa koala</literal>. The command-line parameters respectively
+      represent the owner of the contacts to extract, the username to use, and
+      the password to use. Note that you may need to edit
+      <literal>client.properties</literal> to use a different target URL. To
+      see that security does indeed work, try running <literal>client scott
+      marissa koala</literal>, which will try to obtain
+      <literal>scott</literal>'s contacts when authenticating as
+      <literal>marissa</literal>. To see it work properly, use <literal>client
+      scott scott wombat</literal>.</para>
     </sect1>
 
     <sect1 id="security-become-involved">

+ 0 - 2
samples/contacts/client/client.properties

@@ -6,5 +6,3 @@
 serverName=localhost
 httpPort=8080
 contextPath=/contacts
-username=marissa
-password=koala

+ 0 - 4
samples/contacts/client/clientContext.xml

@@ -23,8 +23,6 @@
 		<property name="serviceUrl">
 			<value>http://${serverName}:${httpPort}${contextPath}/caucho/ContactManager-hessian</value>
 		</property>
-		<property name="username"><value>${username}</value></property>
-		<property name="password"><value>${password}</value></property>
 	</bean>
 
 	<!-- Proxy for the Burlap-exported ContactManager -->
@@ -36,8 +34,6 @@
 		<property name="serviceUrl">
 			<value>http://${serverName}:${httpPort}${contextPath}/caucho/ContactManager-burlap</value>
 		</property>
-		<property name="username"><value>${username}</value></property>
-		<property name="password"><value>${password}</value></property>
 	</bean>
 
 </beans>

+ 47 - 9
samples/contacts/src/main/java/sample/contact/ClientApplication.java

@@ -21,6 +21,9 @@ import org.springframework.context.support.FileSystemXmlApplicationContext;
 
 import org.springframework.util.StopWatch;
 
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
 import java.util.Iterator;
 import java.util.Map;
 
@@ -47,7 +50,8 @@ public class ClientApplication {
 
     //~ Methods ================================================================
 
-    public void invokeContactManager(String username, int nrOfCalls) {
+    public void invokeContactManager(String forOwner, String username,
+        String password, int nrOfCalls) {
         StopWatch stopWatch = new StopWatch(nrOfCalls
                 + " ContactManager call(s)");
         Map orderServices = this.beanFactory.getBeansOfType(ContactManager.class,
@@ -59,13 +63,44 @@ public class ClientApplication {
             ContactManager remoteContactManager = (ContactManager) orderServices
                 .get(beanName);
             System.out.println("Calling ContactManager '" + beanName
-                + "' for owner " + username);
+                + "' for owner " + forOwner);
+
+            Object object = this.beanFactory.getBean("&" + beanName);
+
+            try {
+                System.out.println("Trying to find setUsername(String) method");
+
+                Method method = object.getClass().getMethod("setUsername",
+                        new Class[] {String.class});
+                System.out.println("Found; Trying to setUsername(String) to "
+                    + username);
+                method.invoke(object, new Object[] {username});
+            } catch (NoSuchMethodException ignored) {
+                ignored.printStackTrace();
+            } catch (IllegalAccessException ignored) {
+                ignored.printStackTrace();
+            } catch (InvocationTargetException ignored) {
+                ignored.printStackTrace();
+            }
+
+            try {
+                System.out.println("Trying to find setPassword(String) method");
+
+                Method method = object.getClass().getMethod("setPassword",
+                        new Class[] {String.class});
+                method.invoke(object, new Object[] {password});
+                System.out.println("Found; Trying to setPassword(String) to "
+                    + password);
+            } catch (NoSuchMethodException ignored) {}
+             catch (IllegalAccessException ignored) {}
+             catch (InvocationTargetException ignored) {}
+
             stopWatch.start(beanName);
 
             Contact[] contacts = null;
 
             for (int i = 0; i < nrOfCalls; i++) {
-                contacts = remoteContactManager.getAllByOwner(username);
+                contacts = remoteContactManager.getAllByOwner(forOwner);
             }
 
             stopWatch.stop();
@@ -88,20 +123,23 @@ public class ClientApplication {
     public static void main(String[] args) {
         if ((args.length == 0) || "".equals(args[0])) {
             System.out.println(
-                "You need to specify a user ID and optionally a number of calls, e.g. for user marissa: "
-                + "'client marissa' for a single call per service or 'client marissa 10' for 10 calls each");
+                "You need to specify the owner to request contacts for, the user ID to use, the password to use, and optionally a number of calls, e.g. for user marissa: "
+                + "'client marissa marissa koala' for a single call per service or 'client marissa marissa koala 10' for 10 calls each");
         } else {
-            String username = args[0];
+            String forOwner = args[0];
+            String username = args[1];
+            String password = args[2];
+
             int nrOfCalls = 1;
 
-            if ((args.length > 1) && !"".equals(args[1])) {
-                nrOfCalls = Integer.parseInt(args[1]);
+            if ((args.length > 3) && !"".equals(args[3])) {
+                nrOfCalls = Integer.parseInt(args[3]);
             }
 
             ListableBeanFactory beanFactory = new FileSystemXmlApplicationContext(
                     "clientContext.xml");
             ClientApplication client = new ClientApplication(beanFactory);
-            client.invokeContactManager(username, nrOfCalls);
+            client.invokeContactManager(forOwner, username, password, nrOfCalls);
         }
     }
 }