具有身份验证的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
- CustomUserDetailsService 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:
- 创建安全文件夹并将其放在一起
- 将文件拆分为现有文件夹:CustomAuthFilter.java - > config,CustomUserDetailsService.java - > service,CustomEntryPoint.java - > config,CustomJwtService.java - > service,CustomAuthController.java - > web
- Create security folder and put it here together
- 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