'2020/07'에 해당되는 글 9건

  1. 2020.07.31 [Terraform] S3 Backend 사용.
  2. 2020.07.23 [Spring] Spring Security Session Timeout Disable.
  3. 2020.07.21 [Java] jar 파일 내 resources 읽기 - ClassPathResource
  4. 2020.07.21 [Spring] viewResolver return types...
  5. 2020.07.21 [Terraform] apply 후 생성 된 정보 구하기.
  6. 2020.07.20 [AWS] Java SDK 2.x 사용 - build.gradle
  7. 2020.07.13 [AWS] DescribeInstanceTypes from Java
  8. 2020.07.07 [Spring] Spring Security 기본 Login Form 제거 및 CSRF 만 사용하기
  9. 2020.07.06 [Spring] 다국어(i18n) 적용 하기.

[Terraform] S3 Backend 사용.

Cloud&Container/IaC 2020. 7. 31. 14:21

공식 문서는 아래 참고 하세요.

https://www.terraform.io/docs/backends/types/s3.html

 

terraform {
  backend "s3" {
    bucket = "버킷이름"
    key    = "폴더/파일"
    region = "ap-northeast-2"
  }
}
  • s3 backend 를 사용하기 위해서 먼저 s3 bucket 을 생성 합니다.
  • key 부분은 미리 생성해 놓지 않아도 terraform 실행을 하시면 생성이 됩니다.
  • 만약 bucket 을 만들어 놓지 않고 실행을 하게 되면 아래와 같은 에러가 발생 합니다.
Error refreshing state: BucketRegionError: incorrect region, 
the bucket is not in 'ap-northeast-2' region at endpoint ''
	status code: 301, request id: , host id:
  • bucket 을 생성 하고  $ terraform init 을 해도 같은 에러가 계속 발생 합니다.
  • 이 경우 .terraform 폴더를 삭제 하고 다시 시도 하시면 정상적으로 동작 합니다.

Terraform 기본 명령어)

$ terraform init

$ terraform plan

$ terraform apply

$ terraform destroy

.aws 에 aws configure 를 통한 설정이 되어 있어야 합니다.

 

 

:

[Spring] Spring Security Session Timeout Disable.

ITWeb/개발일반 2020. 7. 23. 07:55

application.yml 또는 properties 파일 내 아래와 같이 선언 하시면 됩니다.

server:
  servlet:
    session:
      timeout: 0

 

:

[Java] jar 파일 내 resources 읽기 - ClassPathResource

ITWeb/개발일반 2020. 7. 21. 20:23

제목에 있는 그대로 입니다.

 

[참고문서]

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/ClassPathResource.html

 

ClassPathResource 를 이용해서 구현 하면 됩니다.

// resourceFile 은 resources 를 제외한 path 와 filename 을 작성 합니다.

ClassPathResource resource = new ClassPathResource(resourceFile);

 

아래 예제 코드는 programcreek.com 에서 가져 왔습니다.

public static String getResourceAsString(String path) {
	try {
		Resource resource = new ClassPathResource(path);
		try (BufferedReader br = new BufferedReader(
				new InputStreamReader(resource.getInputStream()))) {
			StringBuilder builder = new StringBuilder();
			String line;
			while ((line = br.readLine()) != null) {
				builder.append(line).append('\n');
			}
			return builder.toString();
		}
	}
	catch (IOException e) {
		throw new IllegalStateException(e);
	}
}

이와 같이 구현을 한 이유는 로컬에서는 잘 되는데 jar 로 말았을 때는 file 을 찾지 못하는 에러가 발생을 해서 입니다.

:

[Spring] viewResolver return types...

ITWeb/개발일반 2020. 7. 21. 10:45

[참고문서]

https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsingle/#mvc-ann-return-types

https://docs.spring.io/spring/docs/3.0.0.M3/spring-framework-reference/html/ch16s03.html

 

Spring mvc framework 에서 Controller 에서 사용하는 return type 에 따른 viewResolver 적용방법이 다릅니다.

위 문서를 보시면 이해 하실 수 있습니다.

 

가장 많이 사용 하는 몇 개만 뽑아 왔습니다.

 

  • A ModelAndView object, with the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
  • A Model object, with the view name implicitly determined through a RequestToViewNameTranslator and the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
  • A Map object for exposing a model, with the view name implicitly determined through a RequestToViewNameTranslator and the model implicitly enriched with command objects and the results of @ModelAttribute annotated reference data accessor methods.
  • A View object, with the model implicitly determined through command objects and @ModelAttribute annotated reference data accessor methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above).
  • A String value that is interpreted as the logical view name, with the model implicitly determined through command objects and @ModelAttribute annotated reference data accessor methods. The handler method may also programmatically enrich the model by declaring a Model argument (see above).
  • void if the method handles the response itself (by writing the response content directly, declaring an argument of type ServletResponse / HttpServletResponse for that purpose) or if the view name is supposed to be implicitly determined through a RequestToViewNameTranslator (not declaring a response argument in the handler method signature).
  • If the method is annotated with @ResponseBody, the return type is written to the response HTTP body. The return value will be converted to the declared method argument type using HttpMessageConverters. See the section called “Mapping the response body with the @ResponseBody annotation”.

제가 실수한 부분은 void 로 선언을 해 놓고 template 수정 후 반영이 되지 않아 cache 를 의심 했었는데 역시 원인은 제가 선언을 잘 못 했기 때문 이였습니다.

 

void 로 선언 시 해석은 

@GetMapping("/hello")
public void helloworld() {...}

hello.html 을 template 으로 찾게 됩니다.

 

:

[Terraform] apply 후 생성 된 정보 구하기.

ITWeb/개발일반 2020. 7. 21. 08:44

Terraform  을 이용해서 인프라 구성을 한 후에 생성된 정보를 얻어 와야 할 때가 있습니다.

특정 정보만 구하고 싶을 경우 output 설정을 이용해서 얻을 수 있는 방법과 state 를 이용해서 얻을 수 있는 방법이 있습니다.

 

[참고문서]

https://www.terraform.io/docs/commands/output.html

 

tf 설정 파일 내 output 설정을 합니다.

resource "aws_instance" "allinone" {
...중략...
  count = 3
}

output "private_ip" {
  value = "${aws_instance.allinone.*.private_ip}"
}
# 변수 정보를 정상적으로 호출 하지 못할 경우 실행
$ terraform refresh

$ terraform output 변수명
-->
$ terraform output private_ip
[
  "10.0.25.14",
]

 

아래와 같이 하면 모든 state 정보를 return 해 줍니다.

 

$ terraform state pull

 

:

[AWS] Java SDK 2.x 사용 - build.gradle

Cloud&Container/AWS 2020. 7. 20. 08:58

1.x 와는 많이 바뀌었습니다.

확인 하고 변경 하세요.

 

implementation platform('software.amazon.awssdk:bom:2.13.55')
// aws sdk
compile 'software.amazon.awssdk:sdk-core'
compile 'software.amazon.awssdk:ec2'
compile 'software.amazon.awssdk:s3'
compile 'software.amazon.awssdk:sqs'

:

[AWS] DescribeInstanceTypes from Java

Cloud&Container/AWS 2020. 7. 13. 18:06

본 API 가 삭제 되었기 때문에 그냥 아래와 같이 단순하게 구성해 보았습니다.

 

@Service
@Log4j2
@RequiredArgsConstructor
public class Ec2Service {

  public ResponseEntity<String> describeInstanceTypes() {
    ProcessBuilder builder = new ProcessBuilder();
    String instances = "{}";

    try {
      builder.command("bash", "-c", "aws ec2 describe-instance-types");
      Process process = builder.start();

      instances = new BufferedReader(
        new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)).lines()
        .collect(Collectors.joining("\n"));

      int exitCode = process.waitFor();

      JSONObject jsonObject = new JSONObject(instances);
      JSONArray jsonArray = jsonObject.getJSONArray("InstanceTypes");
      JSONArray sortedJsonArray = new JSONArray();
      List list = new ArrayList();

      for(int i = 0; i < jsonArray.length(); i++) {
        list.add(jsonArray.getJSONObject(i));
      }

      Collections.sort(list, new Comparator<JSONObject>() {

        @Override
        public int compare(JSONObject a, JSONObject b) {
          String source = new String();
          String target = new String();

          try {
            source = (String)a.get("InstanceType");
            target = (String)b.get("InstanceType");
          } catch(Exception e) {
          }

          return source.compareTo(target);
        }
      });

      for(int i = 0; i < jsonArray.length(); i++) {
        sortedJsonArray.put(list.get(i));
      }

      instances = "{\"InstanceTypes\": " +sortedJsonArray.toString()+ "}";
    } catch(Exception e) {
      e.printStackTrace();
    }

    return ResponseEntity.ok(instances);
  }
}

 

코드 자체는 AWS CLI 를 ProcessBuilder 를 이용해서 외부 파일을 실행 시켜 데이터를 받아와서 처리 하는 내용입니다.

AWS CLI 로 데이터를 가져 오게 되면 정렬이 안된 상태로 데이터가 넘어 와서 별도 sort 기능 구현이 필요 합니다.

그냥 CLI 에서도 JSON Sort 옵션 하나 넣어 주면 좋았을 걸 아쉽더라고요.

 

근데 왜 이 API 를 없앴는지 차암....

 

1.x 에서 삭제 되어서 2.x 로 변경해서 사용 하시면 됩니다.

    Ec2Client ec2Client = Ec2Client.builder().build();
    DescribeInstanceTypesRequest request = DescribeInstanceTypesRequest.builder()
      .maxResults(100)
      .build();

    DescribeInstanceTypesResponse response = ec2Client.describeInstanceTypes(request);
    log.debug("{}", response.instanceTypes());

한번에 100개 씩 밖에 못 가져 오기 때문에 nextToken 으로 끝까지 요청 하셔야 합니다.

:

[Spring] Spring Security 기본 Login Form 제거 및 CSRF 만 사용하기

ITWeb/개발일반 2020. 7. 7. 08:33

사용하다 보면 별의 별 요구사항이 나오게 됩니다.

단순 하게 CSRF 만 사용 하고 싶은데 자꾸 login form 이 나와서 설정만으로 이걸 해결해 보고자 했습니다.

그러나 설정 만으로는 안되더라고요.

 

설정 예시) 비추천

security:
  enable:
    csrf: true
  basic:
    enabled: false

management:
  security:
    enabled: false

 

코드 예시) 추천

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity.httpBasic().disable();
  }
}

참고 정보)

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-quartz'
  implementation 'org.springframework.boot:spring-boot-starter-web'

  // thymeleaf
  implementation 'org.thymeleaf.extras:thymeleaf-extras-java8time'
  implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
  implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'

  // spring security
  implementation 'org.springframework.boot:spring-boot-starter-security'
  implementation 'org.springframework.security:spring-security-test'
  implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'

  implementation 'org.webjars:jquery:3.5.0'
  implementation 'org.webjars:jquery-ui:1.12.1'
  implementation 'org.webjars.bower:bootstrap:4.4.0'

  compileOnly 'org.projectlombok:lombok'
  annotationProcessor 'org.projectlombok:lombok'
  providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
  testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
}


<meta id="_csrf" name="_csrf" th:content="${_csrf.token}" />
<meta id="_csrf_header" name="_csrf_header" th:content="${_csrf.headerName}" />

let token = $("meta[name='_csrf']").attr("content");
let header = $("meta[name='_csrf_header']").attr("content");

$(function() {
  $(document).ajaxSend(function(e, xhr, options) {
    xhr.setRequestHeader(header, token);
  });
});


spring:
  security:
    user:
      name: admin
      password: admin

 

 

:

[Spring] 다국어(i18n) 적용 하기.

ITWeb/개발일반 2020. 7. 6. 16:08

다국어를 적용 하기 위한 Code Snippet

 

1. Javascript

- 구글링 해보시면 몇 가지 framework 들이 나옵니다.

- 쉽게 구현을 한다고 하면, 현재의 locale 정보를 cookie 또는  session 에서 읽어 와서 해당 하는 locale 의 message 파일 또는 변수를 가지고 설정 하면 됩니다.

 

예제)

let MESSAGE = new Object();
let INTL = MESSAGE["ko"];

document.addEventListener("DOMContentLoaded", function(){
  let applicationLocale;

  try {
    applicationLocale = $.cookie("APPLICATION_LOCALE");
  } catch (e) {
  }

  if(!applicationLocale) {
    applicationLocale = "ko";
  }

  INTL = MESSAGE[applicationLocale];
});

MESSAGE.ko = {
  "HELLO": "안녕"
};

MESSAGE.en = {
  "HELLO": "Hello"
};

MESSAGE.ja = {
  "HELLO": "こんにちは"
};

- 여기서 원래는 jquery 를 이용 하다 보니 $ 가 들어 갔었는데 $(document).ready(); 였으나 $를 찾지 못해서 걍 바꿔 놓았습니다.

- 코드 설명은 쉽습니다. 그냥 정적으로 정의해서 사용 하는 내용이라 보시면 다 아실 것 같습니다.

 

2. Spring Boot + Thymeleaf

Cookie 와 Session 두 가지로 적용이 가능 합니다.

구글링 해보시면 거의 똑같은 코드로 예제들이 나올 거예요.

그냥 담는 그릇을 Cookie 로 할건지 Session 으로 할 건지의 차이 라고 보시면 됩니다.

- 프로젝트에서 "Resource Bundle 'messages'" 를 추가해 주세요.

- 추가 하면서, ko, en, ja, zh 생성 하시면 됩니다.

- messages.properties

- messages_ko.properties

HELLO=안녕

- messages_en.properties

HELLO=Hello

- messages_zh.properties

- messages_ja.properties

HELLO=こんにちは

 

2.1 Session

@Configuration
public class LocaleConfig implements WebMvcConfigurer {

  @Bean
  public LocaleResolver localeResolver() {

    SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
    sessionLocaleResolver.setDefaultLocale(Locale.KOREAN);

    return sessionLocaleResolver;
  }

  @Bean
  public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName("locale");
    
    return localeChangeInterceptor;
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
  }

}

 

2.2 Cookie

@Configuration
public class LocaleConfig implements WebMvcConfigurer {

  @Bean
  public LocaleResolver localeResolver() {

    CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
    cookieLocaleResolver.setDefaultLocale(Locale.KOREAN);
    cookieLocaleResolver.setCookieName("APPLICATION_LOCALE");
    return cookieLocaleResolver;
  }

  @Bean
  public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName("locale");
    return localeChangeInterceptor;
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
  }

}

그래서 Thymeleaf 에서는 아래와 같이 쓰시면 됩니다.

<h1 th:text="#{HELLO}"></h1>

 

: