Skip to content

Commit

Permalink
feat: add the extract business number from the image
Browse files Browse the repository at this point in the history
  • Loading branch information
JIUNG9 committed Jan 23, 2024
1 parent 63d3aca commit d2bebb0
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.web.firewall.DefaultHttpFirewall;
import org.springframework.security.web.firewall.HttpFirewall;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
Expand Down Expand Up @@ -51,7 +54,11 @@ public void addCorsMappings(CorsRegistry registry) {
.allowedHeaders("*")
.exposedHeaders("*")
.allowCredentials(false);
}

@Bean
public HttpFirewall defaultHttpFirewall() {
return new DefaultHttpFirewall();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ private boolean shouldNotFilterBySystemPolicy(HttpServletRequest request) {
return requestURI.contains(JWTAuthenticationShouldNotFilterAntMatcher.SIGNUP_ANT)
|| requestURI.contains(JWTAuthenticationShouldNotFilterAntMatcher.LOGIN_ANT)
|| requestURI.contains(JWTAuthenticationShouldNotFilterAntMatcher.EMAIL_ANT)
|| requestURI.contains(JWTAuthenticationShouldNotFilterAntMatcher.REFRESH_ANT);
|| requestURI.contains(JWTAuthenticationShouldNotFilterAntMatcher.REFRESH_ANT)
|| requestURI.contains(JWTAuthenticationShouldNotFilterAntMatcher.STORE_MANAGER_OCR);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.UnsupportedJwtException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -22,7 +21,7 @@

@RequiredArgsConstructor
@Service
public class RenewRefreshTokenWhenAccessTokenExpired implements
public class RenewRefreshInRedisTokenWhenAccessTokenExpired implements
RenewRefreshTokenStrategy<AuthId> {

private final RedisBlackListTokenUtil redisBlackListTokenUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class JWTAuthenticationShouldNotFilterAntMatcher {
public static final String EMAIL_ANT = "/emails";
public static final String SIGNUP_ANT = "/signup";
public static final String REFRESH_ANT = "/refresh";
public static final String STORE_MANAGER_OCR ="/stores/business-image-number";


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.bit.lot.flower.auth.store.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Getter
public class BusinessImageUrlDto {

private String imageUrl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.bit.lot.flower.auth.store.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Builder
public class ImageRequestDto {

@JsonProperty("images")
private Image[] images;

@JsonProperty("lang")
private String lang;

@JsonProperty("requestId")
private String requestId;

@JsonProperty("resultType")
private String resultType;

@JsonProperty("timestamp")
private long timestamp;

@JsonProperty("version")
private String version;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Builder
public static class Image {

@JsonProperty("format")
private String format;

@JsonProperty("name")
private String name;

@JsonProperty("data")
private Object data;

@JsonProperty("url")
private String url;


}
}
111 changes: 111 additions & 0 deletions src/main/java/com/bit/lot/flower/auth/store/dto/ImageResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.bit.lot.flower.auth.store.dto;


import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Builder
public class ImageResponseDto {

@JsonProperty("version")
private String version;

@JsonProperty("requestId")
private String requestId;

@JsonProperty("timestamp")
private long timestamp;

@JsonProperty("images")
private List<ImageInfo> images;


@AllArgsConstructor
@NoArgsConstructor
@Getter
@Builder
public static class ImageInfo {

@JsonProperty("uid")
private String uid;

@JsonProperty("name")
private String name;

@JsonProperty("inferResult")
private String inferResult;

@JsonProperty("message")
private String message;

@JsonProperty("validationResult")
private ValidationResult validationResult;

@JsonProperty("fields")
private List<FieldInfo> fields;


@AllArgsConstructor
@NoArgsConstructor
@Getter
@Builder
public static class ValidationResult {

@JsonProperty("result")
private String result;


}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Builder
public static class FieldInfo {

@JsonProperty("valueType")
private String valueType;

@JsonProperty("boundingPoly")
private BoundingPoly boundingPoly;

@JsonProperty("inferText")
private String inferText;

@JsonProperty("inferConfidence")
private double inferConfidence;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Builder
public static class BoundingPoly {

@JsonProperty("vertices")
private List<Vertex> vertices;


@AllArgsConstructor
@NoArgsConstructor
@Getter
@Builder
public static class Vertex {

@JsonProperty("x")
private double x;

@JsonProperty("y")
private double y;

}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ public class StoreMangerSignUpCommand {
@URL
@NotNull
private String businessNumberImage;
@NotNull
private String businessNumber;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

import bloomingblooms.response.CommonResponse;
import com.bit.lot.flower.auth.common.valueobject.AuthId;
import com.bit.lot.flower.auth.store.dto.BusinessImageUrlDto;
import com.bit.lot.flower.auth.store.dto.StoreManagerLoginResponseWithNameAndStoreId;
import com.bit.lot.flower.auth.store.dto.StoreMangerSignUpCommand;
import com.bit.lot.flower.auth.store.http.message.StoreManagerNameRequest;
import com.bit.lot.flower.auth.store.http.message.StoreManagerStoreIdRequest;
import com.bit.lot.flower.auth.store.mapper.StoreManagerMessageMapper;
import com.bit.lot.flower.auth.store.message.StoreMangerCreateRequest;
import com.bit.lot.flower.auth.store.service.EmailDuplicationCheckerService;
import com.bit.lot.flower.auth.store.service.RequestBusinessNumberFromImageService;
import com.bit.lot.flower.auth.store.service.StoreManagerService;
import com.bit.lot.flower.auth.store.valueobject.StoreId;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -18,6 +20,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -26,13 +29,20 @@
@Slf4j
@RequiredArgsConstructor
@RestController
public class StoreManagerRestController{
public class StoreManagerRestController {

private final StoreManagerNameRequest<AuthId> storeManagerNameRequest;
private final StoreManagerStoreIdRequest<StoreId,AuthId> storeManagerStoreIdRequest;
private final StoreManagerStoreIdRequest<StoreId, AuthId> storeManagerStoreIdRequest;
private final EmailDuplicationCheckerService emailDuplicationCheckerService;
private final StoreManagerService<AuthId> storeManagerService;
private final StoreMangerCreateRequest storeMangerCreateRequest;
private final RequestBusinessNumberFromImageService requestBusinessNumberFromImageService;

@PostMapping("/stores/business-image-number")
public CommonResponse<String> getBusinessNumber(@RequestBody BusinessImageUrlDto dto) {
return CommonResponse.success(
requestBusinessNumberFromImageService.getBusinessNumber(dto.getImageUrl()));
}


@PostMapping("/stores/emails/{email}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ protected boolean shouldNotFilter(HttpServletRequest request) {
String requestURI = request.getRequestURI();
return !requestURI.contains("/stores") || requestURI.contains("/stores/login") ||
requestURI.contains("/stores/signup") ||
requestURI.contains("/stores/emails");
requestURI.contains("/stores/emails") ||
requestURI.contains("/stores/business-image-number");
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.bit.lot.flower.auth.store.service;

import com.bit.lot.flower.auth.store.dto.ImageRequestDto;
import com.bit.lot.flower.auth.store.dto.ImageResponseDto;
import com.bit.lot.flower.auth.store.dto.ImageResponseDto.ImageInfo.FieldInfo;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@RequiredArgsConstructor
@Service
public class NaverClovaOCRService implements
RequestBusinessNumberFromImageService {

private final RestTemplate restTemplate;
@Value("${ocr.naver.secret}")
private String ocrSecret;

@Override
public String getBusinessNumber(String imageUrl) {

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

headers.set("X-OCR-SECRET", ocrSecret);
ImageRequestDto requestDto = createImageRequestDtoByImageUrl(imageUrl);

HttpEntity<ImageRequestDto> requestEntity = new HttpEntity<>(requestDto, headers);

String apiUrl = "https://5uwmtf47oq.apigw.ntruss.com/custom/v1/27880/634611cfef823f53c9ee0a045d3cbfddfa75b4154fbb56e2b30019466f39fedc/general";


ImageResponseDto response = restTemplate.postForObject(apiUrl, requestEntity,
ImageResponseDto.class);

return getBusinessNumberFromResponse(response);


}

private String getBusinessNumberFromResponse(ImageResponseDto responseDto) {

Optional<FieldInfo> businessNumberfield = responseDto.getImages().stream()
.flatMap(imageInfo -> imageInfo.getFields().stream())
.filter(fieldInfo -> fieldInfo.getInferText().contains("-"))
.findFirst();

if (businessNumberfield.isPresent()) {
return businessNumberfield.get().getInferText();
}

throw new IllegalArgumentException("사업자 등록 번호를 찾을 수 없습니다.");
}

private ImageRequestDto createImageRequestDtoByImageUrl(String imageUrl) {
ImageRequestDto.Image[] image = {createImage(imageUrl)};
return ImageRequestDto.builder().images(image).lang("ko").requestId("string")
.resultType("string")
.version("V1").timestamp(System.currentTimeMillis()).build();
}

private ImageRequestDto.Image createImage(String imageUrl) {
return ImageRequestDto.Image.builder().data(null).format("jpg").name("medium").url(imageUrl)
.build();


}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.bit.lot.flower.auth.store.service;

import org.springframework.stereotype.Service;

@Service
public interface RequestBusinessNumberFromImageService {

public String getBusinessNumber(String imageUrl);
}

0 comments on commit d2bebb0

Please sign in to comment.