Total Pageviews

Tuesday, 27 August 2013

Scene 3

NOTE

  • we can use as many <http> in spring security config as we want e.g. look at the following one:

<http pattern="/resources/**" security="none"/>

All it says is IGNORE any request if that request has /resources/ in it, this is because we can keep all our images, css, javascript, or any other resources required by the application to run but are not treated as secured information.


  • Each <http> is considered in order and only the First Match is Applied
  • The implication of this is the last rule we will be using is the once that matches everything:
    • <http pattern="/**" security="none"/>
  • If this rule (i.e. specifying /** in the last <http> tag) is not followed then Spring Security will generate an error. 
  • Tip: The query strings are ignored when matching a request. 
  • The <intercept-url tags are also considered in order they are defined just as <http> tags. 
  • So we defined the most specific elements first and then later we define the more general rules. 
  • If we do the inverse then the Spring Security will generate a warning about it. because as the general rule we defined is a match and the first match applied so it will always ignore the the more specific rule defined afterwards. - Read the image below
  • Defining an <http> with attribute security="none" should not have any child <intercept-url> tag because it does not make any sense and spring security will generate a warning - Read the image below:
  • We can define pattern attribute in <http> as well but the pattern attribute in <http> and pattern attribute in <intercept-url> are not aware of each other and are independent. 




PATTERNS

  • pattern /events/**
    • /events - YES
    • /events/ - YES
    • /events/event1/features/?page=1 - YES
    • /events/?page=1 - YES
    • /events123 - NO
  • pattern /events*
    • /events1 - YES
    • /events/mainevent - NO
    • /events/ - NO (I dont know why) :-(
  • pattern /events*/**
    • matches all patterns above including NO onces as well. 

what we studied up to now about <intercept-url was BASIC ROLE BASED AUTHORIZATION lets take a look at EXPRESSION-BASED AUTHORIZATION 

EXPRESSION-BASED AUTHORIZATION
Spring provides an expression based authorization where we can use spring expression language to specify the access rules
How to do it:
  • add use-expressions="true" in <http as an attribute
  • Add the expression based rules e.g. 
    • <intercept-url pattern="/logout*" access="permitAll" />
    • <intercept-url pattern="/login/*" access="permitAll" />
    • <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    • permitAll and hasRole('ROLE_USER')  are the expressions based on which the authorization is handled. 
CONDITIONALLY DISPLAYING AUTHENTICATION INFORMATION
At the moment we dont display the username of the logged in user or any thing information like that where we can guess the user is logged in. 
  • Spring Security provides a taglib for this, the maven dependency for that is given below we are using version 3.1.2-Release

  • Add reference to the taglib in your jsp like:
    • <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
  • Add following to your header.jsp or where you want to show the currently logged in user. 


  • <sec:authorize /> is a tag from spring security taglib and determines if a user is logged in or not and gives the results in a variable you specify in var attribute. 
  • <sec:authentication /> tag looks for current Authencation object , this will be covered later in details. 
Customizing behavior after Login
  • add default-target-url="/default" to security config under <login-form tag where /default is the default url where we want to go to
  • Add a controller to you app or handle this /default request and check what role the user has and redirect appropriately. 





Spring Security - Scene 2

Now when we have a basic login stuff working, its time to start polishing the work so that we can move towards a system which can be used for production and customer facing.

Our own login page
The login page we were using was provided by the spring security which will not match the look and feel of our overall application, so we now need to have our own login page.

so to do this we need to change the security config as given below:


  • we specified /login/form as our login page url which could go through a controller or just specify this url in the web.xml and specify the jsp file which will be hit when /login/form requested
  • login-processing-url will go to /j_spring_security_check by default and specifies the url where the login form shall be submitted to. 
  • username and password parameters default to j_username and j_password but we can change then as i did above (customusername and custompassword). 
  • authentication_failure_url specifies the destination to go to when authentication is failed. 
In web.xml file we have included the mapping for /login/form as following


Here is the custom login page
The custom login page is essentially the jsp page with a form having login and password fields, lets have a look at the login page. 


  • We are looking for ${param.error} if there was problem because we have specified this param in authentication-failure-url attribute of <form-login tag in security config

Configuring Logout!

Last but not least we have to configure the logout logic for the app, 
  • configure the logout url in security config file as shown below
  • Add a logout link in your jsp pages which will go to /logout as we have configured it above. 
  • Add a message on login.jsp (because the logout functionality will to /login/form?logout as a result) and check for this logout param in the request and if its present then show the appropriate message as below:
if you run the application at this point you will get an error message from browser saying something like the page is in a redirection loop

This is because Spring is No More rendering the login page and we have our own login page which is rendered when users hit /login or any other page that requires authentication. 

  • we have this rule in our security config
    • <intercept-url pattern="/**" access="ROLE_USER"/>
  • Which means only users with ROLE_USER will be able to see ANY PAGE in the application
  • which means if users want to even go to our login page they wont be allowed to go there
  • hence we need to change it. 
Here what we will do:



so we have added some additional rules say skip any security stuff if users try to access / which is root of the application i.e. Welcome page or any page that has /login in its url which means the it cannot be a page which is meant to be secure or /logout which is for obvious reasons.
There is an additional url for ROLE_USER which is just an example of having as many different role as you want so basically and URL having /events/ in it will not be available if the user does not have ROLE_USER

and now if we run the application again we will see following:




Hurrayyy!!!

when you hit this login button with the correct username and password you will see the secured page you requested for (in my case its index.jsp which i have specified in <welcome-file-list> in web.xml

below is what i see in my case when the login is successful.




Whats the next scene please!!!




Friday, 5 July 2013

Scene 1

Spring framework provides a very nice way of securing your web application. 
We will go through its configuration step by step: 
We will be using maven for dependency management. 

1. Put in the spring security dependencies in your POM file. 


<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>

Note: 

  • Spring Security 3.1 builds with spring 3.0
  • so when you try to include the maven dependencies for Spring Security 3.1, maven automatically brings in spring 3.0 library files
  • If you have to use Spring 3.0 in your application then its fine BUT
  • if you have to use Spring 3.1 then you have to either
    • Declare Spring 3.1 dependencies explicitly OR
    • use maven Dependency Management Tool

Step 2: SETTING UP SPRING CONFIGURATION XML FILE

The next step is to create a spring XML file configuration file for spring security: 

  • The above is the minimal configuration to implement a standard security for an application.
  • The above configuration uses an xml namespace (http://www.springframework.org/schema/security)  to define different security related tags. 

Note: Whats in this config?

  • <http> element creates a servlet filter - which ensures that the current user is associated with the appropriate role. 
  • In the example above the "servlet filter" will ensure that the user "user@example.com" is associated with "ROLE_USER"
  • The name of role is ARBITRARY - so we can create as many roles as we want and give them any names. 
  • <authentication-manager> is how spring security authenticates the user - for this example above we are using an in-memory data store to match username and password which is user1@example.com and user1

Step 3: UPDATE WEB.XML

  • The next step is to update the web.xml file
    • ContextLoaderListener - The first update in the web.xml is to add ContextLoaderListener which starts the and stops the root ApplicationContext Interface of Spring application. [This goes into any application that uses Spring anyway] - CotextLoaderListener determines what configuration to load by looking at <context-param> tag for contextConfigLocation - See Image Below

Now the security.xml will be loaded when the application starts. 
  • For loading multiple files we could use /WEB-INF/spring/*.xml 
Note:
  • Spring's DisplatcherServlet and ContextLoaderListener both define their own contextConfigLocation 


The contextLoaderListener is the parent context (root context) where as the DispatcherServletContext is a child context. The Child Context beans Can Access the beans defined int he parent context but the parent context beans Cannot Access the beans defined int he child context. 



TIP: <filter-mapping> elements in web.ml are considered in the order they are mentioned in the web.xml

springSecurityFilterChain

DelegatingFilterProxy
The org.springramework.web.filter.DelegatingFilterProxy is a servlet filter which delegates all of its work to a Spring bean (From the Root Application Context) that must implement javax.servlet.Filter 

By default the bean is looked up by name using the value of <filter-name> we must ensure we use springSecurityFilterChain as value of <filter-name>  ??

As we said that DelegatingFilterProxy will delegate its work to something else (a servlet filter) , what something else is called FilterChainProxy which can apply filters to the request. 

Everything upto now will work so if you just run the application you can see the login screen and if you try to login using username and password described in the security config file you will be able to successfully login.