목록자바 (86)
삶 가운데 남긴 기록 AACII.TISTORY.COM
이번에는 재귀 호출을 이용하여 특정 폴더의 내용을 모두 삭제하는 예제를 구현해봤습니다. import java.io.File; public class DeleteDirectoryExample { public static void main(String[] args) { String directoryPath = "C:/Temp"; // 윈도우 실행 기준 File directory = new File(directoryPath); // 디렉토리가 존재하면 삭제 수행 if (directory.exists()) { deleteDirectory(directory); System.out.println("디렉토리 및 하위 경로가 삭제되었습니다."); } else { System.out.println("디렉토리가 존재하지 ..
자바로 텍스트 파일을 생성해서 자장할 때 FileWriter(arg1, arg2) 메서드의 arg2 두번째 인수가 false냐 true냐에 따라 기존 파일을 overwrite할지 append 할지 결정 할 수있습니다. import java.io.FileWriter; import java.io.IOException; public class FileWriterExample { public static void main(String[] args) { // 파일 경로 및 파일명 설정(윈도우 기준) String filePath = "C:/예제 파일이름.txt"; try { // FileWriter 객체 생성 //두 번째 매개변수로 false를 전달하면 기존 파일이 있으면 내용을 덮어씀(overwrite) //tr..
날짜 및 시간을 지정한 형식 yyyyMMddHHmmss( 년도월일시분초)으로 출력하는 예제 입니다. import java.text.SimpleDateFormat; import java.util.Date; public class CurrentDateTimeExample { public static void main(String[] args) { // 현재 날짜 및 시간 가져오기 Date currentDate = new Date(); // 원하는 형식으로 날짜 및 시간 포맷팅 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String formattedDateTime = dateFormat.format(currentDate); //..
groovy groovy는 jvm용 스크립트언어이며 gradle은 groovy를 사용한 빌드 도구입니다. gradle 소프트웨어 내부에 groovy가 포함되어 있으므로 따로 설치 하지 않아도 됩니다만, 여기서는 groovy언어 연습용으로 설치해보도록 합시다. 그루비 다운로드 링크: https://groovy.apache.org/download.html
Spring MVC 프로젝트 구조 설명 src/main/java/ : 자바 소스 경로 src/main/resources/ : 실행시 자동 참고되는 경로(주로 설정파일, log4j.xml 등등) src/test/java/ : 테스트 자바 코드 경로 src/test/resources/ : 테스트 관련 설정 파일 경로 src/webapp/WEB-INF/spring/appServlet/ : sevlet-context.xml 외 spring 설정 파일 src/webapp/WEB-INF/spring/ : root-context.xml 외 spring 설정 파일 src/webapp/WEB-INF/views/ : MVC 패턴 중 view 페이지(jsp) 들이 위치 한 경로 src/webapp/WEB-INF/ : tom..
관점 지향(Aspect Oriented Programming) 로깅 보안/인증 트랜잭션 리소스 풀링 에러 검사 / 처리 정책 멀티 스레드 안전 관리 데이터 영속처리 위 항목들은 소프트웨어 개발 시 발생하는 공통적인 문제들입니다. 핵심 관심(core concern)은 업무 로직을 말합니다. 횡단 관심(cross-cutting concern)은 위에서 언급한 로깅이나 보안등 공통 시스템 로직을 말합니다. 이처럼 업무 기능과 시스템 기능간에 결합성을 제거하도록 분리하여 작성하는 방법을 관점 지향이라고 합니다. 관점 지향 용어 advice : 관점이 언제, 무엇을 하는지를 정의 joinpoint : 관점이 실행될 수 있는 위치들 pointcut : joinpoint 중에서 advice 하는 위치(어디서) asp..
Spring bean 스프링 프레임워크가 관리하는 클래스들의 인스턴스 객체들을 Spring bean 이라고 합니다. Spring bean 설정 방법 XML로 설정 할 때에는 아래 처럼 설정합니다. 어노테이션을 이용해서 설정할 때에는 아래처럼 클래스 위에 @Configuration 어노테이션을 설정하고, 메서드 위에는 @Bean 어노테이션을 설정해 줍니다. 패키지 경로는 net.aacii.app 으로 가정합니다. import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import net.aacii.app.service.SampleService; import ne..
1. 인코딩(Encoding) 1.1. 파일 및 문자 인코딩은 UTF-8 (gerneral_ci)으로 통일한다. 2. 이름(Naming) 2.1. 변수명, 클래스명, 인터페이스명, 메서드명에는 영어와 숫자만 사용한다. public interface AutoClosable{ 2.2. 변수명, 클래스명, 인터페이스명, 메서드명 단어 합성시에는 낙타 등 표기법으로 한다. public interface RowMapper{ 2.3. 클래스명, 인터페이스명, 생성자명은 대문자로 시작한다. public class WatcherClass{ 2.4. 변수명과 메서드명은 소문자로 시작해야한다. public String setHtmlName(String mode){ 2.5. 상수명은 모두 대문자로 하고 단어를 합성할 때는 ..
java.util.concurrent.TimeUnit 패키지 TimeUnit은 멀티 Thread가 아닌 메서드에서 시간을 지연 시킬 때 사용합니다. 예제 import java.util.concurrent.TimeUnit; public class SimpleTest { public static void main(String[] args) { try { for (int i = 0; i < 3; i++) { TimeUnit.SECONDS.sleep(2); System.out.println("Sleep "+i+" sec"); } }catch(Exception e) { System.out.println(e); } } } 자세한 api 문서는 아래 참고 https://docs.oracle.com/javase/7/d..
Gradle 빌드 도구 빌드 도구는 컴파일, 라이브러리 다운로드, 패키징, 테스팅 등을 자동화 해주는 도구를 말합니다. 프로젝트가 커지면 라이브러리가 많아지므로 전체를 빌드하기에는 비효율 적이므로 빌드를 나눠서 해야합니다. gradle은 이런 컴파일, 빌드, 유닛 테스트 등을 코드(그루비 언어)로 관리해줍니다. build.gradle plugins { id 'org.springframework.boot' version '2.6.12-SNAPSHOT' id 'io.spring.dependency-management' version '1.0.13.RELEASE' id 'java' } group = 'ds.co.kr' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1..
스프링 부트(Spring boot) 기존 스프링 프레임워크는 세팅이 매우 복잡하기 때문에 세팅을 자동으로 해주는 스프링 부트가 등장했습니다. 스프링 부트 덕분에 Stand-alone 어플리케이션을 쉽게 구동가능해졌습니다. 기존에는 자바 웹 어플리케이션을 WAR 파일로 압축해서 WAS에 배포했었고 다수의 WAR 파일을 동시 구동할 수 있어서 stand-alone이 아닙니다. 스프링 부트는 내장된 임베디드 WAS(tomcat, jetty 등)로 구동하게 됩니다. IDE 는 전자정부프레임워크 4.0 이상이나 STS 에서 스프링 부트를 지원합니다. DispatcherServlet 기존 웹 어플리케이션 개발은 HttpServlet을 상속하는 클래스를 만들고 doGet()이나 doPost()메서드를 구현하고 Htt..
Download of Code Without Integrity Check 무결성 체크를 하지 않은 코드 다운로드 원격으로부터 소스 코드 또는 실행파일을 무결성 검사 없이 다운받고 실행하게되면 host 서버의 변조, DNS spoofing또는 전송시 코드 변조를 통해 악의적인 코드를 실행할 수 있습니다. 방어 방법 자동 업데이트 처럼 다운로드 될 코드를 제공할 때는 코드에 대한 암호화된 시그니처를 사용하고 클라이언트가 시그니처를 검증하도록 합니다. 안전하지 않은 코드의 예 URL[] classURLs= new URL[]{new URL("file:subdir/")}; URLClassLoader loader = new URLClassLoader(classURLs); Class loadedClass = Clas..
Unsafe Reflection 안전하지 않은 리플렉션 동적 클래스 loading시 외부의 입력을 사용할 경우, 공격자가 외부 입력을 변조하여 의도하지 않은 클래스가 로딩되도록 할 수 있습니다. 방어 방법 외부의 입력을 직접 클래스 이름으로 사용하지 말고, white 리스트에서 클래스 이름을 선택하도록 합니다. 안전하지 않은 코드의 예 String type = props.getProperty("type"); Worker w; try{ Class workClass = Class.forName(type + "Worker"); w = (Worker) workClass.newInstance(); w.doAction(); }catch (ClassNotFoundException e) { …… } 5라인 에서 외부입..
Process Control 프로세스 제어 정의 신뢰되지 않는 소스나 환경에서 라이브러리를 적재하거나 명령을 실행하면 악의적인 코드가 실행될 수 있습니다. 방어 방법 라이브러리를 적재할 때 절대 경로를 사용합니다. 안전하지 않은 코드의 예 public void loadLibrary() throws Exception{ //외부 라이브러리 호출 시 절대 경로를 사용하지 않고 있습니다. Runtime.getRuntime().loadLibrary("libraryName"); } 안전한 코드의 예 public void loadLibrary() throws Exception{ //외부 라이브러리 호출 시 절대 경로를 지정합니다. Runtime.getRuntime().loadLibrary("/usr/lib/libra..
Eval Injection 동적으로 생성되어 수행되는 명령어 주입 정의 외부 입력이 동적으로 스크립트나 명령어 생성에 사용될 경우 의도하지 않은 명령어가 외부부터 주입되어 문제를 일으킬 수 있습니다. 방어 방법 외부의 입력이 eval()함수의 인자로 사용될 경우 위험 문자를 제거합니다. ESAPI for javascript 등 보안 API를 이용하여 외부에서 입력되는 값을 검증한 후 사용합니다. 안전하지 않은 코드의 예 외부 입력 evalParam이 eval()함수의 인자로 사용되고 있습니다. 안전한 코드의 예