Spring Security Ramesh Draft2
Spring Security Ramesh Draft2
Introduction
Security is one of the most critical architectural components of any web-based application.
Authentication
Inadvertent privilege escalation due to lack of URL protection and general authentication.
Authentication identifies who is attempting to request a resource.
The different types of authentications are credential based authentication, Hardware authentication,
etc.
Unauthenticated (Anonymous) areas do not:
1) Require a user to log into the system
2) Display sensitive information, such as names, addresses, credit cards, and orders
3) Provide functionality to manipulate the overall state of the system or its data
Authorization
Inappropriate or non-existent use of authorization.
Authorization (such as roles) uses the information that was validated during authentication to
determine if access should be granted to a particular resource.
Sensitive information
Personally identifiable or sensitive information is easily accessible or unencrypted.
Significant and sensitive pieces of data were completely unencrypted or masked anywhere in the
system.
Transport-level protection
Insecure transport-level protection due to lack of SSL encryption.
SSL protection (using https) ensures that communication between the browser client and the web
application server are secure against any kinds of tampering and snooping.
Conclusion:
1. Authentication protects application.
2. Authorization protects web pages in application.
3. Https (using certificates) protects data between browser and server.
Spring Security provides everything we need to implement a top-to-bottom application security solution
in a concise and sensible way. Spring Security offers out-of-the-box integration with many common
enterprise authentication systems; so it's adaptable to most situations with little effort of the developer.
DelegatingFilterProxy is a special servlet filter that, by itself, doesn’t do much. Instead, it delegates
FilterChainProxy, which is an implementation of javax.servlet.Filter. The FilterChainProxy is
automatically registered as a <bean> in the spring application context with the bean id always as
‘springSecurityFilterChain’. Hence the <filter-name> in web.xml file must be always
‘springSecurityFilterChain’.
public class DelegatingFilterProxy implements Filter {
void doFilter(request, response, filterChain) {
Filter delegate = applicationContet.getBean("springSecurityFilterChain")
delegate.doFilter(request,response,filterChain);
}
}
Note: Spring Security will automatically register FilterChainProxy if and only if we add <http> in spring
security configuration file (security.xml).
Spring Security XML configuration file
Spring security configuration file is required to get our application secured. This configuration file uses
spring security name space (http://www.springframework.org/schema/security).
The following file provides: A default login page, a default logout page, authenticate the user, and
require the logged-in user to be associated to ROLE_USER.
#WEB-INF/spring/security.xml
<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns:bean="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http auto-config="true">
<intercept-url pattern="/**" access="ROLE_USER"/>
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="[email protected]" password="user" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>
</bean:beans>
The <authentication-manager> element is how Spring Security authenticates the user. In this instance,
we utilize an in-memory data store to compare a username and password.
<http>
The <http> element automatically creates FilterChainProxy and registered as a <bean> in the spring
application context with the bean id ‘springSecurityFilterChain’. The FilterChainProxy chains together
with one or more additional filters. Hence developers need not to configure all these filter beans
(including chain filters) explicitly in spring configuration file.
The following table contains attributes of <http> element:
Attribute name Description
‘security’ When set to ‘none’, no security filters will be created and <http> element
should be empty, with no children.
‘auto-config’ If set to "true" then automatically registers a login form, BASIC
authentication, anonymous authentication, logout services, remember-me.
The default value is "false".
‘use-expressions’ Enables the SPring Expression Language (SPEL).
The default value is “false”.
<form-login>
Sets up a form login configuration for authentication with a username and password.
The following table contains attributes of <form-login> element:
Attribute name Description
‘login-page’ The URL for the login page. If no login URL is specified, Spring Security
will automatically create a login URL at ‘/spring_security_login’
‘login-processing-url’ The URL that the login form is posted to.
Default value is ‘/j_spring_security_check’
‘username-parameter’ Form field name in login page.
Default value is ‘j_username’
‘password-parameter’ Form field name in login page.
Default value is ‘j_password’
‘authentication-failure-url’ The URL for the login failure page.
If no login failure URL is specified, Spring Security will automatically
create a failure login URL at ‘/spring_security_login?login_error’
<logout>
Incorporates a logout processing filter.
The following table contains attributes of <logout> element:
Attribute name Description
‘logout-url’ Specifies the URL that will cause a logout.
Spring Security will initialize a filter that responds to this particular URL.
Defaults to ‘/j_spring_security_logout’ if unspecified.
<http auto-config="true">
<intercept-url pattern="/**" access="ROLE_USER"/>
</http>
The above configuration internally becomes:
<http auto-config="true">
<form-login login-page="/spring_security_login"
login-processing-url="/j_spring_security_check"
username-parameter="j_username"
password-parameter="j_password"
authentication-failure-url="/spring_security_login?login_error"/>
<http-basic/>
<logout logout-url="/j_spring_security_logout" />
<intercept-url pattern="/**" access="ROLE_USER"/>
</http>
Update the Maven pom.xml file with necessary Spring Security .jar files.
#pom.xml
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
Login page can be accessed using “http://localhost:9090/chapter02.01-calendar/spring_security_login”
or “http://localhost:9090/chapter02.01-calendar/”
2. Allow anonymous users to access the Welcome, Login, and Logout pages.
<intercept-url pattern="/" access="ROLE_ANONYMOUS, ROLE_USER" />
<intercept-url pattern="/login/*" access="ROLE_ANONYMOUS, ROLE_USER" />
<intercept-url pattern="/logout" access="ROLE_ANONYMOUS, ROLE_USER" />
SpEL
Security is add on to business class hence it is an aspect. The security aspect is automatically provided by
Spring Security. The Spring Security is a security framework which provides declarative security.
Spring Security provides authentication and authorization at both web request level and method
invocation level.
Spring Security 2.0 released security specific namespace (spring-security-3.2.xsd) to reduce
configuration file size.
Spring Security 3.0 added SpEL (Spring Expression Language) to reduce configuration file size even more.
Spring Security 3.2 tackles security from 2 angles:
1. To secure web requests and restrict access at the URL level, Spring Security uses servlet filters.
2. Spring Security can also secure method invocations using Spring AOP, proxying objects and
applying advice to ensure that the user has the proper authority to invoke secured methods.
We can get auto generated login form via the path: http://localhost:9090/spring-security-helloworld-
xml/spring_security_login
Note: To put our own login page in place, we’ll need to configure a <form-login> element to override
the default behavior.
<intercept-url pattern="/**" access="ROLE_USER" />
In this case, we’ve set the pattern attribute to /**, indicating that we want all requests, regardless of the
URL, to require ROLE_USER access. The /** has a broad reach, but we can be more specific.
<intercept-url pattern="/admin/**" access="ROLE_ADMIN"/>
This <intercept-url> restricts access to the /admin branch of the site’s hierarchy to users with
ROLE_ADMIN authority.
We can use as many <intercept-url> entries as we like to secure various paths in our web application.
But it’s important to know that the <intercept-url> rules are applied top to bottom. Therefore, this new
<intercept-url> should be placed before the original one or else it’ll be eclipsed by the broad scope of
the /** path.
<intercept-urlpattern="/admin/**"access="ROLE_ADMIN"/>
<intercept-url pattern="/**" access="ROLE_USER" />
Spring Security also supports SpEL (Spring Expression Language) for declaring access requirements. To
enable it, we must set the use-expressions attribute of <http> to true:
<http auto-config="true" use-expressions="true"> ... </http>
Now we can start using SpEL expressions in the access attribute. Here’s how to use a SpEL expression to require
ROLE_ADMIN access for the /admin/** URL pattern:
<intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')"/>
It is always recommended to encrypt and send sensitive information over HTTPS.
The <intercept-url> element’s requires-channel attribute shifts the responsibility for channel
enforcement into the Spring Security configuration.
<intercept-url pattern="/form" requires-channel="https"/>
The home page doesn’t require HTTPS, so we can declare that it always should be sent over HTTP:
<intercept-urlpattern="/home" requires-channel="http"/>
Spring Security comes with a JSP tag library, which includes very few tags such as <s:authentication>,
<s:authorize> and <s:accesscontrollist>.
To use the JSP tag library, we’ll need to declare it in the JSP files where it’s used:
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="s"%>
One of the easiest authentication options available is to declare the user details directly in the Spring
configuration.
<authentication-manager>
<authentication-provider>
<user-service>
<user name="aspire" password="aspire1234" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>