Spring boot version 3
Spring boot version 3
PROJECT FLOWCHART--
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Table(name = "user")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
b.Controller(AuthService)
package com.authentication.Authentication_service.Controller;
import com.authentication.Authentication_service.Entities.User;
import com.authentication.Authentication_service.Services.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/auth")
public class AuthController {
@Autowired
private AuthService authService;
@PostMapping("/register")
public ResponseEntity<String> register(@RequestBody User user) {
return authService.registerUser(user);
}
@PostMapping("/login")
public ResponseEntity<String> login(@RequestBody User user) {
return authService.loginUser(user);
}
}
c.Repositories(UserRepository)
package com.authentication.Authentication_service.Repositories;
import com.authentication.Authentication_service.Entities.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
d.Security(JwtRequestFilter,C,SecurityConfig)
JwtRequestFilter
package com.authentication.Authentication_service.Security;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import
org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
@Component
public class JwtRequestFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
response, FilterChain chain)
throws ServletException, IOException {
if (jwtUtil.validateToken(jwt, userDetails)) {
var authenticationToken = jwtUtil.getAuthenticationToken(jwt,
userDetails);
authenticationToken.setDetails(new
WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
}
chain.doFilter(request, response);
}
}
JwtUtil
package com.authentication.Authentication_service.Security;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class JwtUtil {
return claims.getSubject();
}
SecurityConfig
package com.authentication.Authentication_service.Security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import
org.springframework.security.config.annotation.authentication.configuration.Authenti
cationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import
org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
;
@Configuration
public class SecurityConfig {
@Autowired
private JwtRequestFilter jwtRequestFilter;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws
Exception {
http.csrf(csrf -> csrf.disable()) // Disable CSRF protection
.authorizeHttpRequests(auth -> auth
.requestMatchers("/auth/login",
"/auth/register").permitAll() // Public endpoints
.anyRequest().authenticated() // All other endpoints are
secured
)
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS) //
Set session management to stateless
);
return http.build();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration
authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
}
e.Services(AuthService,CustomUserDetailService)
AuthService
package com.authentication.Authentication_service.Services;
import com.authentication.Authentication_service.Entities.User;
import com.authentication.Authentication_service.Repositories.UserRepository;
import com.authentication.Authentication_service.Security.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@Service
public class AuthService {
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private JwtUtil jwtUtil;
CustomUserDetailService
package com.authentication.Authentication_service.Services;
import com.authentication.Authentication_service.Entities.User;
import com.authentication.Authentication_service.Repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Service
public class CustomUserDetailsService implements UserDetailsService {
@Autowired
public CustomUserDetailsService(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public UserDetails loadUserByUsername(String username) throws
UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found with username: " +
username);
}
return
org.springframework.security.core.userdetails.User.withUsername(user.getUsername())
.password(user.getPassword())
.authorities("USER") // Adjust authorities/roles as needed
.build();
}
}
f.Application.properties.
spring.application.name=Authentication-service
# Server configuration
server.port=8081
# Database configuration
spring.datasource.url=jdbc:mysql://localhost:3306/banking
spring.datasource.username=root
spring.datasource.password=Bhavesh@5
g.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.authentication</groupId>
<artifactId>Authentication-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Authentication-service</name>
<description>This is Authentication service</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!-- Spring Boot Starter for Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter for JPA (for database interactions) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Spring Boot Starter Security for authentication and authorization -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.Core-banking-service
a.Entities-
i-Account.
package com.core_banking_service.Entities;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Table(name = "Accounts")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String accountHolderName;
private Double balance;
ii.Transaction
package com.core_banking_service.Entities;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.Date;
@Entity
@Table(name = "Transactions")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long fromAccountId;
private Long toAccountId;
private Double amount;
private Date transactionDate;
b.Controllers
i.AccountController
package com.core_banking_service.Controllers;
import com.core_banking_service.Dtos.AccountResponse;
import com.core_banking_service.Entities.Account;
import com.core_banking_service.Services.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/accounts")
public class AccountController {
@Autowired
private AccountService accountService;
@PostMapping("/create")
public ResponseEntity<AccountResponse> createAccount(@RequestBody Account
account) {
AccountResponse accountResponse = accountService.createAccount(account);
return ResponseEntity.ok(accountResponse);
}
@GetMapping("/all")
public ResponseEntity<List<AccountResponse>> getAllAccounts() {
return ResponseEntity.ok(accountService.getAllAccounts());
}
@GetMapping(value = "/accountId")
public ResponseEntity<AccountResponse> getAccountById(@PathVariable Long
accountId) {
return ResponseEntity.ok(accountService.getAccountById(accountId));
}
}
c.Dtos
i.AccountResponse
package com.core_banking_service.Dtos;
ii.TransactionRequest-
package com.core_banking_service.Dtos;
d.Repositories
i.AccountRepository
package com.core_banking_service.Repositories;
import com.core_banking_service.Entities.Account;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {
}
ii.TransactionRepository
package com.core_banking_service.Repositories;
import com.core_banking_service.Entities.Transaction;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TransactionRepository extends JpaRepository<Transaction, Long> {
}
e.Services
i.AccountService
package com.core_banking_service.Services;
import com.core_banking_service.Dtos.AccountResponse;
import com.core_banking_service.Entities.Account;
import com.core_banking_service.Repositories.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class AccountService {
@Autowired
private AccountRepository accountRepository;
ii.TransactionService
package com.core_banking_service.Services;
import com.core_banking_service.Dtos.TransactionRequest;
import com.core_banking_service.Entities.Account;
import com.core_banking_service.Entities.Transaction;
import com.core_banking_service.Repositories.AccountRepository;
import com.core_banking_service.Repositories.TransactionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class TransactionService {
@Autowired
private AccountRepository accountRepository;
@Autowired
private TransactionRepository transactionRepository;
Account toAccount =
accountRepository.findById(transactionRequest.getToAccountId())
.orElseThrow(() -> new RuntimeException("Destination account not
found"));
fromAccount.setBalance(fromAccount.getBalance() -
transactionRequest.getAmount());
toAccount.setBalance(toAccount.getBalance() +
transactionRequest.getAmount());
accountRepository.save(fromAccount);
accountRepository.save(toAccount);
transactionRepository.save(transaction);
}
}
f.application.properties.
spring.application.name=Core-banking-service
# Server configuration
server.port=8082
# Database configuration
spring.datasource.url=jdbc:mysql://localhost:3306/banking
spring.datasource.username=root
spring.datasource.password=Bhavesh@5
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.loan-service
4.notification-service