Step 1 - Creating a basic Spring MVC application

Let's demonstrate the configuring basic security in the application. We will use the Spring Security 5.0.0.M2, which is the latest version, and follow the demo step by step, discussed as follows:

  1. Create Ch08_Security_Getting_Started as a dynamic web application.
  2. Copy JARs for spring-core, spring-context, spring-security-core-5.0.0.M2, and spring-security-config-5.0.0.M2 in the lib folder, as shown in the project outline.
  3. Copy the images folder from the earlier Spring MVC application. It's just for presentation purpose, so whether to add it or not, the choice is yours.
  4. Add the Front Controller mapping in DD, as we did in all Spring MVC applications. Give the name of the controller as security.
  5. Create security-servlet.xml to add mapping information about controllers and ViewResovler, as we did in earlier Spring MVC applications.
  6. Create the index page, as shown here, in the WebContent folder to provide two links:
        <a href="welcome.htm">CLICK HERE TO SEE THE WELCOME MESSAGE</a>
<br> <a href="data.htm">CLICK HERE TO READ THE DATA</a>
  1. Now let's add DataController as a controller class in the com.packt.ch08.controllers package whose methods will be invoked upon clicking the links from the index page. You can refer to the following piece of code:
        @Controller 
        public class DataController { 
              @RequestMapping("/welcome.htm") 
          public ModelAndView welcome() 
          { 
            return new ModelAndView("home","message","Welcome to the
Spring security application"); } @RequestMapping("/data.htm") public ModelAndView show() { return new ModelAndView("data","message",
"CLICK THE LINKS TO READ THE data !!!!"); } }
  1. Add the pages home.jsp and data.jsp in the /WEB-INF/jsps/ folder.
  2. We will add the following code in both home.jsp as well as data.jsp:
        <body> 
        <table align="center"> 
          <tr> 
            <td><img alt="No Image Found" src="/images/image.png"
width="100px" height="100px"></td> </tr> </table> <h1> ${message }</h1> </body>

To keep it simple, we added the same code in both the pages; however, in real-time applications, it can be different.

  1. Run the application. Click on the links one by one to make sure the base application is working correctly.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.141.6