add security

This commit is contained in:
Mohd-Arham
2025-11-26 18:01:36 +05:30
parent 985f6eecfd
commit 2991349f8b
19 changed files with 542 additions and 30 deletions
@@ -3,6 +3,7 @@ package com.billpaymentservice.bbps.serviceImpl;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -31,10 +32,12 @@ import com.billpaymentservice.bbps.entites.BharatConnectMaster;
import com.billpaymentservice.bbps.entites.ComplaintStatusOptionsMaster;
import com.billpaymentservice.bbps.entites.InboundTraceLog;
import com.billpaymentservice.bbps.entites.OutboundTraceLog;
import com.billpaymentservice.bbps.entites.TransactionMaster;
import com.billpaymentservice.bbps.exception.CustomException;
import com.billpaymentservice.bbps.repository.BharatConnectMasterRepository;
import com.billpaymentservice.bbps.repository.ComplainRepository;
import com.billpaymentservice.bbps.repository.ComplaintOptionsStatusMasterRepository;
import com.billpaymentservice.bbps.repository.TransactionMasterRepository;
import com.billpaymentservice.bbps.service.ComplainService;
import com.billpaymentservice.bbps.util.BBPSConstant;
import com.billpaymentservice.bbps.util.BillerMapperHelper;
@@ -61,6 +64,7 @@ public class ComplainServiceImpl implements ComplainService {
private final BillerMapperHelper mapperHelper;
private final ComplainRepository complainRepository;
private final ComplaintOptionsStatusMasterRepository complaintStatusMasterRepository;
private final TransactionMasterRepository transactionMasterRepository;
// moved to class-level
private final ObjectMapper objectMapper;
@@ -69,7 +73,8 @@ public class ComplainServiceImpl implements ComplainService {
public ComplainServiceImpl(RestClient restClient, HttpService httpService, Helper helper,
BharatConnectMasterRepository beneficiaryDetailsRepository, BillerMapperHelper mapperHelper,
ComplainRepository complainRepository,
ComplaintOptionsStatusMasterRepository complaintStatusMasterRepository) {
ComplaintOptionsStatusMasterRepository complaintStatusMasterRepository,
TransactionMasterRepository transactionMasterRepository) {
this.restClient = restClient;
this.httpService = httpService;
this.helper = helper;
@@ -77,7 +82,7 @@ public class ComplainServiceImpl implements ComplainService {
this.mapperHelper = mapperHelper;
this.complainRepository = complainRepository;
this.complaintStatusMasterRepository = complaintStatusMasterRepository;
this.transactionMasterRepository = transactionMasterRepository;
// initialize shared ObjectMapper once
this.objectMapper = new ObjectMapper();
this.objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
@@ -110,10 +115,20 @@ public class ComplainServiceImpl implements ComplainService {
BBPSConstant.HTTP_POST, null);
try {
Optional<TransactionMaster> dataById = transactionMasterRepository.findById(complainRequestDTO.getTxnId());
if (dataById == null || dataById.isEmpty()) {
LOGGER.warn("Master Data By Id is null");
throw new CustomException("Master Data is required", "FAILED", BBPSConstant.BADREQUEST, null);
}
TransactionMaster transData = dataById.get();
// Inject tenantId + agentId into request JSON
ObjectNode root = (ObjectNode) objectMapper.readTree(forwardJson);
root.put("tenantId", headersData.getTenantId());
root.put("agentId", headersData.getAgentId());
root.put("billerId", transData.getBillerId());
root.put("billerName", transData.getBillerName());
String finalJson = root.toString();
LOGGER.info("[{}] Final Request Sent to API | reqId={} | payload={}", apiName, requestId, finalJson);
@@ -2,28 +2,35 @@ package com.billpaymentservice.bbps.serviceImpl;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import com.billpaymentservice.bbps.config.JwtUtil;
import com.billpaymentservice.bbps.dto.CustomParamDTO;
import com.billpaymentservice.bbps.dto.CustomerParamGroupsDTO;
import com.billpaymentservice.bbps.dto.GeneralResp;
import com.billpaymentservice.bbps.dto.MasterApiResponse;
import com.billpaymentservice.bbps.dto.ResponseModel;
import com.billpaymentservice.bbps.dto.UserDetailsDTO;
import com.billpaymentservice.bbps.dto.request.BillerDetailRequest;
import com.billpaymentservice.bbps.dto.request.ContactMasterRequest;
import com.billpaymentservice.bbps.dto.request.CustomParamRequest;
import com.billpaymentservice.bbps.dto.request.DispositionRequest;
import com.billpaymentservice.bbps.dto.request.LoginRequest;
import com.billpaymentservice.bbps.dto.request.MasterCategoryRequest;
import com.billpaymentservice.bbps.dto.request.MasterRequest;
import com.billpaymentservice.bbps.dto.request.PlanRequest;
import com.billpaymentservice.bbps.dto.response.AmountOptionsResponse;
import com.billpaymentservice.bbps.dto.response.AuthResponse;
import com.billpaymentservice.bbps.dto.response.BillerDataResponse;
import com.billpaymentservice.bbps.dto.response.BillerResponse;
import com.billpaymentservice.bbps.dto.response.ChannelsListResponse;
@@ -42,6 +49,7 @@ import com.billpaymentservice.bbps.entites.DispositionDetails;
import com.billpaymentservice.bbps.entites.InboundTraceLog;
import com.billpaymentservice.bbps.entites.OutboundTraceLog;
import com.billpaymentservice.bbps.entites.PlanResponses;
import com.billpaymentservice.bbps.entites.UsersDetails;
import com.billpaymentservice.bbps.exception.CustomException;
import com.billpaymentservice.bbps.repository.BharatConnectMasterRepository;
import com.billpaymentservice.bbps.repository.BillerDataRepository;
@@ -50,6 +58,7 @@ import com.billpaymentservice.bbps.repository.DispositionRepository;
import com.billpaymentservice.bbps.repository.InboundTraceLogRepository;
import com.billpaymentservice.bbps.repository.MerchantRepository;
import com.billpaymentservice.bbps.repository.OutboundTraceLogRepository;
import com.billpaymentservice.bbps.repository.UsersDetailsRepository;
import com.billpaymentservice.bbps.service.MasterDataService;
import com.billpaymentservice.bbps.util.BBPSConstant;
import com.billpaymentservice.bbps.util.BillerMapperHelper;
@@ -80,6 +89,11 @@ public class MasterDataServiceImpl implements MasterDataService {
private final InboundTraceLogRepository inboundTraceLogRepository;
private final OutboundTraceLogRepository outboundTraceLogRepository;
private final CustomParamRepository customParamReposiotory;
@Autowired
private UsersDetailsRepository userRepo;
@Autowired
private JwtUtil jwtUtil;
public MasterDataServiceImpl(MerchantRepository merchantRegisterRepo, HttpService httpService,
HttpClientApi httpClientApi, Helper helper, BillerDataRepository billerDataRespository,
@@ -224,7 +238,7 @@ public class MasterDataServiceImpl implements MasterDataService {
throw new CustomException("Invalid JSON received from API");
}
}
@Override
public ResponseModel getCustomerParamByBillerId(CustomParamRequest customerParamRequest) {
String apiName = BBPSConstant.API_GET_CUSTOMER_PARAM_BY_BILLER_ID;
@@ -366,7 +380,7 @@ public class MasterDataServiceImpl implements MasterDataService {
LOGGER.info("[{}] START | reqId={} | mid={} | billerName={} | category={}", apiName, requestId, mid,
masterRequest.getBillerName(), masterRequest.getCategory());
String txnID = helper.generateTxnId(15);
String txnID = helper.generateTxnId(15);
String fullUrl = String.format("%s%s?billerName=%s&category=%s&page=%d&pagesize=%d", masterRegisterUrl,
BBPSConstant.BILLER_URL, masterRequest.getBillerName(), masterRequest.getCategory(),
masterRequest.getPageNo(), masterRequest.getPageSize());
@@ -472,7 +486,7 @@ public class MasterDataServiceImpl implements MasterDataService {
if (entities != null) {
dispositionRepository.saveAll(entities);
} else {
LOGGER.warn("No disposition found in response to save.");
}
@@ -868,4 +882,40 @@ public class MasterDataServiceImpl implements MasterDataService {
}
}
@Override
public AuthResponse authenticateUser(LoginRequest loginRequest) {
LOGGER.info("Processing login for Request: " + loginRequest);
try {
String mobileNo = loginRequest.getMobileNumber();
Optional<UsersDetails> byAdminEmail = userRepo.findByUserMobNo(mobileNo);
if (byAdminEmail.isPresent()) {
UsersDetails user = byAdminEmail.get();
String token = jwtUtil.generateToken(user.getUserMobNo());
LOGGER.info("Generated JWT token for mobileNo: " + mobileNo);
Object userDetailsDTO = null;
if (user != null) {
userDetailsDTO = UserDetailsDTO.builder().id(user.getId()).userEmailId(user.getUserEmailId())
.userMobNo(user.getUserMobNo()).userName(user.getUserName()).userAge(user.getUserAge())
.build();
}
return new AuthResponse(token, "Success", "Login Done", 200, userDetailsDTO);
} else {
return new AuthResponse("", "User Not Found", "Authentication failed", 401, null);
}
} catch (Exception ex) {
System.out.println("Error occurred during login for mobile no: " + loginRequest.getMobileNumber()
+ " Exception: " + ex.getMessage());
return AuthResponse.builder().status("Unauthorized").message("Authentication failed: " + ex.getMessage())
.statusCode(401).build();
}
}
}