Acegi Security's biggest strength, and it's biggest weakness, is there are 100 different ways you can do anything.
Your instinct of using a custom AccessDecisionVoter was actually the most optimal answer. Why? Because it's AOP. Cross-cutting security logic, such as who is allowed work with a particular sized LoanApplication, is usually better put into a LoanApplicationVoter because this abstracts it from application code. This in turn increases testability and allows business logic to be more tightly focused. You could use standard JavaBean properties to modify the different loan levels via Spring IoC.
However, if you want to do it programatically in your services layer, just use:
Code:
// adapted from ContextHolderAwareRequestWrapper
public static boolean isGranted(Authentication auth, String role) {
if ((auth == null) || (auth.getPrincipal() == null)
|| (auth.getAuthorities() == null)) {
return false;
}
for (int i = 0; i < auth.getAuthorities().length; i++) {
if (role.equals(auth.getAuthorities()[i].getAuthority())) {
return true;
}
}
return false;
}
public void myMethod() {
if(loan.getAmount > LOAN_CUTOFF) {
if(isGranted(SecurityContextHolder.getContext().getAuthentication(), "ROLE_MANAGER") {
// approve loan
} else {
// throw exception
}
}
}