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:
- Create Ch08_Security_Getting_Started as a dynamic web application.
- 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.
- 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.
- Add the Front Controller mapping in DD, as we did in all Spring MVC applications. Give the name of the controller as security.
- Create security-servlet.xml to add mapping information about controllers and ViewResovler, as we did in earlier Spring MVC applications.
- 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>
- 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 !!!!"); } }
- Add the pages home.jsp and data.jsp in the /WEB-INF/jsps/ folder.
- 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.
- Run the application. Click on the links one by one to make sure the base application is working correctly.