diff --git a/maxkey-common/src/main/java/org/maxkey/util/IdGenerator.java b/maxkey-common/src/main/java/org/maxkey/util/IdGenerator.java new file mode 100644 index 000000000..1c04f7cbb --- /dev/null +++ b/maxkey-common/src/main/java/org/maxkey/util/IdGenerator.java @@ -0,0 +1,69 @@ +package org.maxkey.util; + +public class IdGenerator { + + String strategy = "uuid"; + + int datacenterId; + + int machineId; + + SnowFlakeId snowFlakeId = new SnowFlakeId(0,0); + + StringGenerator stringGenerator = new StringGenerator(); + + + public String generate(){ + if(strategy.equalsIgnoreCase("uuid")) { + return stringGenerator.uuidGenerate(); + }else if(strategy.equalsIgnoreCase("SnowFlake")) { + return snowFlakeId.nextId()+""; + }else { + return stringGenerator.randomGenerate(); + } + } + + + public IdGenerator() { + super(); + } + + public IdGenerator(String strategy) { + super(); + this.strategy = strategy; + } + + + public int getDatacenterId() { + return datacenterId; + } + + public void setDatacenterId(int datacenterId) { + this.datacenterId = datacenterId; + } + + public int getMachineId() { + return machineId; + } + + public void setMachineId(int machineId) { + this.machineId = machineId; + } + + public SnowFlakeId getSnowFlakeId() { + return snowFlakeId; + } + + public void setSnowFlakeId(SnowFlakeId snowFlakeId) { + this.snowFlakeId = snowFlakeId; + } + + public StringGenerator getStringGenerator() { + return stringGenerator; + } + + public void setStringGenerator(StringGenerator stringGenerator) { + this.stringGenerator = stringGenerator; + } + +} diff --git a/maxkey-common/src/main/java/org/maxkey/util/SnowFlakeId.java b/maxkey-common/src/main/java/org/maxkey/util/SnowFlakeId.java new file mode 100644 index 000000000..16aa7d643 --- /dev/null +++ b/maxkey-common/src/main/java/org/maxkey/util/SnowFlakeId.java @@ -0,0 +1,191 @@ +package org.maxkey.util; + +import java.util.Calendar; +import java.util.Date; + +/** + * 描述: Twitter的分布式自增ID雪花算法snowflake (Java版) + * + * @author Crystal.Sea + * @create 2021-04-17 + **/ +public class SnowFlakeId { + + /** + * 起始的时间戳 + */ + private final static long START_STMP = 1480166465631L; + + /** + * 每一部分占用的位数 + */ + private final static long SEQUENCE_BIT = 12; //序列号占用的位数 + private final static long MACHINE_BIT = 5; //机器标识占用的位数 + private final static long DATACENTER_BIT = 5;//数据中心占用的位数 + + /** + * 每一部分的最大值 + */ + private final static long MAX_DATACENTER_NUM = -1L ^ (-1L << DATACENTER_BIT); + private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT); + private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT); + + /** + * 每一部分向左的位移 + */ + private final static long MACHINE_LEFT = SEQUENCE_BIT; + private final static long DATACENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT; + private final static long TIMESTMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT; + + private long datacenterId; //数据中心 + private long machineId; //机器标识 + private long sequence = 0L; //序列号 + private long lastStmp = -1L;//上一次时间戳 + private String dateTime; + + public SnowFlakeId(long datacenterId, long machineId) { + if (datacenterId > MAX_DATACENTER_NUM || datacenterId < 0) { + throw new IllegalArgumentException("datacenterId can't be greater than MAX_DATACENTER_NUM or less than 0"); + } + if (machineId > MAX_MACHINE_NUM || machineId < 0) { + throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or less than 0"); + } + this.datacenterId = datacenterId; + this.machineId = machineId; + } + + public SnowFlakeId(long datacenterId, long machineId, long sequence, long lastStmp) { + super(); + this.datacenterId = datacenterId; + this.machineId = machineId; + this.sequence = sequence; + this.lastStmp = lastStmp; + dateTime =DateUtils.toUtc( fromatTime(lastStmp)); + } + + /** + * 产生下一个ID + * + * @return + */ + public synchronized long nextId() { + long currStmp = getNewstmp(); + if (currStmp < lastStmp) { + throw new RuntimeException("Clock moved backwards. Refusing to generate id"); + } + + if (currStmp == lastStmp) { + //相同毫秒内,序列号自增 + sequence = (sequence + 1) & MAX_SEQUENCE; + //同一毫秒的序列数已经达到最大 + if (sequence == 0L) { + currStmp = getNextMill(); + } + } else { + //不同毫秒内,序列号置为0 + sequence = 0L; + } + + lastStmp = currStmp; + + return (currStmp - START_STMP) << TIMESTMP_LEFT //时间戳部分 + | datacenterId << DATACENTER_LEFT //数据中心部分 + | machineId << MACHINE_LEFT //机器标识部分 + | sequence; //序列号部分 + } + + private long getNextMill() { + long mill = getNewstmp(); + while (mill <= lastStmp) { + mill = getNewstmp(); + } + return mill; + } + + private long getNewstmp() { + return System.currentTimeMillis(); + } + + public SnowFlakeId parse(long id) { + String sonwFlakeId = Long.toBinaryString(id); + System.out.println(sonwFlakeId); + int len = sonwFlakeId.length(); + int sequenceStart = (int) (len < MACHINE_LEFT ? 0 : len - MACHINE_LEFT); + int workerStart = (int) (len < DATACENTER_LEFT ? 0 : len - DATACENTER_LEFT); + int timeStart = (int) (len < TIMESTMP_LEFT ? 0 : len - TIMESTMP_LEFT); + String sequence = sonwFlakeId.substring(sequenceStart, len); + String workerId = sequenceStart == 0 ? "0" : sonwFlakeId.substring(workerStart, sequenceStart); + String dataCenterId = workerStart == 0 ? "0" : sonwFlakeId.substring(timeStart, workerStart); + String time = timeStart == 0 ? "0" : sonwFlakeId.substring(0, timeStart); + int sequenceInt = Integer.valueOf(sequence, 2); + int workerIdInt = Integer.valueOf(workerId, 2); + int dataCenterIdInt = Integer.valueOf(dataCenterId, 2); + long diffTime = Long.parseLong(time, 2); + long timeLong = diffTime + START_STMP; + + SnowFlakeId snowFlakeIdParse =new SnowFlakeId(dataCenterIdInt,workerIdInt,sequenceInt,timeLong); + + return snowFlakeIdParse; + } + + private static Date fromatTime(long date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(date); + return calendar.getTime(); + } + + + + public long getDatacenterId() { + return datacenterId; + } + + public void setDatacenterId(long datacenterId) { + this.datacenterId = datacenterId; + } + + public long getMachineId() { + return machineId; + } + + public void setMachineId(long machineId) { + this.machineId = machineId; + } + + public long getSequence() { + return sequence; + } + + public void setSequence(long sequence) { + this.sequence = sequence; + } + + public long getLastStmp() { + return lastStmp; + } + + public void setLastStmp(long lastStmp) { + this.lastStmp = lastStmp; + } + + public String getDateTime() { + return dateTime; + } + + public void setDateTime(String dateTime) { + this.dateTime = dateTime; + } + + public static void main(String[] args) { + SnowFlakeId snowFlake = new SnowFlakeId(20, 30); + + long start = System.currentTimeMillis(); + for (int i = 0; i < 1000000; i++) { + System.out.println(snowFlake.nextId()); + } + + System.out.println(System.currentTimeMillis() - start); + + + } +} \ No newline at end of file diff --git a/maxkey-common/src/test/java/org/maxkey/util/SonwFlakeIdTest.java b/maxkey-common/src/test/java/org/maxkey/util/SonwFlakeIdTest.java new file mode 100644 index 000000000..26a445408 --- /dev/null +++ b/maxkey-common/src/test/java/org/maxkey/util/SonwFlakeIdTest.java @@ -0,0 +1,32 @@ +/* + * Copyright [2020] [MaxKey of copyright http://www.maxkey.top] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.maxkey.util; + +import org.junit.Test; + +public class SonwFlakeIdTest { + + + @Test + public void UidGenerator() { + SnowFlakeId snowFlake = new SnowFlakeId(2, 3); + long seq = snowFlake.nextId(); + System.out.println(seq); + System.out.println(snowFlake.parse(seq)); + } +} diff --git a/maxkey-common/src/test/java/org/maxkey/util/UUIDGeneratorTest.java b/maxkey-common/src/test/java/org/maxkey/util/UUIDGeneratorTest.java index 366fa4aea..cf01ee6b9 100644 --- a/maxkey-common/src/test/java/org/maxkey/util/UUIDGeneratorTest.java +++ b/maxkey-common/src/test/java/org/maxkey/util/UUIDGeneratorTest.java @@ -56,4 +56,5 @@ public class UUIDGeneratorTest { } + } diff --git a/maxkey-core/src/main/java/org/maxkey/autoconfigure/ApplicationAutoConfiguration.java b/maxkey-core/src/main/java/org/maxkey/autoconfigure/ApplicationAutoConfiguration.java index 712b3e760..13666f4e3 100644 --- a/maxkey-core/src/main/java/org/maxkey/autoconfigure/ApplicationAutoConfiguration.java +++ b/maxkey-core/src/main/java/org/maxkey/autoconfigure/ApplicationAutoConfiguration.java @@ -32,6 +32,9 @@ import org.maxkey.crypto.password.MessageDigestPasswordEncoder; import org.maxkey.crypto.password.PasswordReciprocal; import org.maxkey.crypto.password.SM3PasswordEncoder; import org.maxkey.crypto.password.StandardPasswordEncoder; +import org.maxkey.util.IdGenerator; +import org.maxkey.util.SnowFlakeId; +import org.maxkey.web.WebContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; @@ -183,6 +186,23 @@ public class ApplicationAutoConfiguration implements InitializingBean { @Value("${maxkey.saml.v20.sp.issuing.entity.id}") String spIssuingEntityName) { return spIssuingEntityName; } + + + /** + * spKeyStoreLoader . + * @return + */ + @Bean(name = "idGenerator") + public IdGenerator idGenerator( + @Value("${maxkey.id.strategy:SnowFlake}") String strategy, + @Value("${maxkey.id.datacenterId:0}") int datacenterId, + @Value("${maxkey.id.machineId:0}") int machineId) { + IdGenerator idGenerator = new IdGenerator(strategy); + SnowFlakeId SnowFlakeId = new SnowFlakeId(datacenterId,machineId); + idGenerator.setSnowFlakeId(SnowFlakeId); + WebContext.idGenerator = idGenerator; + return idGenerator; + } @Override diff --git a/maxkey-core/src/main/java/org/maxkey/domain/Accounts.java b/maxkey-core/src/main/java/org/maxkey/domain/Accounts.java index d11cea1ff..12441940d 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/Accounts.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/Accounts.java @@ -42,7 +42,7 @@ public class Accounts extends JpaBaseDomain implements Serializable { private static final long serialVersionUID = 6829592256223630307L; @Id @Column - @GeneratedValue(strategy = GenerationType.AUTO, generator = "uuid") + @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeid") private String id; @Column private String uid; diff --git a/maxkey-core/src/main/java/org/maxkey/domain/GroupMember.java b/maxkey-core/src/main/java/org/maxkey/domain/GroupMember.java index 123388df6..096d1d297 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/GroupMember.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/GroupMember.java @@ -43,7 +43,7 @@ public class GroupMember extends UserInfo implements Serializable{ private static final long serialVersionUID = -8059639972590554760L; @Id @Column - @GeneratedValue(strategy=GenerationType.AUTO,generator="uuid") + @GeneratedValue(strategy=GenerationType.AUTO,generator="snowflakeid") String id; @Column private String groupId; diff --git a/maxkey-core/src/main/java/org/maxkey/domain/GroupPrivileges.java b/maxkey-core/src/main/java/org/maxkey/domain/GroupPrivileges.java index 159594910..4d52b667f 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/GroupPrivileges.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/GroupPrivileges.java @@ -45,7 +45,7 @@ public class GroupPrivileges extends Apps implements Serializable{ private static final long serialVersionUID = 8634166407201007340L; @Id @Column - @GeneratedValue(strategy=GenerationType.AUTO,generator="uuid") + @GeneratedValue(strategy=GenerationType.AUTO,generator="snowflakeid") String id; @Column private String groupId; diff --git a/maxkey-core/src/main/java/org/maxkey/domain/Groups.java b/maxkey-core/src/main/java/org/maxkey/domain/Groups.java index 772059fb9..a69789d72 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/Groups.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/Groups.java @@ -33,7 +33,7 @@ public class Groups extends JpaBaseDomain implements Serializable { private static final long serialVersionUID = 4660258495864814777L; @Id @Column - @GeneratedValue(strategy = GenerationType.AUTO, generator = "uuid") + @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeid") String id; @Length(max = 60) diff --git a/maxkey-core/src/main/java/org/maxkey/domain/HistoryLogin.java b/maxkey-core/src/main/java/org/maxkey/domain/HistoryLogin.java index aa497bdd3..6779749f9 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/HistoryLogin.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/HistoryLogin.java @@ -43,7 +43,7 @@ public class HistoryLogin extends JpaBaseDomain implements Serializable{ private static final long serialVersionUID = -1321470643357719383L; @Id @Column - @GeneratedValue(strategy=GenerationType.AUTO,generator="uuid") + @GeneratedValue(strategy=GenerationType.AUTO,generator="snowflakeid") String id; @Column String sessionId; diff --git a/maxkey-core/src/main/java/org/maxkey/domain/HistoryLoginApps.java b/maxkey-core/src/main/java/org/maxkey/domain/HistoryLoginApps.java index d089c468e..fc35e6df8 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/HistoryLoginApps.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/HistoryLoginApps.java @@ -37,7 +37,7 @@ public class HistoryLoginApps extends JpaBaseDomain { private static final long serialVersionUID = 5085201575292304749L; @Id @Column - @GeneratedValue(strategy=GenerationType.AUTO,generator="uuid") + @GeneratedValue(strategy=GenerationType.AUTO,generator="snowflakeid") String id; @Column private String sessionId; diff --git a/maxkey-core/src/main/java/org/maxkey/domain/HistoryLogs.java b/maxkey-core/src/main/java/org/maxkey/domain/HistoryLogs.java index 946b1b434..3c9feb7c9 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/HistoryLogs.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/HistoryLogs.java @@ -37,7 +37,7 @@ public class HistoryLogs extends JpaBaseDomain implements Serializable { private static final long serialVersionUID = 6560201093784960493L; @Id @Column - @GeneratedValue(strategy = GenerationType.AUTO, generator = "uuid") + @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeid") String id; @Column String serviceName; diff --git a/maxkey-core/src/main/java/org/maxkey/domain/Notices.java b/maxkey-core/src/main/java/org/maxkey/domain/Notices.java index 42f1a8beb..637d6dc7d 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/Notices.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/Notices.java @@ -22,7 +22,7 @@ public class Notices extends JpaBaseDomain implements Serializable { @Id @Column - @GeneratedValue(strategy = GenerationType.AUTO, generator = "uuid") + @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeid") protected String id; /** * diff --git a/maxkey-core/src/main/java/org/maxkey/domain/Organizations.java b/maxkey-core/src/main/java/org/maxkey/domain/Organizations.java index 2354023a7..007e0d2c9 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/Organizations.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/Organizations.java @@ -33,7 +33,7 @@ public class Organizations extends JpaBaseDomain implements Serializable { @Id @Column - @GeneratedValue(strategy = GenerationType.AUTO, generator = "uuid") + @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeid") private String id; @Column private String code; diff --git a/maxkey-core/src/main/java/org/maxkey/domain/Resources.java b/maxkey-core/src/main/java/org/maxkey/domain/Resources.java index 5d62eefe0..137eb6487 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/Resources.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/Resources.java @@ -32,7 +32,7 @@ public class Resources extends JpaBaseDomain implements Serializable { private static final long serialVersionUID = 2567171742999638608L; @Id @Column - @GeneratedValue(strategy = GenerationType.AUTO,generator = "uuid") + @GeneratedValue(strategy = GenerationType.AUTO,generator = "snowflakeid") String id; @Column String name; diff --git a/maxkey-core/src/main/java/org/maxkey/domain/RoleMember.java b/maxkey-core/src/main/java/org/maxkey/domain/RoleMember.java index 00f2de08b..6bf236075 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/RoleMember.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/RoleMember.java @@ -37,7 +37,7 @@ public class RoleMember extends UserInfo implements Serializable { private static final long serialVersionUID = -8059639972590554760L; @Id @Column - @GeneratedValue(strategy = GenerationType.AUTO, generator = "uuid") + @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeid") String id; @Column private String roleId; diff --git a/maxkey-core/src/main/java/org/maxkey/domain/RolePermissions.java b/maxkey-core/src/main/java/org/maxkey/domain/RolePermissions.java index 0068550a8..095b5ce87 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/RolePermissions.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/RolePermissions.java @@ -26,6 +26,7 @@ import javax.persistence.Id; import javax.persistence.Table; import org.apache.mybatis.jpa.persistence.JpaBaseDomain; import org.maxkey.constants.ConstantsStatus; +import org.maxkey.web.WebContext; @Entity @Table(name = "MXK_ROLE_PERMISSIONS") @@ -34,7 +35,7 @@ public class RolePermissions extends JpaBaseDomain implements Serializable { @Id @Column - @GeneratedValue(strategy = GenerationType.AUTO, generator = "uuid") + @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeid") String id; @Column String appId; @@ -60,7 +61,7 @@ public class RolePermissions extends JpaBaseDomain implements Serializable { * @param resourceId String */ public RolePermissions(String appId, String roleId, String resourceId) { - this.id = this.generateId(); + this.id = WebContext.genId(); this.appId = appId; this.roleId = roleId; this.resourceId = resourceId; diff --git a/maxkey-core/src/main/java/org/maxkey/domain/Roles.java b/maxkey-core/src/main/java/org/maxkey/domain/Roles.java index d21a438af..4bbaa0895 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/Roles.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/Roles.java @@ -33,7 +33,7 @@ public class Roles extends JpaBaseDomain implements Serializable { @Id @Column - @GeneratedValue(strategy = GenerationType.AUTO,generator = "uuid") + @GeneratedValue(strategy = GenerationType.AUTO,generator = "snowflakeid") private String id; @Column private String name; diff --git a/maxkey-core/src/main/java/org/maxkey/domain/UserInfo.java b/maxkey-core/src/main/java/org/maxkey/domain/UserInfo.java index 1f392fe44..d28dde144 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/UserInfo.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/UserInfo.java @@ -43,7 +43,7 @@ public class UserInfo extends JpaBaseDomain { // @Id @Column - @GeneratedValue(strategy = GenerationType.AUTO, generator = "uuid") + @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeid") String id; @Column protected String username; diff --git a/maxkey-core/src/main/java/org/maxkey/domain/UserInfoAdjoint.java b/maxkey-core/src/main/java/org/maxkey/domain/UserInfoAdjoint.java index e926e6f6b..6dadd607b 100644 --- a/maxkey-core/src/main/java/org/maxkey/domain/UserInfoAdjoint.java +++ b/maxkey-core/src/main/java/org/maxkey/domain/UserInfoAdjoint.java @@ -37,7 +37,7 @@ public class UserInfoAdjoint extends JpaBaseDomain { @Id @Column - @GeneratedValue(strategy = GenerationType.AUTO, generator = "uuid") + @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeid") String id; protected String displayName; diff --git a/maxkey-core/src/main/java/org/maxkey/persistence/db/LoginHistoryService.java b/maxkey-core/src/main/java/org/maxkey/persistence/db/LoginHistoryService.java index caf31d2b7..f1c3a8e81 100644 --- a/maxkey-core/src/main/java/org/maxkey/persistence/db/LoginHistoryService.java +++ b/maxkey-core/src/main/java/org/maxkey/persistence/db/LoginHistoryService.java @@ -28,9 +28,9 @@ import org.springframework.jdbc.core.JdbcTemplate; public class LoginHistoryService { private static Logger _logger = LoggerFactory.getLogger(LoginHistoryService.class); - private static final String HISTORY_LOGIN_INSERT_STATEMENT = "INSERT INTO MXK_HISTORY_LOGIN (ID , SESSIONID , UID , USERNAME , DISPLAYNAME , LOGINTYPE , MESSAGE , CODE , PROVIDER , SOURCEIP , BROWSER , PLATFORM , APPLICATION , LOGINURL )VALUES( ? , ? , ? , ? , ?, ? , ? , ?, ? , ? , ?, ? , ? , ?)"; + private static final String HISTORY_LOGIN_INSERT_STATEMENT = "insert into mxk_history_login (id , sessionid , uid , username , displayname , logintype , message , code , provider , sourceip , browser , platform , application , loginurl )values( ? , ? , ? , ? , ?, ? , ? , ?, ? , ? , ?, ? , ? , ?)"; - private static final String HISTORY_LOGOUT_UPDATE_STATEMENT = "UPDATE MXK_HISTORY_LOGIN SET LOGOUTTIME = ? WHERE SESSIONID = ?"; + private static final String HISTORY_LOGOUT_UPDATE_STATEMENT = "update mxk_history_login set logouttime = ? where sessionid = ?"; protected JdbcTemplate jdbcTemplate; diff --git a/maxkey-core/src/main/java/org/maxkey/persistence/db/LoginService.java b/maxkey-core/src/main/java/org/maxkey/persistence/db/LoginService.java index 40c329d87..19f3a0bb4 100644 --- a/maxkey-core/src/main/java/org/maxkey/persistence/db/LoginService.java +++ b/maxkey-core/src/main/java/org/maxkey/persistence/db/LoginService.java @@ -38,29 +38,29 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority; public class LoginService { private static Logger _logger = LoggerFactory.getLogger(LoginService.class); - private static final String LOCK_USER_UPDATE_STATEMENT = "UPDATE MXK_USERINFO SET ISLOCKED = ? , UNLOCKTIME = ? WHERE ID = ?"; + private static final String LOCK_USER_UPDATE_STATEMENT = "update mxk_userinfo set islocked = ? , unlocktime = ? where id = ?"; - private static final String UNLOCK_USER_UPDATE_STATEMENT = "UPDATE MXK_USERINFO SET ISLOCKED = ? , UNLOCKTIME = ? WHERE ID = ?"; + private static final String UNLOCK_USER_UPDATE_STATEMENT = "update mxk_userinfo set islocked = ? , unlocktime = ? where id = ?"; - private static final String BADPASSWORDCOUNT_UPDATE_STATEMENT = "UPDATE MXK_USERINFO SET BADPASSWORDCOUNT = ? , BADPASSWORDTIME = ? WHERE ID = ?"; + private static final String BADPASSWORDCOUNT_UPDATE_STATEMENT = "update mxk_userinfo set badpasswordcount = ? , badpasswordtime = ? where id = ?"; - private static final String BADPASSWORDCOUNT_RESET_UPDATE_STATEMENT = "UPDATE MXK_USERINFO SET BADPASSWORDCOUNT = ? , ISLOCKED = ? ,UNLOCKTIME = ? WHERE ID = ?"; + private static final String BADPASSWORDCOUNT_RESET_UPDATE_STATEMENT = "update mxk_userinfo set badpasswordcount = ? , islocked = ? ,unlocktime = ? where id = ?"; - private static final String LOGIN_USERINFO_UPDATE_STATEMENT = "UPDATE MXK_USERINFO SET LASTLOGINTIME = ? , LASTLOGINIP = ? , LOGINCOUNT = ?, ONLINE = " - + UserInfo.ONLINE.ONLINE + " WHERE ID = ?"; + private static final String LOGIN_USERINFO_UPDATE_STATEMENT = "update mxk_userinfo set lastlogintime = ? , lastloginip = ? , logincount = ?, online = " + + UserInfo.ONLINE.ONLINE + " where id = ?"; - private static final String LOGOUT_USERINFO_UPDATE_STATEMENT = "UPDATE MXK_USERINFO SET LASTLOGOFFTIME = ? , ONLINE = " - + UserInfo.ONLINE.OFFLINE + " WHERE ID = ?"; + private static final String LOGOUT_USERINFO_UPDATE_STATEMENT = "update mxk_userinfo set lastlogofftime = ? , online = " + + UserInfo.ONLINE.OFFLINE + " where id = ?"; - private static final String GROUPS_SELECT_STATEMENT = "SELECT DISTINCT G.ID,G.NAME FROM MXK_USERINFO U,`MXK_GROUPS` G,MXK_GROUP_MEMBER GM WHERE U.ID = ? AND U.ID=GM.MEMBERID AND GM.GROUPID=G.ID "; + private static final String GROUPS_SELECT_STATEMENT = "select distinct g.id,g.name from mxk_userinfo u,`mxk_groups` g,mxk_group_member gm where u.id = ? and u.id=gm.memberid and gm.groupid=g.id "; - private static final String DEFAULT_USERINFO_SELECT_STATEMENT = "SELECT * FROM MXK_USERINFO WHERE USERNAME = ?"; + private static final String DEFAULT_USERINFO_SELECT_STATEMENT = "select * from mxk_userinfo where username = ?"; - private static final String DEFAULT_USERINFO_SELECT_STATEMENT_USERNAME_MOBILE = "SELECT * FROM MXK_USERINFO WHERE USERNAME = ? OR MOBILE = ? "; + private static final String DEFAULT_USERINFO_SELECT_STATEMENT_USERNAME_MOBILE = "select * from mxk_userinfo where username = ? or mobile = ? "; - private static final String DEFAULT_USERINFO_SELECT_STATEMENT_USERNAME_MOBILE_EMAIL = "SELECT * FROM MXK_USERINFO WHERE USERNAME = ? OR MOBILE = ? OR EMAIL = ? "; + private static final String DEFAULT_USERINFO_SELECT_STATEMENT_USERNAME_MOBILE_EMAIL = "select * from mxk_userinfo where username = ? or mobile = ? or email = ? "; - private static final String DEFAULT_MYAPPS_SELECT_STATEMENT = "SELECT DISTINCT APP.ID,APP.NAME FROM MXK_APPS APP,MXK_GROUP_PRIVILEGES GP,MXK_GROUPS G WHERE APP.ID=GP.APPID AND GP.GROUPID=G.ID AND G.ID IN(%s)"; + private static final String DEFAULT_MYAPPS_SELECT_STATEMENT = "select distinct app.id,app.name from mxk_apps app,mxk_group_privileges gp,mxk_groups g where app.id=gp.appid and gp.groupid=g.id and g.id in(%s)"; protected JdbcTemplate jdbcTemplate; diff --git a/maxkey-core/src/main/java/org/maxkey/persistence/db/PasswordPolicyValidator.java b/maxkey-core/src/main/java/org/maxkey/persistence/db/PasswordPolicyValidator.java index 17eaae882..af9b711d2 100644 --- a/maxkey-core/src/main/java/org/maxkey/persistence/db/PasswordPolicyValidator.java +++ b/maxkey-core/src/main/java/org/maxkey/persistence/db/PasswordPolicyValidator.java @@ -92,15 +92,15 @@ public class PasswordPolicyValidator { private static final String PASSWORD_POLICY_KEY = "PASSWORD_POLICY_KEY"; - private static final String LOCK_USER_UPDATE_STATEMENT = "UPDATE MXK_USERINFO SET ISLOCKED = ? , UNLOCKTIME = ? WHERE ID = ?"; + private static final String LOCK_USER_UPDATE_STATEMENT = "update mxk_userinfo set islocked = ? , unlocktime = ? where id = ?"; - private static final String PASSWORD_POLICY_SELECT_STATEMENT = "SELECT * FROM MXK_PASSWORD_POLICY "; + private static final String PASSWORD_POLICY_SELECT_STATEMENT = "select * from mxk_password_policy "; - private static final String UNLOCK_USER_UPDATE_STATEMENT = "UPDATE MXK_USERINFO SET ISLOCKED = ? , UNLOCKTIME = ? WHERE ID = ?"; + private static final String UNLOCK_USER_UPDATE_STATEMENT = "update mxk_userinfo set islocked = ? , unlocktime = ? where id = ?"; - private static final String BADPASSWORDCOUNT_UPDATE_STATEMENT = "UPDATE MXK_USERINFO SET BADPASSWORDCOUNT = ? , BADPASSWORDTIME = ? WHERE ID = ?"; + private static final String BADPASSWORDCOUNT_UPDATE_STATEMENT = "update mxk_userinfo set badpasswordcount = ? , badpasswordtime = ? where id = ?"; - private static final String BADPASSWORDCOUNT_RESET_UPDATE_STATEMENT = "UPDATE MXK_USERINFO SET BADPASSWORDCOUNT = ? , ISLOCKED = ? ,UNLOCKTIME = ? WHERE ID = ?"; + private static final String BADPASSWORDCOUNT_RESET_UPDATE_STATEMENT = "update mxk_userinfo set badpasswordcount = ? , islocked = ? ,unlocktime = ? where id = ?"; public PasswordPolicyValidator() { } diff --git a/maxkey-core/src/main/java/org/maxkey/web/WebContext.java b/maxkey-core/src/main/java/org/maxkey/web/WebContext.java index 30865c64e..df717b535 100644 --- a/maxkey-core/src/main/java/org/maxkey/web/WebContext.java +++ b/maxkey-core/src/main/java/org/maxkey/web/WebContext.java @@ -32,7 +32,7 @@ import org.apache.commons.logging.LogFactory; import org.maxkey.configuration.ApplicationConfig; import org.maxkey.domain.UserInfo; import org.maxkey.util.DateUtils; -import org.maxkey.util.StringGenerator; +import org.maxkey.util.IdGenerator; import org.maxkey.web.message.Message; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,6 +63,8 @@ public final class WebContext { public static ArrayList sessionAttributeNameList = new ArrayList(); + public static IdGenerator idGenerator; + static { sessionAttributeNameList.add(WebConstants.CURRENT_LOGIN_USER_PASSWORD_SET_TYPE); sessionAttributeNameList.add(WebConstants.FIRST_SAVED_REQUEST_PARAMETER); @@ -518,7 +520,10 @@ public final class WebContext { * @return String */ public static String genId() { - return (new StringGenerator()).uuidGenerate(); + if(idGenerator == null) { + idGenerator = new IdGenerator(); + } + return idGenerator.generate(); } public static ModelAndView redirect(String redirectUrl) { diff --git a/maxkey-lib/mybatis-jpa-extra-2.2.jar b/maxkey-lib/mybatis-jpa-extra-2.2.jar deleted file mode 100644 index f6d3cb220..000000000 Binary files a/maxkey-lib/mybatis-jpa-extra-2.2.jar and /dev/null differ diff --git a/maxkey-lib/mybatis-jpa-extra-2.3.jar b/maxkey-lib/mybatis-jpa-extra-2.3.jar new file mode 100644 index 000000000..3601745e3 Binary files /dev/null and b/maxkey-lib/mybatis-jpa-extra-2.3.jar differ diff --git a/maxkey-lib/mybatis-jpa-extra-spring-boot-starter-2.2.jar b/maxkey-lib/mybatis-jpa-extra-spring-boot-starter-2.2.jar deleted file mode 100644 index aa39b1ced..000000000 Binary files a/maxkey-lib/mybatis-jpa-extra-spring-boot-starter-2.2.jar and /dev/null differ diff --git a/maxkey-lib/mybatis-jpa-extra-spring-boot-starter-2.3.jar b/maxkey-lib/mybatis-jpa-extra-spring-boot-starter-2.3.jar new file mode 100644 index 000000000..75f3ae883 Binary files /dev/null and b/maxkey-lib/mybatis-jpa-extra-spring-boot-starter-2.3.jar differ diff --git a/maxkey-persistence/src/main/java/org/maxkey/persistence/mapper/AppsMapper.java b/maxkey-persistence/src/main/java/org/maxkey/persistence/mapper/AppsMapper.java index f88ba732b..3475f1f25 100644 --- a/maxkey-persistence/src/main/java/org/maxkey/persistence/mapper/AppsMapper.java +++ b/maxkey-persistence/src/main/java/org/maxkey/persistence/mapper/AppsMapper.java @@ -37,7 +37,7 @@ public interface AppsMapper extends IJpaBaseMapper { public int updateApp(Apps app); - @Update("UPDATE MXK_APPS SET ISEXTENDATTR=#{isExtendAttr}, EXTENDATTR=#{extendAttr} WHERE id = #{id}") + @Update("update mxk_apps set isextendattr=#{isExtendAttr}, extendattr=#{extendAttr} where id = #{id}") public int updateExtendAttr(Apps app); diff --git a/maxkey-persistence/src/main/java/org/maxkey/persistence/mapper/UserInfoMapper.java b/maxkey-persistence/src/main/java/org/maxkey/persistence/mapper/UserInfoMapper.java index 93f360f61..f04effb26 100644 --- a/maxkey-persistence/src/main/java/org/maxkey/persistence/mapper/UserInfoMapper.java +++ b/maxkey-persistence/src/main/java/org/maxkey/persistence/mapper/UserInfoMapper.java @@ -60,7 +60,7 @@ public interface UserInfoMapper extends IJpaBaseMapper{ public int updateProfile(UserInfo userInfo); - @Select("SELECT * FROM MXK_USERINFO WHERE EMAIL = #{value} OR MOBILE= #{value}") + @Select("select * from mxk_userinfo where email = #{value} or mobile= #{value}") public UserInfo queryUserInfoByEmailMobile(String emailMobile); } diff --git a/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/GroupMemberController.java b/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/GroupMemberController.java index fba994a73..60d3891dd 100644 --- a/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/GroupMemberController.java +++ b/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/GroupMemberController.java @@ -128,7 +128,7 @@ public class GroupMemberController { for (int i = 0; i < arrMemberIds.length; i++) { GroupMember newGroupMember = new GroupMember(groupId,groupMember.getGroupName(), arrMemberIds[i], arrMemberNames[i],"USER"); - newGroupMember.setId(newGroupMember.generateId()); + newGroupMember.setId(WebContext.genId()); result = groupMemberService.insert(newGroupMember); } if(!result) { diff --git a/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/GroupPrivilegesController.java b/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/GroupPrivilegesController.java index 16827e318..04315078d 100644 --- a/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/GroupPrivilegesController.java +++ b/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/GroupPrivilegesController.java @@ -110,7 +110,7 @@ public class GroupPrivilegesController { for (int i = 0; i < arrAppIds.length; i++) { GroupPrivileges newGroupApp = new GroupPrivileges(groupId, arrAppIds[i]); - newGroupApp.setId(newGroupApp.generateId()); + newGroupApp.setId(WebContext.genId()); result = groupPrivilegesService.insert(newGroupApp); } if(!result) { diff --git a/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/RoleMemberController.java b/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/RoleMemberController.java index 0333e4778..cc8eea840 100644 --- a/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/RoleMemberController.java +++ b/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/RoleMemberController.java @@ -114,7 +114,7 @@ public class RoleMemberController { for (int i = 0; i < arrMemberIds.length; i++) { RoleMember newRoleMember = new RoleMember(groupId,roleMember.getRoleName(), arrMemberIds[i], arrMemberNames[i],"USER"); - newRoleMember.setId(newRoleMember.generateId()); + newRoleMember.setId(WebContext.genId()); result = roleMemberService.insert(newRoleMember); } if(!result) { diff --git a/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/UserInfoController.java b/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/UserInfoController.java index c56011476..e4e9c8870 100644 --- a/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/UserInfoController.java +++ b/maxkey-web-manage/src/main/java/org/maxkey/web/contorller/UserInfoController.java @@ -116,7 +116,7 @@ public class UserInfoController { // new Message(WebContext.getValidErrorText(),result); } - userInfo.setId(userInfo.generateId()); + userInfo.setId(WebContext.genId()); //userInfo.setNameZHShortSpell(StringUtils.hanYu2Pinyin(userInfo.getDisplayName(), true)); //userInfo.setNameZHSpell(StringUtils.hanYu2Pinyin(userInfo.getDisplayName(), false)); if( userInfoService.insert(userInfo)) { diff --git a/maxkey-web-manage/src/main/resources/application.properties b/maxkey-web-manage/src/main/resources/application.properties index 6765f293d..ecfc1fa2e 100644 --- a/maxkey-web-manage/src/main/resources/application.properties +++ b/maxkey-web-manage/src/main/resources/application.properties @@ -44,6 +44,8 @@ spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #mybatis mybatis.type-aliases-package=org.maxkey.domain,org.maxkey.domain.apps, mybatis.mapper-locations=classpath*:/org/maxkey/persistence/mapper/xml/mysql/*.xml +mybatis.table-column-snowflake-datacenter-id=1 +mybatis.table-column-snowflake-machine-id=1 mybatis.table-column-escape=true ############################################################################ diff --git a/maxkey-web-maxkey/src/main/java/org/maxkey/web/interceptor/HistoryLoginAppAdapter.java b/maxkey-web-maxkey/src/main/java/org/maxkey/web/interceptor/HistoryLoginAppAdapter.java index 0f9578c0a..b96b72fce 100644 --- a/maxkey-web-maxkey/src/main/java/org/maxkey/web/interceptor/HistoryLoginAppAdapter.java +++ b/maxkey-web-maxkey/src/main/java/org/maxkey/web/interceptor/HistoryLoginAppAdapter.java @@ -88,7 +88,6 @@ public class HistoryLoginAppAdapter implements AsyncHandlerInterceptor { final UserInfo userInfo = WebContext.getUserInfo(); _logger.debug("sessionId : " + sessionId + " ,appId : " + app.getId()); HistoryLoginApps historyLoginApps = new HistoryLoginApps(); - historyLoginApps.setId(historyLoginApps.generateId()); historyLoginApps.setAppId(app.getId()); historyLoginApps.setSessionId(sessionId); historyLoginApps.setAppName(app.getName()); diff --git a/maxkey-web-maxkey/src/main/resources/application-http.properties b/maxkey-web-maxkey/src/main/resources/application-http.properties index 1e47d1f23..fcdbbe030 100644 --- a/maxkey-web-maxkey/src/main/resources/application-http.properties +++ b/maxkey-web-maxkey/src/main/resources/application-http.properties @@ -49,6 +49,8 @@ spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #mybatis mybatis.type-aliases-package=org.maxkey.domain,org.maxkey.domain.apps, mybatis.mapper-locations=classpath*:/org/maxkey/persistence/mapper/xml/mysql/*.xml +mybatis.table-column-snowflake-datacenter-id=1 +mybatis.table-column-snowflake-machine-id=1 mybatis.table-column-escape=true ############################################################################ diff --git a/maxkey-web-maxkey/src/main/resources/application-https.properties b/maxkey-web-maxkey/src/main/resources/application-https.properties index ff39ac419..83b07c71b 100644 --- a/maxkey-web-maxkey/src/main/resources/application-https.properties +++ b/maxkey-web-maxkey/src/main/resources/application-https.properties @@ -49,6 +49,8 @@ spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #mybatis mybatis.type-aliases-package=org.maxkey.domain,org.maxkey.domain.apps, mybatis.mapper-locations=classpath*:/org/maxkey/persistence/mapper/xml/mysql/*.xml +mybatis.table-column-snowflake-datacenter-id=1 +mybatis.table-column-snowflake-machine-id=1 mybatis.table-column-escape=true ############################################################################