Spring에서 AOP를 사용하면 여러가지 기능을 구현할 수 있다-
가장 간단하게 로그인 처리하는 부분을 남기려 한다-
1. pom.xml에 다음을 추가한다-
1
2
3
4
5
6
7
8
9
10
11 | <!-- AOP 관련 -->
<dependency>
<groupid>org.aspectj</groupid>
<artifactid>aspectjweaver</artifactid>
<version>1.7.4</version>
</dependency>
<dependency>
<groupid>cglib</groupid>
<artifactid>cglib</artifactid>
<version>2.2.2</version>
</dependency>
|
2. Aspect.java
pointcut, advice, joinpoint 구현(?)
*req.getRequestURI()를 통해 특정 uri의 통과를 구현할 수 있다-
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 | import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.web.servlet.ModelAndView;
@Aspect
public class AdminAspect {
@Pointcut("bean(adminController) || bean(adminStudentController)")
public void adminOperation(){}
@Around("adminOperation()")
public Object aroundAnyAdminOperation(ProceedingJoinPoint joinPoint) throws Throwable {
HttpServletRequest req = null;
for (Object obj : joinPoint.getArgs()) {
if (obj instanceof HttpServletRequest) {
req = (HttpServletRequest) obj;
}
}
String connUri = req.getRequestURI();
System.out.println(connUri);
boolean isPass = false;
boolean isLogined = false;
HttpSession session = req.getSession();
String adminLogined = (String)session.getAttribute("adminLogined");
if(adminLogined != null || !adminLogined.equals("")){
isLogined = true;
}else if(connUri.startsWith("/login")){
isPass = true;
}
if(!isLogined || !isPass){
ModelAndView mav = new ModelAndView("/admin_login");
session.setAttribute("error", "관리자 로그인 후 이용 가능합니다.");
return mav;
}else{
Object result = joinPoint.proceed();
return result;
}
}
}
|