Skip to content

Instantly share code, notes, and snippets.

@runeflobakk
Created October 18, 2011 20:03
Show Gist options
  • Save runeflobakk/1296543 to your computer and use it in GitHub Desktop.
Save runeflobakk/1296543 to your computer and use it in GitHub Desktop.
public class SecurityLevelVoter implements AccessDecisionVoter {
private String securityLevelPrefix = "SECURITY_LEVEL_";
public int vote(Authentication authentication,
Object object,
Collection<ConfigAttribute> attributes) {
int result = ACCESS_ABSTAIN;
Object principal = authentication.getPrincipal();
for (ConfigAttribute attribute : attributes) {
if (this.supports(attribute)) {
result = ACCESS_DENIED;
if (principal instanceof MyPrincipal) {
int requiredSecurityLevel = getRequiredSecurityLevel(attribute);
int securityLevel = getUserSecurityLevel((MyPrincipal) principal);
if (securityLevel >= requiredSecurityLevel) {
return ACCESS_GRANTED;
}
}
}
}
return result;
}
private int getRequiredSecurityLevel(ConfigAttribute attribute) {
String requiredSecurityLevel =
removeStart(attribute.getAttribute(), securityLevelPrefix);
return toInt(requiredSecurityLevel, Integer.MAX_VALUE);
}
private int getUserSecurityLevel(MyPrincipal principal) {
return toInt(principal.getSecurityLevel(), Integer.MIN_VALUE);
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment