优化id generator逻辑

This commit is contained in:
zhou-hao
2018-12-01 18:30:56 +08:00
parent caa272cb5f
commit 7fd22b1f1d
2 changed files with 18 additions and 5 deletions

View File

@@ -34,6 +34,16 @@ import java.security.NoSuchAlgorithmException;
public interface IDGenerator<T> {
T generate();
/**
* 空ID生成器
*/
IDGenerator<?> NULL = () -> null;
@SuppressWarnings("unchecked")
static <T> IDGenerator<T> getNullGenerator() {
return (IDGenerator) NULL;
}
/**
* 使用UUID生成id
*/
@@ -60,7 +70,7 @@ public interface IDGenerator<T> {
/**
* 雪花算法
*/
IDGenerator<Long> SNOW_FLAKE = SnowflakeIdGenerator.getInstance()::nextId;
IDGenerator<Long> SNOW_FLAKE = SnowflakeIdGenerator.getInstance()::nextId;
/**
* 雪花算法转String

View File

@@ -16,6 +16,7 @@ import org.hswebframework.web.service.AbstractService;
import org.hswebframework.web.service.DefaultDSLQueryService;
import org.hswebframework.web.service.authorization.events.ClearUserAuthorizationCacheEvent;
import org.hswebframework.web.service.authorization.events.UserModifiedEvent;
import org.hswebframework.web.service.authorization.simple.terms.UserInRoleSqlTerm;
import org.hswebframework.web.validate.ValidationException;
import org.hswebframework.utils.ListUtils;
import org.hswebframework.web.validator.group.CreateGroup;
@@ -76,6 +77,10 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
return entityFactory.newInstance(BindRoleUserEntity.class);
}
protected IDGenerator<String> getIdGenerator() {
return IDGenerator.MD5;
}
@Override
@Transactional(readOnly = true)
public UserEntity selectByUsername(String username) {
@@ -135,7 +140,7 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
//密码强度验证
tryValidateProperty(passwordStrengthValidator, UserEntity.password, userEntity.getPassword());
userEntity.setCreateTime(System.currentTimeMillis());
userEntity.setId(IDGenerator.MD5.generate());
userEntity.setId(getIdGenerator().generate());
userEntity.setSalt(IDGenerator.RANDOM.generate());
userEntity.setStatus(DataStatus.STATUS_ENABLED);
//验证其他属性
@@ -168,9 +173,6 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
@Override
@Caching(evict = {
@CacheEvict(value = CacheConstants.USER_CACHE_NAME, key = "#userId")
// , @CacheEvict(value = CacheConstants.USER_AUTH_CACHE_NAME, key = "#userId"),
// @CacheEvict(value = CacheConstants.USER_AUTH_CACHE_NAME, key = "'user-menu-list:'+#userId"),
// @CacheEvict(value = CacheConstants.USER_AUTH_CACHE_NAME, key = "'menu-tree:'+#userId")
})
public void update(String userId, UserEntity userEntity) {
userEntity.setId(userId);
@@ -298,6 +300,7 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
if (CollectionUtils.isEmpty(roleIdList)) {
return new java.util.ArrayList<>();
}
// org.hswebframework.web.service.authorization.simple.terms.UserInRoleSqlTerm
return createQuery()
.where("id", "user-in-role", roleIdList)
.listNoPaging();