Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot project structure with Authentication

I am learning Spring boot and its modules, but I want avoid to learning bad practices. So can you tell me what project structure is usually using for that?

I am using this structure:

com
 +- example
     +- myproject
         +- Application.java
         |
         +- config
         |   +- CustomSecurityConfig.java (extends WebSecurityConfigurerAdapter)
         |
         +- domain
         |   +- Customer.java
         |
         +- repository
         |   +- CustomerRepository.java
         |    
         +- service
         |   +- CustomerService.java
         |
         +- web
             +- CustomerController.java

Now I need implement JWT Authentication, so I have theese new classes:

  • CustomAuthFilter.java for Security Filter Chain
  • CustomUserDetailsService.java for AuthenticationManager
  • CustomEntryPoint.java for handle exceptions
  • CustomJwtService.java for managing jwt tokens
  • CustomAuthController.java for rest endpoints like /login, /logout, /create-user, /reset-password

Can you tell me where store this classes? I have 2 ideas:

  1. Create security folder and put it here together
  2. Split files into existing folders like that: CustomAuthFilter.java -> config, CustomUserDetailsService.java -> service, CustomEntryPoint.java -> config, CustomJwtService.java -> service, CustomAuthController.java -> web

Can yo give me some advice pls? Thank.

like image 623
Denis Stephanov Avatar asked Nov 17 '22 21:11

Denis Stephanov


1 Answers

This is code organization problem and independent of spring boot.

Go by business sense/ functionality of the module than the technical sense of the module. Functionality sense helps to understand the code easily if you are looking after a while.

+Application.java
+security/ 
+-CustomAuth
+-CustomJwtSevice

if functionality is scatters in multiple files then keep them in subfolder like 'security' in above.

+Application.java
+security/
+-auth/
+--token/
+---JWTtokenGenerator.java
+---JWTutil.java
+--configuration/
+---SecurityConfig
+---...
+--customer/
+---customerservice.java
+---customerrepo.java

The above pattern works by dividing a module, into sub-module and sub-module to their sub-modules.. so on

like image 98
The Mighty Programmer Avatar answered Dec 10 '22 02:12

The Mighty Programmer