Enterprise Architect in HCL Technologies a $7Billion IT services organization. My role is to work as a Technology Partner for large enterprise customers providing them low cost opensource solutions around Java, Spring and vFabric stack. I am also working on various projects involving, Cloud base solution, Mobile application and Business Analytics around Spring and vFabric space. Over 23 yrs, I have build repository of technologies and tools I liked and used extensively in my day to day work. In this blog, I am putting all these best practices and tools so that it will help the people who visit my website. Krishna is a DZone MVB and is not an employee of DZone and has posted 64 posts at DZone. You can read more from them at their website. View Full User Profile

Container based Security and Spring Security

12.19.2012
| 3062 views |
  • submit to reddit

One of the materials on internet that talks about the differences between Container based Security framework and Spring Security is Spring Security FAQ. It lays down the power of Spring Security. Spring MVC based application and other Spring Based application can take advantage of Spring Security

Authentication is a way to provide user identity so that the application identifies who logged into the system. Authorization is a way to tell who can access which part of the application.

As mentioned in that material Container offers Realm based authentication that resides within a containers server.xml vs as opposed to Spring Security that offers Authentication Providers that sits in the application config file,

A simple Realm is Tomcat’s MemoryRealm and it depends on tomcat-users.xml to configure the users, in reality we use LDAP or any database to store user information.

<Realm className="org.apache.catalina.realm.MemoryRealm" />
<role rolename="secureconn"/>
<user username="client" password="password" roles="secureconn"/>
<role rolename="secureconn1"/>
<user username="client1" password="password" roles="secureconn1"/>

A simple Authentication Provider looks as below, and it is not specific to a container. Again in reality the user information will be in LDAP or Database.

<authentication-manager>
<authentication-provider>
<!-- <password-encoder ref="encoder"/>-->
<user-service id="accountService">
<user name="client" password="" authorities="secureconn" />
<user name="client1" password="" authorities="secureconn1" />
</user-service>
</authentication-provider>
</authentication-manager>

In container based security the Authorization can be achieved by a simple, security constraint mechanism as below,

<security-constraint>
<web-resource-collection>
<web-resource-name>Demo App</web-resource-name>
<url-pattern>/secure/*</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>secureconn</role-name>
</auth-constraint>
</security-constraint>

In spring we can achieve Authorization as below, in the spring-security context. The authorization in Container based Security is limited to virtual folder within the container. But with Spring Security we can provide regular expression and secure portions of application.

<http use-expressions="true">
<intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" requires-channel="https"/>
<intercept-url pattern="/secure1/**" access="hasRole('supervisor')"/>
<intercept-url pattern="/secure/**" access="isAuthenticated()" />
</http>

Spring also extends security further to Service layer using a technique called ACL, refer Spring Security document.

If you notice for simple authentication/authorization capabilities container based security is enough, but for more complex enterprise service level security, it make sense to consider Spring Security.

I hope this blog helped.

Published at DZone with permission of Krishna Prasad, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)