소스는 돌아가지 않아요.. ^^ 대략적으로 이렇다~

// 파일정보 전달 Class
public class FileCommand {
    private String originalFileNm;
    private String storedFileNm;
    private Long fileSize;
    private String fileExtension;
    private byte[] fileInfo;
}

// MultipartFile 형식으로 업로드 받은 메서드
public void fileUpload(MultipartFile uploadFile) {

	FileCommand fileCommand = new FileCommand();

    // 확장자
    String fileExtension = "." + FilenameUtils.getExtension(uploadFile.getOriginalFilename());

    // 저장파일명
    String localDateTimeNow = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
    String storedFileNm = localDateTimeNow + fileExtension;
    agreeFileCommand.setOriginalFileNm(uploadFile.getOriginalFilename()); // 원본 파일명
    agreeFileCommand.setStoredFileNm(storedFileNm); // 저장파일명
    agreeFileCommand.setFileSize(uploadFile.getSize()); // 파일 사이즈
    agreeFileCommand.setFileExtension(fileExtension); // 파일 확장자
    agreeFileCommand.setFileInfo(uploadFile.getBytes()); // 파일정보

    // @PostMapping - api 호출 공통
    uploadApi(@RequestBody FileCommand command);
}


// 파일 수신 API
@PostMapping("fileSave")
public void fileSave(@RequestBody FileCommand file) {
    File file = fileSave(file.getFilePath(), file.getFileNm(), file.fileInfo() );
}


/** 파일 업로드 */
public File fileSave(String filePath, String fileNm, byte[] fileInfo) {

    // 파일업로드 처리
    File tmpFile = new File(filePath + fileNm);
    try {
        FileUtils.copyToFile(new ByteArrayInputStream(fileInfo), tmpFile);
    } catch (IOException e) {
        log.error("동의서파일 저장실패 : " + filePath + storedFileNm);
    }
    return new File(filePath + storedFileNm);
}

스프링 씨큐리티 권한으로 컨트롤러 접근을 체크하긴 했지만 별도의 권한 체크가 필요 AOP를 사용하기로 했습니다.

* 조건 *
1. 씨큐리티 권한이 아닌 다른 조건으로 권한 체크 필요
2. 컨트롤러 중 일부 컨트롤러에만 적용 필요
3. 컨트롤러에 어노테이션으로 해당 컨트롤로 권한을 체크 하기로.

1. 어노테이션 인터페이스 생성

package chsoft.frontend.baseprj.framework.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * 사용자 체크용 ApiController Annotation
 * 	- OO고객인 경우 확인하여 접근 금지.
 * 	- 사용예1) @DemoUserCheck
 *             	- "OO고객은 사용하실 수 없는 기능입니다." 기본 메시지 출력
 *  - 사용예2) @DemoUserCheck(message="메시지")
 *  			- message 내용 출력
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
public @interface DemoUserCheck {
	String message() default "OO고객은 사용하실 수 없는 기능입니다.";
}

2.  어노테이션 체크 AOP

/**
 * DemoUserCheck 어노테이션 체크 AOP
 *	- OO회원은 접근 할 수 없도록 체크 합니다.
 */
@Aspect
@Component
public class DemoUserCheckAspect {
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired CommonService commonService;
	
	@Before("@annotation(chsoft.frontend.baseprj.framework.annotation.DemoUserCheck)")
	public void demoUserCheckAnnotation(JoinPoint joinPoint) {
		
		// 어노테이션 정보 획득
		MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
		DemoUserCheck demoUserCheck = methodSignature.getMethod().getAnnotation(DemoUserCheck.class);
		
		if( demoUserCheck != null ) {
        	if (1==1) {
            	//강제에러..
				throw new chsoft403ForbiddenException("권한이 없습니다.");
            }
		}	
		
	}
}

 

3. 컨트롤러 적용

	/** 등록 */
	@DemoUserCheck
	@PreAuthorize("hasRole('ㅋㅋㅋㅋㅋ')")
	@PostMapping(value="/등록해보자")
	public UserVO insertData .....

 

 

 

+ Recent posts