我不明白为什么这个简单的方法不起作用
我的班级有两个方法AccessChecker
的属性:Hashtable<String, List<Permission>> permissions = new Hashtable<String, List<Permission>>();
第一种方法
如果我把this.permissions.containsKey(key)
这个方法放在最后,用一个好的键,它就可以工作。
public void updatePermissions() {
List<Permission> permissionsTmp = permissionRepository.findAllWithEagerRelationships();
// clear permissions
this.permissions = new Hashtable<>();
// load all permissions
for (Permission permission : permissionsTmp) {
Profil profil = profilRepository.findOne(permission.getProfil().getId());
permission.setProfil(profil);
UserFonc user = userFoncRepository.findOne(permission.getUserFonc().getId());
permission.setUserFonc(user);
log.error("updatePermissions ** user login = " + user.getNom());
for (WebService webService: profil.getWebServices()) {
String key = createKeyPermissions(user.getNom().toLowerCase(), webService.getNom(), webService.getMethode());
log.error("updatePermissions ** key = " + key);
if (this.permissions.containsKey(key)){
this.permissions.get(key).add(permission);
}
else {
List<Permission> newPermissions = new ArrayList<>();
newPermissions.add(permission);
this.permissions.put(key, newPermissions);
}
}
}
}
第二种方法
但是,当我在方法中执行此操作时hasAccessToWebservice()
,它不适用于同一个键......
public boolean hasAccessToWebservice(HttpServletRequest request) {
boolean hasAccess = false;
String webservice = getServiceFromRequest(request);
String methode = request.getMethod();
String login = SecurityUtils.getCurrentUserLogin();
String userAgent = request.getHeader(Constants.USER_AGENT);
final String userLogin = SecurityUtils.getCurrentUserLogin();
log.error("hasAccessToWebservice ** user login = " + userLogin);
String key = createKeyPermissions(login.toLowerCase(), webservice, methode);
log.error("hasAccessToWebservice ** key = " + key);
log.error("hasAccessToWebservice ** element = " + this.permissions.size());
Set t = this.permissions.keySet();
log.error(t.toString());
if (this.permissions.containsKey(key)) {
log.error("hasAccessToWebservice ** key found !!");
hasAccess = true;
}
return hasAccess;
}
你能解释一下为什么吗?
谢谢