forked from mostafa-eltaher/AbacSpringSecurity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc95f1a
commit ad8d4cb
Showing
4 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/edu/mostafa/abac/security/policy/json/JsonFilePolicyDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package edu.mostafa.abac.security.policy.json; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.expression.Expression; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
|
||
import edu.mostafa.abac.security.policy.PolicyDefinition; | ||
import edu.mostafa.abac.security.policy.PolicyRule; | ||
|
||
@Component | ||
public class JsonFilePolicyDefinition implements PolicyDefinition { | ||
private static Logger logger = LoggerFactory.getLogger(JsonFilePolicyDefinition.class); | ||
|
||
private static String DEFAULT_POLICY_FILE_NAME = "default-policy.json"; | ||
|
||
private List<PolicyRule> rules; | ||
|
||
@PostConstruct | ||
private void init(){ | ||
ObjectMapper mapper = new ObjectMapper(); | ||
SimpleModule module = new SimpleModule(); | ||
module.addDeserializer(Expression.class, new SpelDeserializer()); | ||
mapper.registerModule(module); | ||
try { | ||
PolicyRule[] staff = mapper.readValue(getClass().getResourceAsStream(DEFAULT_POLICY_FILE_NAME), PolicyRule[].class); | ||
this.rules = Arrays.asList(staff); | ||
} catch (JsonMappingException e) { | ||
logger.error("An error occurred while parsing the policy file.", e); | ||
} catch (IOException e) { | ||
logger.error("An error occurred while reading the policy file.", e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<PolicyRule> getAllPolicyRules() { | ||
return rules; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/edu/mostafa/abac/security/policy/json/SpelDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package edu.mostafa.abac.security.policy.json; | ||
|
||
import java.io.IOException; | ||
|
||
import org.springframework.expression.Expression; | ||
import org.springframework.expression.ExpressionParser; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
|
||
public class SpelDeserializer extends StdDeserializer<Expression> { | ||
ExpressionParser elParser = new SpelExpressionParser(); | ||
|
||
public SpelDeserializer(){ | ||
this(null); | ||
} | ||
|
||
protected SpelDeserializer(Class<?> vc) { | ||
super(vc); | ||
} | ||
|
||
@Override | ||
public Expression deserialize(JsonParser jp, DeserializationContext ctxt) | ||
throws IOException, JsonProcessingException { | ||
String expresionString = jp.getCodec().readValue(jp, String.class); | ||
return elParser.parseExpression(expresionString); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/resources/edu/mostafa/abac/security/policy/json/default-policy.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[ | ||
{ | ||
"name": "ResourceOwner", | ||
"description": "Resource owner should have access to it.", | ||
"target": "true", | ||
"condition": "subject.name == resource.owner" | ||
} | ||
] |