Develop login page simply with Spring security (part 1)

Wednesday, December 2nd, 2015 (2 Comments)

We develop a page login with spring mvc example and connect to mysql database to check username and password. In this article is include 2 part. The first is login basic, not use database. The second is login use database.

Step 1: create a login with spring mvc example project as following:

You should create project the same Helloworld article and practice step by step to clearly and be perfect in Spring MVC.

login project's struct

login project’s struct

Step 2: modify some config files

porn.xml: we add full libraries of necessaries of project and I commended detail for each library.

servlet-context.xml: modify the same file’s configuration of helloworld project

context-security.xml: modify to permit privileges before and after login

  • line 20: permit access to home page if logged in
  • line 21: permit access to user page if logged in and have privileges ROLE_ADMIN
  • line 22: permit all access to login
  • line 23: if have not privileges to access page, will redirect to denied page
  • line 25: default after login OK is redirect to home page
  • line 28: after logout is redirect to root or index page
  • line 31: delegated to CustomAuthenticationProvider class to handle or custom login function.

web.xml: edit and load some configuration files

Step 3: modify java files:

UserInfo.java: information of user

LoginController.java:

  • line 15: add isAuthenticated() function, to check logged or not. If logged and type url with login page, return home page.

UserInfoController.java: to manage user’s page

CustomAuthenticationProvider.java: custom login function

  • line 42: hardcode username=admin and password=123 to check login function
  • line 48: hardcode role=ROLE_ADMIN to permit access to user page as config file context-security.xml
  • hardcode username, password and role, you can get from database.

Step 4: Edit layout for pages

login.jsp:

home.jsp: after logged in, redirect to this page

user.jsp: after logged in and have role ROLE_ADMIN, permit access this page

denied.jsp: redirect to this page when not permit access to some page

index.jsp: redirect to login page

Finally, build and run. In part 2, We will merge “Spring connect to Mysql Database” article into login project to check login with database.

Tags: , , , ,

Related Posts

spring
How to create quartz spring dynamic in database
spring
Spring mvc mongodb crud users roles
spring
Java spring mvc tutorial on lvtutorial.com
spring
Angularjs spring mvc crud example

Related Posts

spring
How to create quartz spring dynamic in database
spring
Spring mvc mongodb crud users roles
spring
Java spring mvc tutorial on lvtutorial.com
spring
Angularjs spring mvc crud example

2 Comments

  1. Richard2364

    I am really impressed with your writing abilities as smartly as with the format in your weblog. Is that this a paid subject or did you modify it your self? Either way keep up the excellent high quality writing, it’s rare to peer a great weblog like this one nowadays..

    1. Vivian Post author

      Thanks for your feedback, it is going to help my weblog is improved and focused on practice programming for beginner!

Leave a Reply

Your email address will not be published. Required fields are marked *