-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion to improve LoginUserFinder
logic
#39
Labels
Comments
...
@Override
public Object get() {
AAAContext aaaContext = AAA.context();
if (null != aaaContext) {
Principal principal = aaaContext.getCurrentPrincipal();
if (null != principal) {
if (principal instanceof UserBase) {
return principal;
}
String fieldName = this.querySpec;
AAAPlugin plugin = Act.getInstance(AAAPlugin.class);
List<Field> idFields = $.fieldsOf(dao.modelType(), field -> field.getAnnotation(Id.class) != null);
if (C.notEmpty(idFields)) {
Field idField = idFields.get(0);
if (S.eq(idField.getName(), fieldName)) {
Object idValue = $.getProperty(principal, fieldName);// $.convert($.getFieldValue(principal, idField)).to(dao.idType());
String cacheKey = S.string(idValue);
Object cached = plugin.cachedUser(cacheKey);
if (null == cached) {
cached = dao.findById(idValue);
if (null != cached) {
plugin.cacheUser(cacheKey, cached);
}
}
return cached;
}
}
fieldName = principal.getName();
int pos = fieldName.indexOf(':');
Object fieldValue;
if (pos > 0) {
fieldName = fieldName.substring(0, pos);
fieldValue = fieldName.substring(pos + 1);
} else {
fieldValue = $.getProperty(dao.modelType(), fieldName);
}
String cacheKey = S.concat(fieldName, "::", fieldValue);
Object cached = plugin.cachedUser(cacheKey);
if (null == cached) {
Field field = $.fieldOf(dao.modelType(), fieldName);
if (field != null) {
fieldValue = $.convert(fieldValue).to(field.getType());
cached = dao.findOneBy(fieldName, fieldValue);
if (null != cached) {
plugin.cacheUser(cacheKey, cached);
}
}
}
return cached;
}
}
return null;
}
... |
greenlaw110
changed the title
建议修改aaa中使用"id"作为user.key时的判断及值获取方式
Suggestion to improve Mar 5, 2020
LoginUserFinder
logic
greenlaw110
added a commit
that referenced
this issue
Mar 5, 2020
Changes
|
greenlaw110
added a commit
that referenced
this issue
Mar 7, 2020
userKeyType = $.fieldOf(userType, AAAConfig.user.key.get()).getType();没必要定于为全局字段和在initialized()中初始化 @Override
protected void initialized() {
App app = App.instance();
userType = spec.rawType();
userKeyType = $.fieldOf(userType, AAAConfig.user.key.get()).getType();
dao = app.dbServiceManager().dao(userType);
querySpec = S.string(options.get(KEY_USER_KEY));
if (S.blank(querySpec)) {
querySpec = AAAConfig.user.key.get();
}
} 应当放到如下位置, 并且将查询的field name修改为querySpec String name = principal.getName();
int pos = name.indexOf(':');
if (pos > 0) {
querySpec = name.substring(0, pos);
name = name.substring(pos + 1);
} Class<?> userKeyType = $.fieldOf(userType, querySpec).getType(); String cacheKey = S.concat(querySpec, "::", name);
Object cached = plugin.cachedUser(cacheKey);
if (null == cached) {
Object val = userKeyType == String.class ? name : $.convert(name).to(userKeyType);
cached = dao.findOneBy(querySpec, val);
if (null != cached) {
plugin.cacheUser(cacheKey, cached);
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
目前 aaa 框架中, 使用
Entity#id
作为user.key
时, 直接使用id.equels()
方式判断并获取idValue
, 这种方式存在如下问题:当
Entity#id
字段用其他名称命名, 则该方法会失效, 例如实体为User
, id 字段命名为userId
时,principal.getProperty("id")
返回idValue
值为null如果把 id 字段作为通用的
username
, 即propName:propValue
, 因为数据类型的原因, 无法获取principal
实例 (aaa 框架中propName:propValue
的数据类型默认为String
类型)建议:
通过反射
@Id
判断是否为 id通用
propName:propValue
方式加入 prop 数据类型的获取和转换, 兼容非String
类型 prop 作为user.key
的情况另外,
user.key
应具有唯一性, 可以考虑将 id 为 user.key 的情况作为通用情况处理, 在加入数据类型处理的前提下, 不将 id 作为特例情况单独判断, 统一使用dao.findOneBy(key, value)
获取 User, 则配置项aaa.user.key
可以取消, 统一采用principal.getName()
返回 "propName:propValue" 的方式来获取USER,以此降低 aaa 框架使用的复杂度!The text was updated successfully, but these errors were encountered: