스프링 AOP(Aspect Oriented Programming) 로그인 설정

Posted by HULIA(휴리아)
2016. 8. 19. 00:47 백엔드개발/자바스프링

스프링에 AOP(Aspect Oriented Programming) 설정을 통해서 로그인 세션을 처리해 보는 로직을 구현해 보고자 한다


AOP(Aspect Oriented Programming)는 스프링에서 Interceptor와 비슷한 기능을 가진 기능이다


Interceptor는 서블릿 기반으로써 서블릿 실행 전, 실행 후 등으로 그때마다 어떤일을 할지에 대해서 정할 수 있다 

그리고 주소(url)로만 대상을 지정할 수 있다


그에 반해 AOP(Aspect Oriented Programming)는 메소드 기반으로써 메소드 이전, 메소드 이후 등으로 그때마다 어떤일을 할지에 대해서 정할 수 있다.

그리고 주소(url) 뿐만 아니라 파리미터, 어느테이션 등 다양한 방법으로 대상을 지정할 수 있다.



AOP(Aspect Oriented Programming) 구성요소

-어드바이스(advice)

-포인트컷(point cut)

-조인 포인트(join point)




어드바이스(advice)

작업에 대해서 언제 그 작업을 수행해야 하는지 정의를 해준다

-before : 메소드 호출되기 전에

-after : 메소드 호출되고 난 후

-after-returning : 메소드가 성공적으로 완료된 후

-ater-throwing : 메소드가 예외를 던진 후



포인트 컷

어떤 클래스의 어느 조인포인트를 사용할 것 인지 정의를 해준다



조인포인트

작업에 대해서 동작할 수 있는 실행 가능한 특정위치




AOP(Aspect Oriented Programming) 필요라이브러리

-asm-3.3.1.jar

-aspectjweaver-1.6.11.jar

-cglib-2.2.jar

-aopalliance-1.0.jar





AOP(Aspect Oriented Programming) 스프링 설정(xml파일)

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

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


<aop:aspectj-autoproxy />

<bean id="LoginAspect" class="com.my.app.main.aspect.LoginAspect">

</bean>




AOP(Aspect Oriented Programming) 클래스(java파일)

@Aspect

public class LoginAspect{

@Pointcut("execution(public * com.my.app.main.controller.*Controller.*(..))")

private void test(){}



@Around(value="test()")

public Object trace(ProceddingJoinPoint joinPoint) throws Throwable{


HttpServletRequest request= null;

for(Object o:joinPoint.getArgs()){

if(o instanceof HttpServletRequest){

request = (HttpServletRequest) o;

}

}


//로그인을 체크해야 하는 페이지

if(reqeust!= null){

String strUrl = request.getRequestURL().toString();



//로그인 페이지 제외

if(!strUrl.endsWith("/login")){

//세션체킹

HttpSession session = request.getSession();


String loginId = (String) session.getAttribut("LoginId");

if(loginId == null || "".equals(loginId)){

return "로그인페이지";

}

}

}//request가 null이 아니라면


//controller에서 httprequest가 없거나 /login페이지라면

Object result = joinPoint.proceed();

return result;

}

}




AOP(Aspect Oriented Programming) 또 다른 방식의 스플링 설정(xml파일)

<bean id="guide" class="com.my.aspect"></bean>

<aop:config>

<aop:aspect id="guideAspect" ref="guide">

<!-- 실행하게될 범위를 설정한다-->

<aop:pointcut expression="execution(public * com.my.do..*(..))" id="publicMethod" />



<!-- 실행되는 모든 시점-->

<aop:around method="guide" pointcut-ref="publicMethod" />

</aop:aspect>

</aop:config>


<!-- execution 예제들 -->

<!--리턴타입 void 메소드 이름이 set으로 시작하고 파라미터가 0개 이상인 메소드-->

<aop:pointcut expression="execution(public void set*(..))" />


<!--com.my.test패키지의 파라미터가 없는 모든 메소드 호출-->

<aop:pointcut expression="execution(* com.my.test.*.*())" />


<!--com.my.test 패키지 또는 하위의 패키지에 있는 파라미터가 0개 이상인 메소드-->

<aop:pointcut expression="execution(* com.my.test..*.*(..))" />


<!--리턴타입이 String인 kkk인터페이스의 getSomething()메소드 호출-->

<aop:pointcut expression="execution(String com.my.test.kkk.getSomething())" />


<!--이름이 get으로 시작하고 1개의 파라미터를 갖는 메소드 호출-->

<aop:pointcut expression="execution(* get*(*))" />


<!--이름이 get으로 시작하고 2개의 파라미터를 갖는 메소드 호출-->

<aop:pointcut expression="execution(* get*(*,*))" />


<!--이름이 read로 시작하고, 첫번째 파라미터 타입이 Integer이며, 1개이상의 파라미터를 갖는 메소드 호출-->

<aop:pointcut expression="execution(* read(Integer,..))"  />