具有身份验证的Spring Boot项目结构 [英] Spring Boot project structure with Authentication

查看:133
本文介绍了具有身份验证的Spring Boot项目结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Spring启动及其模块,但我希望避免学习不良实践。那么你能告诉我通常使用什么项目结构吗?

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?

我正在使用这种结构:

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

现在我需要实现JWT身份验证,所以我有新的类:

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


  • 安全过滤器链的CustomAuthFilter.java

  • CustomUserDetailsS​​ervice AuthenticationManager的.java

  • 处理异常的CustomEntryPoint.java

  • 用于管理jwt令牌的CustomJwtService.java

  • CustomAuthController.java用于其他端点,如/ login,/ logout,/ create-user,/ reset-password

  • 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

你能告诉我在哪里吗?存储这个类?我有2个想法:

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


  1. 创建安全文件夹并将其放在一起

  2. 将文件拆分为现有文件夹:CustomAuthFilter.java - > config,CustomUserDetailsS​​ervice.java - > service,CustomEntryPoint.java - > config,CustomJwtService.java - > service,CustomAuthController.java - > web

  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.

推荐答案

这是代码组织问题,与spring boot无关。

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

这篇关于具有身份验证的Spring Boot项目结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆