728x90
반응형

간단하게 파일확장자를 체크하는 클래스를 만들어보았다. 
기본적으로 확장자의 대소문자를 구분하여 필터링 시킬 확장자를 포함한 파일에 대해 에러 및 flag값을 발생시킨다.
업무적으로 보안취약점 중 "웹쉘 업로드" 부분떄문에 아래와 같은 유틸리티 클래스를 개발 하여 취약점을 해결하였다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.File;
import java.io.IOException;
 
public class FileExtFilter {
 
    /**
     * 파일의 확장자를 체크하여 필터링된 확장자를 포함한 파일인 경우에 예외를 발생한다.
     * @param file
     * */
    public static void badFileExtIsReturnException(File file) {
        String fileName = file.getName();
        String ext = fileName.substring(fileName.lastIndexOf(".") + 1,
                fileName.length());
        final String[] BAD_EXTENSION = { "jsp""php""asp""html""perl" };
 
        try {
            int len = BAD_EXTENSION.length;
            for (int i = 0; i < len; i++) {
                if (ext.equalsIgnoreCase(BAD_EXTENSION[i])) {
                    // 불량 확장자가 존재할떄 IOExepction 발생
                    throw new IOException("BAD EXTENSION FILE UPLOAD");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 파일의 확장자를 체크하여 필터링된 확장자를 포함한 파일인 경우에 true를 리턴한다.
     * @param file
     * */
    public static boolean badFileExtIsReturnBoolean(File file) {
        String fileName = file.getName();
        String ext = fileName.substring(fileName.lastIndexOf(".") + 1,
                fileName.length());
        final String[] BAD_EXTENSION = { "jsp""php""asp""html""perl" };
 
        int len = BAD_EXTENSION.length;
        for (int i = 0; i < len; i++) {
            if (ext.equalsIgnoreCase(BAD_EXTENSION[i])) {
                return true// 불량 확장자가 존재할때..
            }
        }
        return false;
    }
    
 
 
}
 


728x90
반응형
728x90
반응형

문자열을 처리하기 위한 String , StringBufferStringBuilder세가지 클래스 중에서, 가장 메모리를 많이

차지하고 응답시간에 많은 영향을 주는 것은  String 클래스 이다. 만약 사용중인 WAS나 시스템이 JDK5.0 이상을 사용한다면,

컴파일러에서 자동으로 StringBuilder로 변환하여 준다. 하지만 반복 루프를 사용해서 문자열을 더할 때에는 객체를 
계속 추가한다는 사실에는 변함이 없다. 그러므로 String 클래스를 쓰는 대신, 스레드와 관련이 있으면

StringBuffer를, 스레드 안전 여부와 상관이 없으면 StringBuilder를 사용하는 것을 권장한다.

 






728x90
반응형
728x90
반응형

Spring 은 bean객체를 싱글턴으로 인스턴스화 시켰을경우 다중스레드에 대한 처리를 자동적으로 지원해준다. 

우리는 직접 구현한 싱글턴 객체에 대해 bean 에 설정하는방법에 대해서도 알아야한다 . 

 


test01.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


<!-- 싱글턴패턴 클래스타입의 객체를 생성하는 방법 -->

<bean name="single" class="test.single.SingletonDao"

factory-method="getInstance" />

</beans>


SingletonDao.java

package test.single;

// 싱글톤패턴의 DAO

public class SingletonDao {

private static SingletonDao instance = new SingletonDao();

private SingletonDao(){}

public static SingletonDao getInstance() {

return instance;

}

public void insert( String data ) {

System.out.println(data+"를 추가 했습니다.");

}

public void delete( String data ) {

System.out.println(data+"를 삭제 했습니다.");

}

public void select( String data ) {

System.out.println(data+"를 조회 했습니다.");

}

public void update( String data ) {

System.out.println(data+"를 수정 했습니다.");

}

}



TestMain.java

package test.main;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import test.single.SingletonDao;

public class TestMain {

public static void main(String[] args) {

String res = "test/main/test01.xml";

ApplicationContext context = 

new ClassPathXmlApplicationContext(res);

// 싱글톤 객체의 인스턴스 생성

SingletonDao dao = 

(SingletonDao)context.getBean("single");

dao.insert("이상훈");

}


}


출력결과

싱글턴 패턴 으로 구현된 클래스는 생성자를 가져올수 없다(private) 이기 떄문에 

그래서 인스턴스를 구현한 getter 메서드를 factory-method 라는 속성으로 getter 메소드를 가져올 수 있다.


728x90
반응형
728x90
반응형

Spring 은 경량 컨테이너 이다. 

컨테이너는 컴포넌트들의 라이프사이클을 관리하는 걸 말하는데 예를 들어 톰캣,스윙(JPanel) 등...

Spring도 마찬가지로 초기화작업이나 소멸시에 작동할 작업을 설정을 통해 미리 지정해주는것이 가능하다. 


LifeCycleBean.java

package test.life;

public class LifeCycleBean {

public void init() {

System.out.println("초기화 메소드 입니다.");

}

public void execute() {

System.out.println("요청 작업을 수행합니다.");

}

public void destroy() {

System.out.println("객체가 소멸될때 호출되는 메소드입니다.");

}

}


test01.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 

객체가 생성될떄 init 메소드가 호출되고 객체가 소멸될때 destroy 메소드가 호출된다. 

init-method : 객체 생성시 호출되는 메소드 지정 (초기화작업)

destroy-method : 객체소멸시 호출되는 메소드 (자원해제등의 작업)

-->

<bean name="life" class="test.life.LifeCycleBean"

init-method="init" destroy-method="destroy"

/>

</beans>



scope 속성을 이용하여 LifeCycle 을 관리할 수 있다. 

init-method : 객체 생성시 호출되는 메소드 지정 (초기화작업)

destroy-method : 객체소멸시 호출되는 메소드 (자원해제등의 작업)


728x90
반응형

+ Recent posts