Container based Security and Spring Security
Join the DZone community and get the full member experience.Join For Free
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, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.