Security checker is a programmatically way for evaluating Spring Security expressions, normally evaluated by @PreAuthorize
annotation (a SpEL-enabled equivalent to @Secured
annotation).
The annotation way applies only to certain Spring enabled beans, but in some case your project may need to evaluate the same kind of expressions elsewhere in the code.
##Usage Enable global method security:
<security:global-method-security pre-post-annotations="enabled" />
Simply pass standard or SpEL security expression to check
method. For example:
SecurityChecker.check("hasRole('ROLE_USER')");
A complete reference can be found here
Thanks for sharing, this helped a lot.
Just want to add some notes in case of ppl who is using spring boot and spring boot oauth2,
Replace
MethodSecurityExpressionHandler expressionHandler = ContextLoader.getCurrentWebApplicationContext().getBean(DefaultMethodSecurityExpressionHandler.class);withOAuth2MethodSecurityExpressionHandler expressionHandler = new OAuth2MethodSecurityExpressionHandler();
will make it work