'ITWeb'에 해당되는 글 798건

  1. 2020.10.20 [Similarity] Universal Sentence Encoder/4 - 링크.
  2. 2020.10.13 [Spring] WebSocket 관련 링크
  3. 2020.09.07 [Bootstrap] Nav 사용 시 Form 태그 위치 주의.
  4. 2020.08.04 [JSON] JSON.parse(), JSON.stringify()
  5. 2020.07.23 [Spring] Spring Security Session Timeout Disable.
  6. 2020.07.21 [Java] jar 파일 내 resources 읽기 - ClassPathResource
  7. 2020.07.21 [Spring] viewResolver return types...
  8. 2020.07.21 [Terraform] apply 후 생성 된 정보 구하기.
  9. 2020.07.07 [Spring] Spring Security 기본 Login Form 제거 및 CSRF 만 사용하기
  10. 2020.07.06 [Spring] 다국어(i18n) 적용 하기.

[Similarity] Universal Sentence Encoder/4 - 링크.

ITWeb/검색일반 2020. 10. 20. 17:32

https://tfhub.dev/google/universal-sentence-encoder/4

https://www.elastic.co/blog/text-similarity-search-with-vectors-in-elasticsearch

 

Elasticsearch 내 dense_vector type 을 이용한 Similarity 검색을 하기 위해 필요한 거라 링크 걸어 둡니다.

:

[Spring] WebSocket 관련 링크

ITWeb/개발일반 2020. 10. 13. 12:08
https://spring.io/guides/gs/messaging-stomp-websocket/

https://stomp.github.io/stomp-specification-1.2.html

https://github.com/sockjs/sockjs-client

WebSocket 관련 개발 참고 문서 입니다.

:

[Bootstrap] Nav 사용 시 Form 태그 위치 주의.

ITWeb/개발일반 2020. 9. 7. 12:21

기억하기 위해 기록 합니다.

 

[참고문서]

bootstrap ui nav document

 

정상 동작 코드)

<nav>
  <div class="nav nav-tabs nav-pills" id="nav-tab" role="tablist">
    <a class="nav-item nav-link active" id="nav-terraform-configuration" data-toggle="tab" href="#terraform-configuration" role="tab" aria-controls="terraform-configuration" aria-selected="true">Step 1 - Terraform</a>
...중략...    
  </div>
</nav>

<div class="tab-content" id="nav-tabContent">
  
  <div class="tab-pane fade show active" id="terraform-configuration" role="tabpanel" aria-labelledby="nav-terraform-configuration">
    <form name="terraformForm">
...중략...
		</form>
	</div>

	<div class="tab-pane fade" id="kibana-installation" role="tabpanel" aria-labelledby="nav-kibana-installation">
...중략...
	</div>
</div>

 

비정상 동작 코드)

<nav>
  <div class="nav nav-tabs nav-pills" id="nav-tab" role="tablist">
    <a class="nav-item nav-link active" id="nav-terraform-configuration" data-toggle="tab" href="#terraform-configuration" role="tab" aria-controls="terraform-configuration" aria-selected="true">Step 1 - Terraform</a>
...중략...    
  </div>
</nav>

<div class="tab-content" id="nav-tabContent">
	
	<form name="terraformForm">
  <div class="tab-pane fade show active" id="terraform-configuration" role="tabpanel" aria-labelledby="nav-terraform-configuration">
...중략...
	</div>
	</form>

	<div class="tab-pane fade" id="kibana-installation" role="tabpanel" aria-labelledby="nav-kibana-installation">
...중략...
	</div>
</div>

위 코드의 차이점은 <form> 태그의 위치 입니다.

 

정상적으로 동작 하는 코드에서는

- <div> tab-content 내부에 tab 영역에 해당 하는 <div> 안쪽에 선언이 되어 있고,

- 비정상적으로 동작하는 코드에서는 밖에 선언이 되어 있습니다.

 

:

[JSON] JSON.parse(), JSON.stringify()

ITWeb/개발일반 2020. 8. 4. 16:49

[참고문서]

https://www.w3schools.com/js/js_json_parse.asp

https://www.w3schools.com/js/js_json_stringify.asp

 

String to JSON Object)

JSON_OBJECT = JSON.parse("JSON String");

 

JSON Object to String)

JSON_STRING = JSON.stringify(JSON_OBJECT);

 

:

[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

 

:

[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>

 

: