diff --git a/src/main/java/org/casbin/jcasbin/persist/Helper.java b/src/main/java/org/casbin/jcasbin/persist/Helper.java index 4f224218..f36ff7bc 100644 --- a/src/main/java/org/casbin/jcasbin/persist/Helper.java +++ b/src/main/java/org/casbin/jcasbin/persist/Helper.java @@ -19,6 +19,7 @@ import java.util.Arrays; import java.util.List; +import java.util.Map; import static org.casbin.jcasbin.util.Util.splitCommaDelimited; @@ -40,7 +41,14 @@ public static void loadPolicyLine(String line, Model model) { String key = tokens[0]; String sec = key.substring(0, 1); - Assertion ast = model.model.get(sec).get(key); + Map astMap = model.model.get(sec); + if(astMap == null) { + return; + } + Assertion ast = astMap.get(key); + if(ast == null) { + return; + } List policy = Arrays.asList(Arrays.copyOfRange(tokens, 1, tokens.length)); ast.policy.add(policy); ast.policyIndex.put(policy.toString(), ast.policy.size() - 1); diff --git a/src/main/java/org/casbin/jcasbin/persist/file_adapter/FileAdapter.java b/src/main/java/org/casbin/jcasbin/persist/file_adapter/FileAdapter.java index e6af32ba..555008ef 100644 --- a/src/main/java/org/casbin/jcasbin/persist/file_adapter/FileAdapter.java +++ b/src/main/java/org/casbin/jcasbin/persist/file_adapter/FileAdapter.java @@ -154,7 +154,7 @@ public void removePolicy(String sec, String ptype, List rule) { } try { List lines = IOUtils.readLines(new FileInputStream(filePath), Charset.forName("UTF-8")); - lines.remove(ruleText); + lines.removeIf(line -> line.replaceAll("\\s", "").equals(ruleText.replaceAll("\\s", ""))); savePolicyFile(String.join("\n", lines)); } catch (IOException e) { e.printStackTrace();