Review
In the previous section, we have laid down the functional specs of the application. In this section, we will discuss the project's structure, write the Java classes, and organize them in layers.Table of Contents
Part 1: Introduction and Functional SpecsPart 2: Java classes
Part 3: XML configuration
Part 4: HTML Files
Part 5: Running the Application
Project Structure
Our application is a Maven project and therefore follows Maven structure. As we create the classes, we've organized them in logical layers: domain, repository, service, and controller.Here's a preview of our project's structure:
The Layers
Domain Layer
This layer contains two domain classes, User and Role. They represent our database tables, user and role respectively. Because we're developing a JPA-based repository, both classes must be annotated with JPA annotations.Specifying the entity name inside the @Entity is not required. However, I've encountered instances where the tables are created in lowercase and uppercase, i.e user and USER. For consistency, I've specified them here.
Controller Layer
This layer contains two controllers, AccessController and MediatorController.- AccessController is responsible for handling access related requests, mainly login and logout requests
- MediatorController is responsible for handling requests to common pages such as user and admin pages
Service Layer
This layer contains a single service, CustomUserDetailsService. Its main purpose is to retrieve user information from our custom database and translate that user information into a format that Spring Security understands. Take note of the helper methods provided within this class.What's the logic here?
- We must implement UserDetailsService because we have a custom database
- loadUserByUsername method must return an object that implements the UserDetails interface
- We must map our domain org.krams.domain.User to org.springframework.security.core.userdetails.User
- We must map numerical roles as SimpleGrantedAuthority objects
- We are responsible for interpreting what each numerical role value represents
- ROLE_USER and ROLE_ADMIN are abitrary values we assigned to numerical values
Repository Layer
This layer contains a single interface, UserRepository. And this is our data access object (DAO). With the help of Spring Data JPA, Spring will automatically provide the actual implementation.What is Spring Data JPA?
Spring JPA is part of the umbrella Spring Data project that makes it easy to easily implement JPA based repositories.
Implementing a data access layer of an application has been cumbersome for quite a while. Too much boilerplate code has to be written to execute simple queries as well as perform pagination, and auditing. Spring JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that's actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.
Source: http://www.springsource.org/spring-data/jpa
Utility classes
TraceInterceptor class is an AOP-based utility class to help us debug our application. This is a subclass of CustomizableTraceInterceptor (see Spring Data JPA FAQ)Next
In the next section, we will focus on the configuration files and create them accordingly. Click here to proceed.
Share the joy:
|
Subscribe by reader Subscribe by email Share
grate.....tutorial
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteGreat tutorial, but I need an explanation.
ReplyDeleteHow can you return a new User in loadUserByUsername if your User class doesn't implement UserDetails class?
@Nenaza, you cannot return a new User if it doesn't implement the UserDetails interface. However, if you examine carefully the CustomUserDetailsService class, we're able to return a new User. That's because it's implementing the UserDetails interface. And the user we're referring to is from the package org.springframework.security.core.userdetails.User and we're mapping from org.krams.domain.User. In other words, there are two User classes in this tutorial. See http://static.springsource.org/spring-security/site/docs/3.1.x/apidocs/index.html for details
ReplyDeleteHi Krams when i run this example project i am getting this exceptions please help me
ReplyDelete1)javax.persistence.PersistenceException: [PersistenceUnit: hibernatePersistenceUnit] Unable to build EntityManagerFactory
2)org.hibernate.MappingException: Could not determine type for: com.manam.domain.Role, at table: user, for columns: [org.hibernate.mapping.Column(role)]
/* Excuse me for my english */
ReplyDeleteFirst, I thank Mr. Krams for this very interesting tutorial. and i wonder if
someone can help me by posting an updated pom.xml for this project, in fact there is some problems in the "goldin" dependency.
thank you
Note: the probem remains even if we update the pom.xml as it indicated by Mr.Krams somewhere here in a comment
HI Krams
ReplyDeletePlease Explain the spring AOP . i did not understand this concept. why we using Traceinterceptor.java file
@Dummy, this is use for debugging purposes. So that we can lessen the need of manually adding trace statements in each method just to see what's coming in and what's coming out. The value is easy troubleshooting.
DeleteHi Krams,
ReplyDeleteFirst of all i would like to thank you for this wonderfull tutorial...
I changed the spring JPA to hibernate sessionFactory and when i run the application the CustonmUserDetailsService loadUserByUsername method is not getting called
The best way to troubleshoot this problem is to enable DEBUG-level logging.
DeleteHello Krams,
ReplyDeletethank you for this great tutorial.
I was looking about how to integrate Spring Security and JPA and probably your posts will help me a lot.
The point is that I have already developed in my Spring MVC my DAO Layer.
I have not used @Repository annotations yet and either Spring Data JPA.
Is that possible to use simple JPA layer as I have developed with the architecture you have suggested? I mean, what is the benefit of using Spring Data JPA over traditional JPA?
Is the configuration going to change a lot using JPA only?
This is how my JPA works:
public class UsuarioJPADAO implements UsuarioDAO{
private EntityManager entityManager;
@Override
public void setEntityManager(EntityManager em) {
this.entityManager = em;
}
@Override
public Collection findByName(String name) throws DAOException {
Query query = entityManager.createNamedQuery("Usuario.findByName");
query.setParameter("name", name);
return query.getResultList();
}
Thank you and regards.
Thanks for amazing Spring security series, all of them
ReplyDeleteIncredibly well written articles. Much better than tutorial provided by Spring
Thanks for the tutorial. I am having an compilation error with loadUserByUsername returning UserDetails. Any suggestions.
ReplyDeleteMy issue turned out to be POM related. I needed all to surrounded by and that fixed my Lifecycle issues. SpringData then was able to populate the loadUserByUsername return UserDetails.
ReplyDeleteall plugin elements must be in a pluginManagement, plugins elements in the POM. The comments don't accept < and >
ReplyDeletehello; i dont understand in the getRoles method in CusomUserDetailsService, the integer represents what? can you explain
ReplyDeletenice tutorial..thanks
ReplyDeleteHi Krams,
ReplyDeletewe have already implement all layers. And in DB side , we dnt have Role table .
So is it mandatory to have that table for implementing spring security ?
Thanks for sharing this wonderful information. I hope you will share more helpful information regarding the content.
ReplyDeleteweb portal development company in chennai
sem services in chennai
professional web design company in chennai
Mobile Phone Repairing Services in Bangalore
ReplyDeletevivo service center in Bangalore.
Oneplus service center in Bangalore.
Moto service center in Bangalore.
Honor service center in Bangalore.
Oppo service center in Bangalore.
mi service center in Bangalore
Apple service center in Bangalore
Pest control services Chennai is a dedicated service solution to all your pest and insect troubles in Chennai.
ReplyDeletePest control services in chennai.
Pest control in chennai.
Pest control services near me.
Realme Mobile Service center in Chennai
ReplyDeleteRealme Servicing by No. 1 Leaders Awesome Services On-time, Every time!
Realme Mobile Service Center in Chennai
iPhone Service center in Bangalore No. 1 Leaders Awesome Services On-time, Every time!
ReplyDeleteiPhone Service Center in Bangalore
Apple Service center in Bangalore
Apple Service Center in Bangalore
Mi Service Center in Chennai
ReplyDeleteMi Service Center in Chennai No. 1 Leaders Awesome Services On-time, Every time!
Mi Service Center in Chennai
Redmi Service Center in Chennai
Mi Service Center in Chennai
This comment has been removed by the author.
ReplyDeleteMoto service center in Chennai
ReplyDeleteLeading Motorola Service Center In Chennai. Fast & Reliable Services On-time, Every time!
Moto service center in Chennai
Motorola Service Center In Chennai
Motorola Service Center In Chennai
LG Mobile Service Center In Chennai
ReplyDeleteLG Mobile Service Center In Chennai. Providing Fast, Honest, Reliable & Cost Effective Service Solutions to The LG Mobile Users.
LG Mobile Service Center In Chennai
Sony Mobile Service Center In Chennai
ReplyDeleteSony Servicing by No. 1 Leaders Awesome Services On-time, Every time!
Sony Mobile Service Center In Chennai
Nokia Mobile Service Center In Chennai
ReplyDeleteNokia Servicing by No. 1 Leaders Awesome Services On-time, Every time!
Nokia Mobile Service Center In Chennai
Ipad service center in Chennai
ReplyDeleteIpad service Center in Chennai offers you best quality of Services On-time, Every time!
Ipad Service Center in Chennai
Apple Imacbook Service Center in Chennai
ReplyDeleteApple Laptop Service Center in Chennai offers you best quality of Services On-time, Every time!
Apple Laptop Service Center in Chennai
Acer Service Center in Chennai
ReplyDeleteAcer Service Center in Chennai Best service for all model acer laptop and desktop Services On-time, Every time!
Acer Laptop Service Center in Chennai
Acer Service Center in Chennai
Acer Service Center in Chennai
Lenovo Service Center in Chennai
ReplyDeleteLenovo Service Center Chennai
Lenovo Service Center Chennai
Lenovo service center near me in Chennai
Lenovo service center near me in Chennai
Awesome Post!!!
ReplyDeleteMI service center in Chennai
Redmi service center in Chennai No. 1 Leaders Awesome Services On-time, Every time!
Redmi Service Center in chennai
Apple Service center in Bangalore
Mi Service Center in Chennai
WOW Great Post!!!
ReplyDeleteNokia Service Center In Chennai
Nokia service center in Chennai offers you a secure and trusted service for all types of mobiles
Nokia Service Center In Chennai
Apple Iphone Service Center in chennai
ReplyDeleteApple Iphone screen replacement in chennai is possible by contacting the industry experts.
Apple Screen Replacement in Chennai
Macbook Service Center in Chennai
apple authorised service centre in chennai
Mi Service Center in Chennai
ReplyDeleteAre you experiencing issues with your MI smart phone and looking for Mi Service center? Xiaomi is the umbrella name, under which Redmi has been manufactured as a brand.
Mi Service Center in Chennaii
Redmi Service Center in Chennai
Redmi Service Center in Chennai
Oneplus Service Center in Chennai
ReplyDeleteOnePlus service center chennai is the best and cheap place to fix all your OnePlus mobile issues.Oneplus Repair Center in Chennai- All our OnePlus service center technicians having more than 7 years experience in the mobile care industry. Our experts assure you impeccable, unsurpassed quality in service with our handpicked team of Service Experts and Analysts who carefully assess your products and suggest appropriate solution.
Oneplus Service Center in Chennai
Oneplus Service Center Chennai
oneplus authorised service center in chennai
Honour Mobile Service Center in Chennai
ReplyDeleteHonor Service Center in Chennai for the better service of your Honor mobile phone.
Honor Repair Service Center in Chennai- Visit to our best Honor Mobile Service center In Chennai to solve your phone problems.
honor service centre near me
Realme Service Center in Chennai
ReplyDeleteWe are the best realme service center in Chennai, we provide services with professionally trained realme mobile workers
Realme Authorised Service Center in Chennai
Asus Service Center in Chennai
ReplyDeleteAsus is one of the dominant and leading players in the gadget industry. Be it smartphone, pad or laptop, Asus products are highly preferred by most of the customers.
Asus Mobile Service Center in Chennai
Lg Mobile Service Center in Chennai
ReplyDeleteLg Mobile Repair Service Center in Chennai. For the finest service and fastest delivery for your LG mobile, visit us! Go home a happy customer! Connect with us!!
lg mobile authorised service center in chennai
Oneplus Service Center in Bangalore
ReplyDeleteOneplus Repair Service Center in Bangalore. For the finest service and fastest delivery for your Oneplus mobile, visit us! Go home a happy customer! Connect with us!!
Oneplus Mobile Service Center in Bangalore
This is really nice. Thanks for sharing this informative article.
ReplyDeleteOne Plus Display Replacement Service in Chennai