스프링 씨큐리티 권한으로 컨트롤러 접근을 체크하긴 했지만 별도의 권한 체크가 필요 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