10 changed files with 214 additions and 6 deletions
@ -1,6 +1,16 @@ |
|||||||
package org.leolo.web.dm; |
package org.leolo.web.dm; |
||||||
|
|
||||||
public class Constant { |
public class Constant { |
||||||
|
//Session name
|
||||||
public static final String SESSION_USER_ID = "uid"; |
public static final String SESSION_USER_ID = "uid"; |
||||||
public static final String SESSION_USER_NAME = "uname"; |
public static final String SESSION_USER_NAME = "uname"; |
||||||
|
|
||||||
|
//Basic user information map
|
||||||
|
public static final String BUI_KEY_USER_ID = "uid"; |
||||||
|
public static final String BUI_KEY_USER_NAME = "uname"; |
||||||
|
|
||||||
|
//System parameter map
|
||||||
|
|
||||||
|
//Common value
|
||||||
|
public static final int COM_DEFAULT_USER_ID = 1; |
||||||
} |
} |
||||||
|
|||||||
@ -0,0 +1,80 @@ |
|||||||
|
package org.leolo.web.dm.dao; |
||||||
|
|
||||||
|
import java.sql.Connection; |
||||||
|
import java.sql.PreparedStatement; |
||||||
|
import java.sql.ResultSet; |
||||||
|
import java.sql.SQLException; |
||||||
|
import java.sql.Timestamp; |
||||||
|
import java.text.SimpleDateFormat; |
||||||
|
import java.util.Date; |
||||||
|
import java.util.Hashtable; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.concurrent.ExecutorService; |
||||||
|
import java.util.concurrent.Executors; |
||||||
|
|
||||||
|
import org.leolo.web.dm.Constant; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
public class APIKeyDao extends BaseDao { |
||||||
|
|
||||||
|
private static Logger log = LoggerFactory.getLogger(APIKeyDao.class); |
||||||
|
private static ExecutorService dbPool = Executors.newFixedThreadPool(1); |
||||||
|
|
||||||
|
public String getUsernameByApiKey(String key) { |
||||||
|
try( |
||||||
|
Connection conn = getConnection(); |
||||||
|
PreparedStatement pstmt = conn.prepareStatement("SELECT user_name FROM api_key a JOIN user u ON a.user=u.user_id WHERE api_key = ?") |
||||||
|
){ |
||||||
|
pstmt.setString(1, key); |
||||||
|
try(ResultSet rs = pstmt.executeQuery()){ |
||||||
|
if(rs.next()) { |
||||||
|
return rs.getString(1); |
||||||
|
} |
||||||
|
} |
||||||
|
}catch(SQLException e) { |
||||||
|
log.error(e.getMessage(), e); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
public Map<String, Object> getBasicUserInfomationBuApiKey(String key){ |
||||||
|
try( |
||||||
|
Connection conn = getConnection(); |
||||||
|
PreparedStatement pstmt = conn.prepareStatement("SELECT user_id, user_name FROM api_key a JOIN user u ON a.user=u.user_id WHERE api_key = ?") |
||||||
|
){ |
||||||
|
pstmt.setString(1, key); |
||||||
|
try(ResultSet rs = pstmt.executeQuery()){ |
||||||
|
if(rs.next()) { |
||||||
|
Map<String, Object> map = new Hashtable<>(); |
||||||
|
map.put(Constant.BUI_KEY_USER_ID, rs.getInt(1)); |
||||||
|
map.put(Constant.BUI_KEY_USER_NAME, rs.getString(2)); |
||||||
|
return map; |
||||||
|
} |
||||||
|
} |
||||||
|
}catch(SQLException e) { |
||||||
|
log.error(e.getMessage(), e); |
||||||
|
} |
||||||
|
|
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
public void markKeyUsed(String key) { |
||||||
|
final long LAST_USE_TIME = System.currentTimeMillis(); |
||||||
|
dbPool.execute(new Runnable() { |
||||||
|
@Override |
||||||
|
public void run() { |
||||||
|
try( |
||||||
|
Connection conn = getConnection(); |
||||||
|
PreparedStatement pstmt = conn.prepareStatement("UPDATE api_key SET last_key_use = ? WHERE api_key = ?"); |
||||||
|
){ |
||||||
|
pstmt.setTimestamp(1, new Timestamp(LAST_USE_TIME)); |
||||||
|
pstmt.setString(2, key); |
||||||
|
pstmt.executeUpdate(); |
||||||
|
}catch(SQLException e) { |
||||||
|
log.error(e.getMessage(), e); |
||||||
|
} |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,5 @@ |
|||||||
|
package org.leolo.web.dm.model; |
||||||
|
|
||||||
|
public class User { |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,26 @@ |
|||||||
|
package org.leolo.web.dm.util; |
||||||
|
|
||||||
|
import java.security.SecureRandom; |
||||||
|
import java.util.Random; |
||||||
|
|
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
|
||||||
|
public class APIKeyGenerator { |
||||||
|
|
||||||
|
private static Logger log = LoggerFactory.getLogger(APIKeyGenerator.class); |
||||||
|
private static Random random = new SecureRandom(); |
||||||
|
public static final int DEFAULT_LENGTH = 48; |
||||||
|
public static final char [] AVAILABLE_CHARS = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-_".toCharArray(); |
||||||
|
|
||||||
|
public static String generateApiKey() { |
||||||
|
return generateApiKey(DEFAULT_LENGTH); |
||||||
|
} |
||||||
|
public static String generateApiKey(final int LENGTH) { |
||||||
|
StringBuilder sb = new StringBuilder(); |
||||||
|
for(int i=0;i<LENGTH;i++) { |
||||||
|
sb.append(AVAILABLE_CHARS[random.nextInt(AVAILABLE_CHARS.length)]); |
||||||
|
} |
||||||
|
return sb.toString(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,8 @@ |
|||||||
|
/** |
||||||
|
* |
||||||
|
*/ |
||||||
|
/** |
||||||
|
* @author user |
||||||
|
* |
||||||
|
*/ |
||||||
|
package org.leolo.web.dm.util; |
||||||
@ -0,0 +1,19 @@ |
|||||||
|
package test.org.leolo.web.dm; |
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*; |
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
import org.leolo.web.dm.util.APIKeyGenerator; |
||||||
|
|
||||||
|
class APIGeneratorTester { |
||||||
|
|
||||||
|
@Test |
||||||
|
void test() { |
||||||
|
String key1 = APIKeyGenerator.generateApiKey(); |
||||||
|
String key2 = APIKeyGenerator.generateApiKey(); |
||||||
|
System.out.println("Key 1:"+key1); |
||||||
|
System.out.println("Key 2:"+key2); |
||||||
|
assertNotEquals(key1, key2); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue