views:

1061

answers:

3

I have a concern when it comes to GrantedAuthority objects in a Spring Security application. I'm looking for a good way to handle things. First of all I'm trying to describe my concern, if there are any factual errors do not hesitate to point them out, I'll only be greatful.

Spring Security uses GrantedAuthority instances to act as tokens of authorization in different parts of the application.

By default a GrantedAuthority may present itself as a String. When methods are secured using *@Secured("ROLE_NAME")*, or URL's are secured using the Spring XML configuration or when the HttpServletRequest request is checked programmatically as in *if(request.isUserInRole("ROLE_NAME")) {..}* it's always the String that you are using to specify the authority which is checked for.

I'm wondering about the implications of using static strings in several places of the application. If a role name is changed the developer has to hunt down all the old strings and update them. There will be no compile time error if a String is missed, only a problem at runtime.

What is the best way according to you when it comes to handling GrantedAuthority objects in a Spring Security application? What pros and cons does your solution have?

A: 

I do not see a big problem here. It is very unlikely for GrantedAuthority to change the key. Just do not name your roles ROLE_A.

Also, I personally prefer XML security configuration over annotations. In general to keep any related configurations in one place looks like a good idea.

Georgy Bolyuba
The problem I see is that it's easy to misspell something and not find out about it until the application is tested or run. Being alterted of declarative errors during compilation is a plus in my book.And there is always a balance between keeping "related configurations" together and keeping them together with what they are configuring. For instance I prefer annotations in most cases where a configuration doesn't change between the test, dev and prod environments. That's why I use annotations for Hibernate entities and XML configuration for my Spring beans.
DeletedAccount
s/alterted/alerted
DeletedAccount
A: 

In Spring and other frameworks (especially for dynamic languages) "Convention over Configuration" is used. If you were free to define the role names yourself, you easily find out that much more lines of code are needed.

So stick to the convention. Always use the 3 following roles, ROLE_ANONYMOUS, ROLE_ADMIN, and ROLE_USER. If you need another one, name it accordingly and use it in all occasions. Documentations is important.

Also unit testing is imported. It helps you in the cases that an error isn't caught by the compiler.

kgiannakakis
Thanks for your answer, but I think you are confused. Convention over configuration isn't related to this at all, I have to configure the role names in each and every place I use them. Convention over configuration is when I don't have to configure something, unless I want to break a convention. Those three role names may be commonly used. But I still don't get any compile time errors, which was my complaint. Unit testing is good for sure, I still don't see any clear advantage of not having the option to use declarations cought by the compiler. Silent errors are never a good thing. All IMHO.:)
DeletedAccount
With `I have to configure the role names in each and every place I use them` I mean that I write the name of the accepted role(s) each time. Nothing is stopping me from performing a spelling error, it will simple be interpreted as a new role (which no user has). In that sense I'm doing configuration all the time when using `@Secured`.
DeletedAccount
post has nothing todo with question.
Salvin Francis
+1  A: 

First off, if possible, only do your checks at a particular place in the application (e.g. an HTTP interceptor at the beginning of the request), and using only one of the mentioned approaches. This is a good practice since you will be able to know authoritatively when a user becomes authorized.

If this is not possible, use enums for the role names and only compare on the enums. Therefore, if you wanted to locate all usages in the application it's a simple search.

eqbridges
+1. This problem of changing Strings / making constants/enums is nothing specific to using Spring Security - a good practice no matter what.
matt b
I'm using a combination of method security in the Spring (service) layer and component security in the Wicket (presentation) layer. The idea is that if something is missed in one of the layers it's most likely cought in the other. The risk of users not being allowed to execute code they should be allowed to execute increases a bit, but I think it's worth it.
DeletedAccount
Using enums is exactly what I would like to do. But as far as I have come it seems impossible as the GrantedAuthority interface extends Comparable. The result in my enum is `Name clash: The method compareTo(Object) of type A has the same erasure as compareTo(T) of type Comparable<T> but does not override it` where A is the name of my enum.
DeletedAccount
Yes, that's a known issue with Spring Security. You need to create a enum class as opposed to using a Java 5 enum.
eqbridges