1 16 17 package org.apache.axis.security.simple; 18 19 import org.apache.axis.Constants; 20 import org.apache.axis.MessageContext; 21 import org.apache.axis.components.logger.LogFactory; 22 import org.apache.axis.security.AuthenticatedUser; 23 import org.apache.axis.security.SecurityProvider; 24 import org.apache.axis.utils.Messages; 25 import org.apache.commons.logging.Log; 26 27 import java.io.File ; 28 import java.io.FileReader ; 29 import java.io.LineNumberReader ; 30 import java.util.HashMap ; 31 import java.util.StringTokenizer ; 32 33 38 public class SimpleSecurityProvider implements SecurityProvider { 39 protected static Log log = 40 LogFactory.getLog(SimpleSecurityProvider.class.getName()); 41 42 HashMap users = null; 43 HashMap perms = null; 44 45 boolean initialized = false; 46 47 private synchronized void initialize(MessageContext msgContext) 49 { 50 if (initialized) return; 51 52 String configPath = msgContext.getStrProp(Constants.MC_CONFIGPATH); 53 if (configPath == null) { 54 configPath = ""; 55 } else { 56 configPath += File.separator; 57 } 58 File userFile = new File (configPath + "users.lst"); 59 if (userFile.exists()) { 60 users = new HashMap (); 61 62 try { 63 64 FileReader fr = new FileReader ( userFile ); 65 LineNumberReader lnr = new LineNumberReader ( fr ); 66 String line = null ; 67 68 while ( (line = lnr.readLine()) != null ) { 70 StringTokenizer st = new StringTokenizer ( line ); 71 if ( st.hasMoreTokens() ) { 72 String userID = st.nextToken(); 73 String passwd = (st.hasMoreTokens()) ? st.nextToken() : ""; 74 75 if (log.isDebugEnabled()) { 76 log.debug( Messages.getMessage("fromFile00", 77 userID, passwd) ); 78 } 79 80 users.put(userID, passwd); 81 } 82 } 83 84 lnr.close(); 85 86 } catch( Exception e ) { 87 log.error( Messages.getMessage("exception00"), e ); 88 return; 89 } 90 } 91 initialized = true; 92 } 93 94 100 public AuthenticatedUser authenticate(MessageContext msgContext) { 101 102 if (!initialized) { 103 initialize(msgContext); 104 } 105 106 String username = msgContext.getUsername(); 107 String password = msgContext.getPassword(); 108 109 if (users != null) { 110 if (log.isDebugEnabled()) { 111 log.debug( Messages.getMessage("user00", username) ); 112 } 113 114 if ( username == null || 116 username.equals("") || 117 !users.containsKey(username) ) 118 return null; 119 120 String valid = (String ) users.get(username); 121 122 if (log.isDebugEnabled()) { 123 log.debug( Messages.getMessage("password00", password) ); 124 } 125 126 if ( valid.length()>0 && !valid.equals(password) ) 128 return null; 129 130 if (log.isDebugEnabled()) { 131 log.debug( Messages.getMessage("auth00", username) ); 132 } 133 134 return new SimpleAuthenticatedUser(username); 135 } 136 137 return null; 138 } 139 140 145 public boolean userMatches(AuthenticatedUser user, String principal) { 146 if (user == null) return principal == null; 147 return user.getName().compareToIgnoreCase(principal) == 0; 148 } 149 } 150 | Popular Tags |