Spring Security Xml

Spring Security XML Configuration

The Spring Security we can be implemented by two way :

  1. Xml Based
  2. Java Based

Lets see the example of Spring Security with xml based configuration in Spring Project :


Create a Dynamic Project in eclipse .


After selected click on next button then enter the name of your application or project like below :


Then click on finish button. After finish right click on your project and convert to Maven Project like below :


After doing this if you want to change you can change group id or artifact id but in my case I don’t want to change so I use same as below like this :

And then click on finish button.

Now we have to add some dependency in pom.xml file as below :

After adding above dependency save it and make sure you are connected with a stable internet connection.

Now the final project structure is just like below :
Create a web.xml under the WEB-INF folder and below code :

As standard, we declare a Spring dispatcher servlet that handles all URLs coming to the application, and a Spring Web Context Loader Listener to loads Spring security configuration (in a Spring security configuration file named hello-security.xml file under /WEB-INF folder).

The DelegatingFilterProxy is a servlet filter that allows passing control to Filter classes that have access to the Spring application context. Spring Security relies on this technique heavily.

In web.xml we need to configure a filter named as DelegatingFilterProxy which looks for a Spring bean by the name of filter (for eg. springSecurityFilterChain) and delegates all works to that bean.

Create a file under the WEB-INF folder and name should be in my case hello-servlet.xml

As you are familiar with Spring MVC, the element tells Spring to analyze annotations for loading configurations and controllers.

The element specifies which Java package to search for Spring components.

And an InternalResourceViewResolverbean is declared to tell Spring how to resolve logical view names to physical view pages.

Create a file under the WEB-INF folder and name should be like in my case hello-security.xml

In spring-security-core:5.0.0.RC1, the default PasswordEncoder is built as a DelegatingPasswordEncoder. When you store the users in memory, you are providing the passwords in plain text and when trying to retrieve the encoder from the DelegatingPasswordEncoder to validate the password it can't find one that matches the way in which these passwords were stored. You can also simply prefix {noop} to your passwords in order for the DelegatingPasswordEncoder use the NoOpPasswordEncoder to validate these passwords. Notice that NoOpPasswordEncoder is deprecated though, as it is not a good practice to store passwords in plain text. Here, there are two elements are used for authentication and authorization:

In the element, we declare which URL pattern will be intercepted by Spring security filter, using the element. As per this configuration, all the URL patterns /admin are secured, and only the users having role ROLE_ADMIN can be authorized to access these URLs. In this you can also add multiple urls.

The element declares a user with username, password and role (ROLE_ADMIN per this configuration). This user can be authenticated to access the application.

When we use element, Spring Security creates FilterChainProxy bean with bean name springSecurityFilterChain. The configuration within element is used to build a filter chain within FilterChainProxy. We can use more elements to add extra filter chains. All the filters which require a reference to AuthenticationManager will be automatically injected. Each namespace block creates a SecurityContextPersistenceFilter, an ExceptionTranslationFilter and a FilterSecurityInterceptor and they can not be replaced with alternatives.

Create a controller in controller package and name is MyController.java
package com.deepsingh44.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

@GetMapping("/")
public String welcome() {
return "welcome";
}

@GetMapping("/admin")
public ModelAndView home() {
ModelAndView model = new ModelAndView("home");
model.addObject("message", "Spring Security.");
return model;
}

@GetMapping("/error")
public String error() {
return "error";
}

}
Create a welcome.jsp file under the WEB-INF/views folder.
Create a error.jsp file under the WEB-INF/views folder
Create home.jsp under the WEB-INF/views folder.

The getUserPrincipal() method returns an object of some class derived from the Principal interface, which is an abstraction of the entity that is the "user" responsible for the request. From it you get an actual object that, depending on the implementing class, you can use to get all sorts of information about that user/identity. One of those properties is the string-representation of the name of the user/identity, which you obtain by calling getName().

getRemoteUser() is really just a shortcut to getting that string-representation. You don't have access to any other methods implemented by the implementing class, not do you have access to the object itself, just the string-representation of the name.

Output is :

No comments: