- Spring Initializr에 접속하여 Spring Boot 프로젝트 생성하기

  1. start.spring.io 에 접속하기

       Spring Initializr는 https://github.com/spring-io/initializr 오픈소스를 이용한 웹 서비스 이다.



  2. 원하는 Build 도구와 Spring Boot 버전을 선택한 뒤, 내게 필요한 dependency들을 선택한다.

       이미 많이 사용하는 의존성 목록을 추가해 놓았기 때문에, 일일히 mvnrepository에서 각 패키지들을 import 시킬 필요가 없고, start로 주어지는 묶음을 사용하면 매우 편리하다.


  3. Generate Project를 눌러 Project 파일을 다운로드 받고, 압축을 푼다.

  4. IntelliJ나 eclipse에서 압축을 푼 프로젝트를 열어준다.

  5. 만약 Thymeleaf나 react 등이 아닌 JSP로 Frontend를 사용하고 싶다면, 아래와 같은 jasper와 jstl dependency를 추가 해주어야 한다.

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>

6. 작성 중

'Java Programming' 카테고리의 다른 글

Maven에서 Java Spring 세팅하기  (0) 2018.11.30
Spring Boot 2에서 Thymeleaf 사용하기  (0) 2018.11.21
Java 101 - StringBuffer  (0) 2018.09.03
Java 101 - 생성자(Constructor)  (0) 2018.09.03

WRITTEN BY
장태희

,

※ 본 항목은 업데이트 중입니다.


- pom.xml 설정하기

Spring Project를 개발하기 위해서는, pom.xml에 packaging과 property, dependency를 추가 해 주어야 한다.


  - packaging을 war로 설정한다.

<packaging>war</packaging>

  - property에서 Spring Framework version을 설정한다.

    spring.version에 대한 정보는 Github(https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-Versions) 혹은 Spring 공식 사이트(https://spring.io/projects/spring-framework#learn)에서 확인할 수 있다.

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version> 4.3.5.RELEASE</spring.version>
</properties>

  - <dependencies>에서 spring framework에 필요한 spring-context, spring-webmvc, spring-test dependency들을 추가해 준다.

<dependencies>
<!-- Spring 프로젝트에 필요한 dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>

  - JSP를 추가적으로 이용하고자 한다면, JSTL과 EL dependency들을 추가해준다.

<!-- Servlet JSP 사용을 위한 JSTL dependency -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

  - Database 통신을 위해 PlatformTransactionManager와 JDBC, SQL Driver(여기에서는 MySQL), Connection Pool(여기에서는 DBCP2)을 추가한다.

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>

<!-- basic data source -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>

  - 파일 전송을 원한다면, Apache Commons File Upload와 IO를 추가한다.

<!-- Apache Commons FileUpload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>

<!-- Apache Commons IO -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>


</dependencies>

  - JDK 버전과 tomcat 버전을 추가해준다.

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<charset>UTF-8</charset>
<uriEncoding>UTF-8</uriEncoding>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>


- Servlet 3.0 미만에서 web.xml 설정하기

  전통적인 Spring Framework에서는 해당 프로젝트의 /src/main/webapp/WEB-INF 폴더에 web.xml 파일을 추가함으로서 Application Context를 등록할 수 있다. web.xml은 tomcat과 같은 was가 읽어서 등록시킨다.

  web.xml은 아래와 같이 설정한다.

<?xml version="1.0" encoding="UTF-8"?>
<web-app>

<display-name>Spring JavaConfig Sample</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>examples.daoexam.config.ApplicationConfig</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>examples.daoexam.config.WebMvcContextConfiguration</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


</web-app>


'Java Programming' 카테고리의 다른 글

Maven으로 Spring Boot 2 세팅하기  (0) 2018.12.03
Spring Boot 2에서 Thymeleaf 사용하기  (0) 2018.11.21
Java 101 - StringBuffer  (0) 2018.09.03
Java 101 - 생성자(Constructor)  (0) 2018.09.03

WRITTEN BY
장태희

,

- Thymeleaf란?

타임리프는 server-side의 Java 템플릿으로서,  TML, XML, JavaScript, CSS, 일반 텍스트 문서를 생성하는 View Template Engine이다.

타임리프의 목표는 우아하고, 고 유지보수성(유지보수가 용이한)의 템플릿 생성을 제공하는 것이다.


- Spring Boot2에서 Thymeleaf 적용 설정하기

1-1. Spring Initializr에서 Thymeleaf 선택하기

Spring Initializr(https://start.spring.io/)에서 Spring Boot 프로젝트를 생성할 때, Thymeleaf 항목을 추가한다.


1-2. pom.xml에서 spring-boot-starter-thymeleaf 추가하기(Maven 기준)

https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf 에서 본인이 원하는 버전에 맞는 타임리프 버전의 dependency를 추가해 준다.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>

2. application.properties에서 thymeleaf 관련 설정

application.properties에서 다음의 항목을 추가해 준다.

spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=/WEB-INF/views/
spring.thymeleaf.suffix=.html

spring.thymeleaf.enabled → thymeleaf의 사용을 enable 시킨다.

spring.thymeleaf.encoding → thymeleaf의 인코딩을 UTF-8로 기본 설정한다.

spring.thymeleaf.prefix → thymeleaf의 기본 경로인 templates를 원하는 임의의 경로로 설정한다. (여기에서는 webapps/WEB-INF/views 로 설정하였다.)

spring.thymeleaf.suffix → thymeleaf가 읽어들이는 파일의 확장자를 설정한다. 예를 들어 Controller의 method에서 return "index"; 일 경우, 자동적으로 index.html을 읽어로게 된다.


- Thymeleaf의 기본 폴더 경로

Thymeleaf에서는 기본적으로 resources 폴더의 templates폴더에 html 파일들이 위치한다.

이 외의 css, js 등의 정적 파일들은 static 폴더에 위치하게 된다.


- Trouble Shooting

1. ERROR - org.thymeleaf.exceptions.TemplateInputException: Error resolving template “index”, template might not exist or might not be accessible by any of the configured Template Resolvers

→ templates 폴더에 "index.html"파일이 없어서 발생하는 문제이다. 이 외에도 해당 경로에 대상 view가 없으면 이와 같은 에러가 발생한다.

2. 기존에 JSP를 사용하고 있다면, jstl과 jasper 관련 설정을 pom.xml에서 주석 처리 해주어야 한다.


참고

http://cyberx.tistory.com/132

http://araikuma.tistory.com/30

https://blog.hanumoka.net/2018/08/01/spring-20180801-spring-Thymeleaf/

https://www.tutorialspoint.com/spring_boot/spring_boot_thymeleaf.htm

https://www.mkyong.com/spring-boot/spring-boot-hello-world-example-thymeleaf/

http://rura6502.tistory.com/2

http://wonwoo.ml/index.php/post/1209

https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#what-is-thymeleaf

https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html

https://memorynotfound.com/spring-boot-thymeleaf-configuration-example/

https://cizz3007.github.io/%ED%83%80%EC%9E%84%EB%A6%AC%ED%94%84/syntax/thymeleaf/2018/04/09/thymeleaf/


'Java Programming' 카테고리의 다른 글

Maven으로 Spring Boot 2 세팅하기  (0) 2018.12.03
Maven에서 Java Spring 세팅하기  (0) 2018.11.30
Java 101 - StringBuffer  (0) 2018.09.03
Java 101 - 생성자(Constructor)  (0) 2018.09.03

WRITTEN BY
장태희

,

2018년 9월 15일 토요일 우분투 한국 커뮤니티 9월 서울지역 세미나의 발표 자료입니다.

URL : http://event.ubuntu-kr.org/2018/07/31/sept-seoul-seminar.html

Youtube : https://youtu.be/XMeyVWVK4W8?t=6558

[20180919]리눅스를 이용한 이중화 기초.pdf


Feedback : rsync를 사용할 시 보안 문제가 발생할 수 있다. Hadoop 등을 사용하는 것을 추천.

'커뮤니티 활동' 카테고리의 다른 글

Global Azure Bootcamp x Korea 2017  (0) 2018.02.03

WRITTEN BY
장태희

,

※ 본 내용은 Java JDK 8을 기준으로 설명합니다.


- StringBuffer란

공식 문서: https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html

StringBuffer는 문자열을 추가하거나 변경 할 때 주로 사용하는 자료형이다.


- StringBuffer에서 사용할 수 있는 메소드는 아래와 같다.

append

insert

delete

substring

reverse


- StringBuffer 사용 예제

StringBuffer.append


StringBuffer.insert


StringBuffer.substring


StringBuffer.delete


StringBuffer.reverse



WRITTEN BY
장태희

,
  • 생성자란?

생성자란 객체를 생성할 때 초기화를 위해 항상 맨 처음 실행되는 메소드이다.



  • 생성자 규칙

자바 생성자를 만들기 위한 규칙은 3가지가 있다.

1. 생성자 이름은 클래스의 이름과 동일해야 한다.

2. 생성자는 일반적인 메소드와 다르게 명시된 리턴 타입이 없어야 한다.

3. Java 생성자는 abstract, static, final, synchronized가 될 수 없다.



  • 생성자의 종류

생성자는 두 가지 형태가 존재한다.

1. 기본 생성자(매개변수가 없는 생성자)

2. 매개변수가 있는 생성자


자바 기본 생성자

어떠한 매개변수도 갖지 않는 생성자를 기본 생성자 라고 한다.


생성자 구현 예제

기본 생성자


매개변수가 있는 생성자

'Java Programming' 카테고리의 다른 글

Maven으로 Spring Boot 2 세팅하기  (0) 2018.12.03
Maven에서 Java Spring 세팅하기  (0) 2018.11.30
Spring Boot 2에서 Thymeleaf 사용하기  (0) 2018.11.21
Java 101 - StringBuffer  (0) 2018.09.03

WRITTEN BY
장태희

,

2017년 4월 22일, Azure 한국 커뮤니티 주관, 오픈스택 한국 커뮤니티, 우분투 한국 커뮤니티, 코딩과 무관합니다만등의 운영진 참여로 Global Azure Bootcamp x Korea 2017이 개최되었습니다.

성공적인 행사가 될 수 있도록 도와주신 많은 분들께 진심으로 감사드립니다.



WRITTEN BY
장태희

,

- Intro -

안녕하세요, 장태희 입니다.

오늘은 자신의 사이트에 무료로 SSL을 발급받고 만료가 되기 전에 자동 갱신을 해보도록 하겠습니다.

얼마 전까지 제가 운영하던 https://www.ubuntu-kr.org/Startcom에서 무료로 발급하는 Startssl을 사용 중이었는데요, 무료인 점은 좋지만 몇 가지 단점이 있었습니다.


1. 갱신 주기에 맞추어 재갱신을 해주어야 한다. 재갱신 주기를 놓칠 경우 인증서를 처음 key 파일부터 다시 해야한다.

2. 예전에 발급받은 인증서의 경우 재갱신을 할 경우에 서버측 intermediate.crt 파일이 바뀌어 바뀐 crt 파일을 넣어줘야 한다.

3. SSL 발급 도중 발생 도중 본인의 실수로 인하여 발급받지 못한 경우 무조건 금액을 지불하고 복구하여야 한다.


SSL은 대부분 돈 주고 발급받고있고, 가격도 저렴한것부터 EV Class등 비싼것도 많습니다. 찾아보면 부담스럽지 않는 Class 1 SSL도 많습니다.

하지만 비영리 커뮤니티를 운영하는 저의 경우 누가 돈을 계속 지불할것이며, 누가 관리를 지속적으로 해 줄 것인가에 대한 문제가 있어 최대한 무료로 할 수 있는 방법을 적용하였습니다.


- Environment -

SSL 발급을 위한 세팅을 다음과 같이 적용하였습니다.

- Environment -

OS

 Microsoft Azure Cloud

(Virtual Machine / Ubuntu 14.04)

 

Web Server Application

 Apache2

 

Domain 

 필요 / ssl.thjang.com (hosting.kr에서 구입)

 


- Install -

1. https://certbot.eff.org/ 으로 접속합니다.

2. 현재 사용중인 Webserver와 OS를 선택합니다. 두 조합에 따라 설치 방법이 약간씩 다릅니다.


3. 선택 후 아래와 같이 설치 매뉴얼이 제공됩니다.


4. /usr/local/sbin 폴더로 이동합니다. 

   아무 디렉터리에서나 실행 및 설치가 가능하지만 추후 자동 갱신 cron설정을 위해 이 폴더로 설정하였습니다.

cd /usr/local/sbin


wget 명령어로 certbot-auto 파일을 다운로드 받습니다. 우분투 16.04 / Debian 8의 경우 apt-get으로 python-letsencrypt-apache 패키지를 바로 설치할 수 있습니다.

sudo wget https://dl.eff.org/certbot-auto


다운로드 받은 certbot-auto 파일에 실행 권한을 줍니다.

sudo chmod a+x certbot-auto


관리자 권한으로 certbot-auto를 실행합니다.

sudo ./certbot-auto


기다리고 있으면 설치 확인 메시지와 함께 필요한 패키지들을 자동 설치하게 됩니다.


- Deploy -

기본 패키지 설치가 완료되면

1. 어떠한 도메인 정보도 없어 '도메인 정보가 없는데 계속 진행할 것이냐' 혹은(현재의 경우)

2. VirtualHost 파일에 도메인 정보를 저장한 경우 어떤 도메인을 대상으로 발급 할 것인지 창이 뜨게 됩니다.

해당하는 도메인만 선택하시거나(처음에는 모든 도메인에 *표시로 선택이 되어 있습니다.) Yes를 선택하여 계속 진행합니다.


SSL 발급을 원하는 도메인을 입력합니다. 

콤마, 슬래시, 스페이스바로 한칸 띄우고 다른 도메인을 입력하면 여러 다른 서브도메인들도 같이 발급받을 수 있습니다.

여기서는 ssl.thjang.com 하나만 발급받아 보았습니다.


해당 도메인을 구입하였을때 입력한 메일 주소를 입력합니다.

도메인 구입시 등록한 이메일이 생각나지 않을 경우, http://whois.kisa.or.kr/kor/에서 확인하실 수 있습니다.


Certbot 이용에 대한 문서 안내가 나옵니다. Agree를 선택합니다.


SSL적용 방식에 대해 선택합니다.

http와 https 둘 다 허용하고 싶으면 Easy를, http로 접속하면 https로 자동 접속을 원하면 Secure를 선택합니다.


SSL 발급이 완료되고 바로 사이트에 적용됩니다.

화면에 성공 창이 나타나면 사이트에 접속하여 SSL이 적용된 것을 확인할 수 있습니다.


- Renew -

Certbot을 통한 SSL 발급은 기한이 3개월 입니다. 인증서 갱신을 위해서는 renew --dry-run 옵션을 통해 연장할 수 있습니다.

sudo ./certbot-auto renew --dry-run


- Automation -

SSL적용 후의 최종 목적은 '내가 굳이 신경쓰지 않아도 알아서 갱신되었으면 좋겠다'이실 겁니다.

만료 주기를 어딘가에 적어놓거나 만료 시점의 메일을 받고 재갱신을 하는건 여간 귀찮은 일이 아닐 수 없습니다.

데비안의 경우 /etc/cron.d/certbot에 자동으로 설정이 되지만 우분투의 경우 crontab 등으로 세팅을 해 주셔야 합니다.


아래는 데비안의 자동 세팅 파일입니다.


데비안 세팅 파일을 참고하여 우분투 crontab에 등록을 해 보도록 하겠습니다.

먼저 crontab에 접속합니다.

sudo 권한으로 실행해야하기 때문에 앞에 꼭 sudo를 붙겨주시기 바랍니다.

sudo crontab -e


crontab 파일 맨 아래 아래와 같이 입력합니다.

0 */12 * * * /usr/local/sbin/certbot-auto renew --quiet --no-self-upgrade

시간 세팅이 0 */12 * * * 은 매일 12시간 0분마다 certbot-auto를 실행하라는 뜻입니다.

현재 certbot-auto는 하루에 두번 실행을 권장하고 있습니다.

평소에는 아무 작업도 하지 않지만, 인증서 만기가 다가온 경우 자동으로 인증서를 갱신하게 됩니다.


- Trouble Shooting -

1. 집 컴퓨터에서는 SSL 발급이 안됩니다.

→ Public IP와 Domain을 가지고 있어야 발급이 가능합니다. Router IP와 localhost로는 실행할 수 없습니다.


2. Failed authorization procedure. domain (tls-sni-01): run:acme:error:connection :: The server could not connect to the client to verify to domain ~ 과 같은 에러가 나타납니다.


→ 80포트와 더불어 443포트(https)도 개방되어 있는지 확인해 주시기 바랍니다. 클라우드 서버의 경우 보안그룹 옵션 등에서 포트 개방 여부를 확인하여 주세요.


3. certbot-auto 실행 후에 webserver ssl conf파일 옵션이 변경되었습니다.

→ certbot-auto는 자동 설정이라는 매우 편리함이 있지만, webserver의 ssl 설정을 수정하게 됩니다.

    백업 파일도 만들어 주지 않기 때문에 백업을 해두지 않으면 세팅값이 사라질 수도 있습니다.

    반드시 실행 전 ssl conf(우분투의 경우 /etc/apache2/sites-available/default-ssl.conf)파일을 반드시 백업하시기 바랍니다.


4. 바로 적용은 하지 않고 일단 crt와 pem키 등만 발급받고 싶어요.

→ certbot-auto 실행시 --webserver이름 certonly 옵션을 사용하면 SSL만 발급받고 서버에 적용은 되지 않습니다.

    경로는 /etc/letsencrypt에 저장되어 있습니다.

    여러 디렉터리 중 현재 적용되고 있는 pem 파일들을 보려면 live 디렉터리에서 확인하실 수 있습니다.

    설정 경로는 SSL이 적용된 서버의 conf 파일을 참고하시기 바랍니다.



WRITTEN BY
장태희

,