Book Image

SPRING COOKBOOK

By : Jerome Jaglale, Yilmaz
Book Image

SPRING COOKBOOK

By: Jerome Jaglale, Yilmaz

Overview of this book

This book is for you if you have some experience with Java and web development (not necessarily in Java) and want to become proficient quickly with Spring.
Table of Contents (14 chapters)
13
Index

Authenticating users using a custom login page


In this recipe, you'll learn how to build your own login form instead of using Spring's default login form.

How to do it…

Here are the steps to define a custom login page:

  1. Make sure that the JSTL Maven dependency is declared in pom.xml:

    <dependency> 
        <groupId>javax.servlet</groupId> 
        <artifactId>jstl</artifactId> 
        <version>1.2</version> 
    </dependency> 
  2. Make sure that a JSP view resolver is declared in the AppConfig class:

    @Bean 
    public ViewResolver jspViewResolver(){ 
        InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
        resolver.setViewClass(JstlView.class); 
        resolver.setPrefix("/WEB-INF/jsp/"); 
        resolver.setSuffix(".jsp"); 
        return resolver; 
    } 
  3. Add a controller method for the login page in a controller class:

    @Controller
    public class UserController {
    
      @RequestMapping("login") 
      public String login() { 
          return "login"; 
      }
    … 
  4. Add a JSP for...