1

Does Spring Security provide any way to authorize a user in java class, the way it provides tags for authorization in JSPs (such as <sec:authorize ifAllGranted="ROLE_ADMIN"/>)?

I am expecting a static method of some class that I can use in my code like this:

if(SomeSpringSecurityClass.authorize("ROLE_ADMIN")){
...
}
craftsman
  • 15,133
  • 17
  • 70
  • 86

1 Answers1

1

You'd better do this check declaratively, but if you still need programmatic way - it has already been discussed on stackoverflow earlier: How to check "hasRole" in Java Code with Spring Security?

In short words, there's no such single ready-made method but you can start from SecurityContextHolder.getContext() to perform this check.

Community
  • 1
  • 1
Paul Lysak
  • 1,284
  • 1
  • 14
  • 18