NOTE
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.
PATTERNS
- 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.
- 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.
No comments:
Post a Comment