#5207 Add validation of credentials on target server before starting application...
authorCherian Mathew <c.mathew@bgbm.org>
Fri, 27 Nov 2015 10:48:54 +0000 (11:48 +0100)
committerCherian Mathew <c.mathew@bgbm.org>
Fri, 27 Nov 2015 10:48:54 +0000 (11:48 +0100)
eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/cdm/api/application/CdmApplicationRemoteConfiguration.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/store/LoginManager.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/RemotingLoginDialog.java

index 52bb763b0aea0be09eaa91bdbd215714e1dff59c..47757c9863f8fc05198f383250ff08ddeeb690a9 100644 (file)
@@ -303,6 +303,11 @@ public class CdmApplicationRemoteConfiguration implements ICdmApplicationConfigu
         return (IUserService) getService(IUserService.class, "/remoting-public/user.service", new CdmServiceRequestExecutor());\r
     }\r
 \r
         return (IUserService) getService(IUserService.class, "/remoting-public/user.service", new CdmServiceRequestExecutor());\r
     }\r
 \r
+\r
+    public static IUserService getUserService(ICdmRemoteSource remoteSource) {\r
+        return (IUserService) getService(IUserService.class, "/remoting-public/user.service", remoteSource, new CommonsHttpInvokerRequestExecutor());\r
+    }\r
+\r
     @Override\r
     public IMetadataService getMetadataService() {\r
         return (IMetadataService) getService(IMetadataService.class, "/remoting-public/metadata.service", new CommonsHttpInvokerRequestExecutor());\r
     @Override\r
     public IMetadataService getMetadataService() {\r
         return (IMetadataService) getService(IMetadataService.class, "/remoting-public/metadata.service", new CommonsHttpInvokerRequestExecutor());\r
@@ -328,17 +333,22 @@ public class CdmApplicationRemoteConfiguration implements ICdmApplicationConfigu
         if(authenticationManager != null) {\r
             return authenticationManager;\r
         }\r
         if(authenticationManager != null) {\r
             return authenticationManager;\r
         }\r
+\r
+        authenticationManager = getAuthenticationManager(getUserService());\r
+        return authenticationManager;\r
+    }\r
+\r
+    public static ProviderManager getAuthenticationManager(IUserService userService) {\r
         Md5PasswordEncoder passwordEncoder = new Md5PasswordEncoder();\r
         ReflectionSaltSource saltSource = new ReflectionSaltSource();\r
         saltSource.setUserPropertyToUse("getUsername");\r
 \r
         DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();\r
         Md5PasswordEncoder passwordEncoder = new Md5PasswordEncoder();\r
         ReflectionSaltSource saltSource = new ReflectionSaltSource();\r
         saltSource.setUserPropertyToUse("getUsername");\r
 \r
         DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();\r
-        daoAuthenticationProvider.setUserDetailsService(getUserService());\r
+        daoAuthenticationProvider.setUserDetailsService(userService);\r
         daoAuthenticationProvider.setSaltSource(saltSource);\r
         daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);\r
 \r
         daoAuthenticationProvider.setSaltSource(saltSource);\r
         daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);\r
 \r
-        authenticationManager = new ProviderManager(Arrays.asList((AuthenticationProvider)daoAuthenticationProvider));\r
-        return authenticationManager;\r
+        return new ProviderManager(Arrays.asList((AuthenticationProvider)daoAuthenticationProvider));\r
     }\r
 \r
 \r
     }\r
 \r
 \r
index 04f2d13d0f9b815556e2e074fa9e51a2bd27eb79..f56a8293dc6d98e76f4f024b9abc8e668cfe4874 100644 (file)
@@ -46,8 +46,12 @@ public class LoginManager extends Observable implements IConversationEnabled, IC
 
        private ConversationHolder conversation;
 
 
        private ConversationHolder conversation;
 
+       public static final String INCORRECT_CREDENTIALS_MESSAGE = "Login and/or Password incorrect";
+       public static final String ACCOUNT_LOCKED_MESSAGE = "Account is locked";
+       public static final String EMPTY_CREDENTIALS_MESSAGE = "Login and/or Password empty";
+
        public LoginManager(){
        public LoginManager(){
-               CdmStore.getContextManager().addContextListener(this);
+           CdmStore.getContextManager().addContextListener(this);
        }
 
        /**
        }
 
        /**
@@ -107,15 +111,16 @@ public class LoginManager extends Observable implements IConversationEnabled, IC
                    this.notifyObservers();
                }
            } catch(BadCredentialsException e){
                    this.notifyObservers();
                }
            } catch(BadCredentialsException e){
-               throw new CdmAuthenticationException("Login and/or Password incorrect", e);
+               throw new CdmAuthenticationException(INCORRECT_CREDENTIALS_MESSAGE, e);
            } catch(LockedException e){
            } catch(LockedException e){
-               throw new CdmAuthenticationException("Account is locked", e);
+               throw new CdmAuthenticationException(ACCOUNT_LOCKED_MESSAGE, e);
            } catch(IllegalArgumentException e){
            } catch(IllegalArgumentException e){
-               throw new CdmAuthenticationException("Login and/or Password empty", e);
+               throw new CdmAuthenticationException(EMPTY_CREDENTIALS_MESSAGE, e);
            }
 
        }
 
            }
 
        }
 
+
        private void _logGrantedAuthotities(StringBuilder gaText, String indent,
                        Set<GrantedAuthority> gaSet) {
                for(GrantedAuthority ga : gaSet){
        private void _logGrantedAuthotities(StringBuilder gaText, String indent,
                        Set<GrantedAuthority> gaSet) {
                for(GrantedAuthority ga : gaSet){
index a46299076a3daaa045f0c8ed8a7bad7e6b7c449a..92824fcd6cdef7f56da83515276177637f22ed44 100644 (file)
@@ -49,8 +49,13 @@ import org.eclipse.ui.progress.IProgressConstants;
 import org.eclipse.wb.swt.SWTResourceManager;
 import org.osgi.service.prefs.BackingStoreException;
 import org.osgi.service.prefs.Preferences;
 import org.eclipse.wb.swt.SWTResourceManager;
 import org.osgi.service.prefs.BackingStoreException;
 import org.osgi.service.prefs.Preferences;
+import org.springframework.security.authentication.BadCredentialsException;
+import org.springframework.security.authentication.LockedException;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 
 
+import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteConfiguration;
 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
+import eu.etaxonomy.cdm.api.service.IUserService;
 import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
 import eu.etaxonomy.cdm.model.metadata.CdmMetaData;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
@@ -62,6 +67,7 @@ import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo;
 import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo.CdmInstanceInfo;
 import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.remoting.source.CdmServerInfo.CdmInstanceInfo;
 import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
 import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.store.LoginManager;
 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 import eu.etaxonomy.taxeditor.webapp.CDMEmbeddedServerException;
 import eu.etaxonomy.taxeditor.webapp.CDMServer;
 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 import eu.etaxonomy.taxeditor.webapp.CDMEmbeddedServerException;
 import eu.etaxonomy.taxeditor.webapp.CDMServer;
@@ -507,15 +513,17 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
 
 
     private void refreshCdmServer() {
 
 
     private void refreshCdmServer() {
+        txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
+        clearOnServerChange();
+        emptyCredentials();
+        updateSelectedCdmServer();
         Display.getDefault().asyncExec(new Runnable() {
             @Override
             public void run() {
         Display.getDefault().asyncExec(new Runnable() {
             @Override
             public void run() {
-                txtCdmServerStatus.setText(STATUS_CHECKING_AVAILABILITY);
-                updateSelectedCdmServer();
                 checkSelectedCdmServer();
                 checkSelectedCdmServer();
-                updatePort();
             }
         });
             }
         });
+
     }
 
     private void updateSelectedCdmServer() {
     }
 
     private void updateSelectedCdmServer() {
@@ -550,10 +558,6 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
     }
 
     private void checkSelectedCdmServer() {
     }
 
     private void checkSelectedCdmServer() {
-
-        clearOnServerChange();
-        emptyCredentials();
-
         if(selectedCsii != null) {
             if(selectedCsii.isLocalhost()) {
                 txtPort.setEditable(true);
         if(selectedCsii != null) {
             if(selectedCsii.isLocalhost()) {
                 txtPort.setEditable(true);
@@ -570,6 +574,7 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
                 disableCdmInstanceControls("", "");
             }
         }
                 disableCdmInstanceControls("", "");
             }
         }
+        updatePort();
     }
 
 
     }
 
 
@@ -855,7 +860,7 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
 
         ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
 
 
         ICdmRemoteSource source = selectedCsii.getCdmRemoteSource(selectedCdmInstance, getPort());
 
-        if(!validateLogin()) {
+        if(!validateLogin(source)) {
             return;
         }
 
             return;
         }
 
@@ -934,7 +939,7 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
         return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
     }
 
         return selectedCsii.toString(selectedCdmInstance.getName(), getPort()) + PASSWORD_SUFFIX;
     }
 
-    private boolean validateLogin() {
+    private boolean validateLogin(ICdmRemoteSource remoteSource) {
         if(getUsername() == null || getUsername().isEmpty()) {
             setMessage("User login cannot be empty");
             return false;
         if(getUsername() == null || getUsername().isEmpty()) {
             setMessage("User login cannot be empty");
             return false;
@@ -943,8 +948,25 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
             setMessage("Password cannot be empty");
             return false;
         }
             setMessage("Password cannot be empty");
             return false;
         }
+
+
+        try {
+            IUserService userService = CdmApplicationRemoteConfiguration.getUserService(remoteSource);
+            UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(getUsername(), getPassword());
+            CdmApplicationRemoteConfiguration.getAuthenticationManager(userService).authenticate(token);
+        } catch(BadCredentialsException e){
+            setMessage(LoginManager.INCORRECT_CREDENTIALS_MESSAGE);
+            return false;
+        } catch(LockedException e){
+            setMessage(LoginManager.ACCOUNT_LOCKED_MESSAGE);
+            return false;
+        } catch(IllegalArgumentException e){
+            setMessage(LoginManager.INCORRECT_CREDENTIALS_MESSAGE);
+            return false;
+        }
         return true;
     }
         return true;
     }
+
     public String getUsername() {
         return txtLogin.getText();
     }
     public String getUsername() {
         return txtLogin.getText();
     }
@@ -1035,6 +1057,8 @@ public class RemotingLoginDialog extends Dialog implements ICDMServerError {
         txtServerVersion.setText("");
         txtServerVersion.setToolTipText("");
         txtServerCDMVersion.setText("");
         txtServerVersion.setText("");
         txtServerVersion.setToolTipText("");
         txtServerCDMVersion.setText("");
+        comboCdmInstance.removeAll();
+        txtCdmInstanceStatus.setText("");
         txtPort.setEditable(false);
         txtPort.setEnabled(false);
     }
         txtPort.setEditable(false);
         txtPort.setEnabled(false);
     }