Rev 2009 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2009 | Rev 2049 | ||
---|---|---|---|
Line 7... | Line 7... | ||
7 | import org.aspectj.lang.annotation.Pointcut; |
7 | import org.aspectj.lang.annotation.Pointcut; |
8 | import pt.estgp.es.exemplos.hibernate.HibernateUtils; |
8 | import pt.estgp.es.exemplos.hibernate.HibernateUtils; |
9 | import pt.estgp.es.exemplos.hibernate.UserSession; |
9 | import pt.estgp.es.exemplos.hibernate.UserSession; |
10 | import pt.estgp.es.exemplos.hibernate.security.SecurityContextProvider; |
10 | import pt.estgp.es.exemplos.hibernate.security.SecurityContextProvider; |
11 | 11 | ||
- | 12 | import java.util.Arrays; |
|
- | 13 | ||
12 | 14 | ||
13 | /** |
15 | /** |
14 | * Created by jorgemachado on 18/10/18. |
16 | * Created by jorgemachado on 18/10/18. |
15 | */ |
17 | */ |
16 | @Aspect |
18 | @Aspect |
17 | public class SecurityAspects |
19 | public class SecurityAspects |
18 | { |
20 | { |
19 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger= 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(SecurityAspects.class); |
- | |
20 | - | ||
21 | 21 | ||
- | 22 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(SecurityAspects.class); |
|
22 | //Defines a pointcut that we can use in the @Before,@After, @AfterThrowing, @AfterReturning,@Around specifications |
23 | //Defines a pointcut that we can use in the @Before,@After, @AfterThrowing, @AfterReturning,@Around specifications |
23 | //The pointcut will look for the @YourAnnotation |
24 | //The pointcut will look for the @YourAnnotation |
24 | @Pointcut("@annotation(Transaction)") |
25 | @Pointcut("@annotation(Transaction)") |
25 | public void serviceTransactionPointCutDefinition(){} |
26 | public void serviceTransactionPointCutDefinition(){} |
26 | 27 | ||
- | 28 | ||
27 | @Pointcut("@annotation(IsAuthenticated)") |
29 | @Pointcut("@annotation(IsAuthenticated)") |
28 | public void isAuthenticatedPointCut(){} |
30 | public void isAuthenticatedPointCut(){} |
29 | 31 | ||
30 | @Pointcut("@annotation(hasRole)") |
32 | @Pointcut("@annotation(hasRole)") |
31 | public void hasRolePointCut(HasRole hasRole){} |
33 | public void hasRolePointCut(HasRole hasRole){} |
Line 34... | Line 36... | ||
34 | public void printParameterExamplePointCut(PrintParameterExample printParameterExample){} |
36 | public void printParameterExamplePointCut(PrintParameterExample printParameterExample){} |
35 | 37 | ||
36 | @Pointcut("execution(* *(..))") |
38 | @Pointcut("execution(* *(..))") |
37 | public void executionPointCut(){} |
39 | public void executionPointCut(){} |
38 | 40 | ||
- | 41 | ||
39 | @Around("serviceTransactionPointCutDefinition() && executionPointCut()") |
42 | @Around("serviceTransactionPointCutDefinition() && executionPointCut()") |
40 | public 5+0%2Fdocs%2Fapi+Object">Object envolventeServico(ProceedingJoinPoint pjp) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
43 | public 5+0%2Fdocs%2Fapi+Object">Object envolventeServico(ProceedingJoinPoint pjp) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
41 | { |
44 | { |
42 | HibernateUtils.getCurrentSession().beginTransaction(); |
45 | HibernateUtils.getCurrentSession().beginTransaction(); |
43 | //Sugestão: um motor de serviços poderia neste ponto abrir a transação na base de dados |
46 | //Sugestão: um motor de serviços poderia neste ponto abrir a transação na base de dados |
44 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Iniciando chamada do servico:" + pjp.getSignature().getName() + |
47 | logger.info("Iniciando chamada do servico:" + pjp.getSignature().getName() + |
45 | " na classe " + pjp.getSourceLocation().getClass().getName()); |
48 | " na classe " + pjp.getSourceLocation().getClass().getName()); |
46 | try { |
49 | try { |
47 | 5+0%2Fdocs%2Fapi+Object">Object returnObj = pjp.proceed(); |
50 | 5+0%2Fdocs%2Fapi+Object">Object returnObj = pjp.proceed(); |
48 | //Sugestão: um motor de serviços poderia neste ponto fazer commit da transação na base de dados |
51 | //Sugestão: um motor de serviços poderia neste ponto fazer commit da transação na base de dados |
49 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Terminado chamada do servico"); |
52 | logger.info("Terminado chamada do servico"); |
50 | HibernateUtils.getCurrentSession().getTransaction().commit(); |
53 | HibernateUtils.getCurrentSession().getTransaction().commit(); |
51 | - | ||
52 | return returnObj; |
54 | return returnObj; |
53 | } catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) { |
55 | } catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) { |
54 | HibernateUtils.getCurrentSession().getTransaction().rollback(); |
56 | HibernateUtils.getCurrentSession().getTransaction().rollback(); |
55 | - | ||
56 | //Sugestão: um motor de serviços poderia neste ponto fazer rollback da transação na base de dados |
57 | //Sugestão: um motor de serviços poderia neste ponto fazer rollback da transação na base de dados |
57 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Excepcao no Servico"); |
58 | logger.error("Excepcao no Servico",e); |
58 | throw e; |
59 | throw e; |
59 | } |
60 | } |
60 | } |
61 | } |
61 | 62 | ||
- | 63 | ||
62 | @Around("isAuthenticatedPointCut() && executionPointCut()") |
64 | @Around("isAuthenticatedPointCut() && executionPointCut()") |
63 | public 5+0%2Fdocs%2Fapi+Object">Object isAuthenticatedAdvise(ProceedingJoinPoint pjp) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
65 | public 5+0%2Fdocs%2Fapi+Object">Object isAuthenticatedAdvise(ProceedingJoinPoint pjp) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
64 | { |
66 | { |
- | 67 | logger.info("Is Authenticated Aspect"); |
|
65 | 1.5.0/docs/api/java/lang/String.html">String cookie = SecurityContextProvider.getInstance().getSecuritySessionContext().getRequester(); |
68 | 1.5.0/docs/api/java/lang/String.html">String cookie = SecurityContextProvider.getInstance().getSecuritySessionContext().getRequester(); |
- | 69 | UserSession session = (UserSession) HibernateUtils.getCurrentSession().load(UserSession.class,cookie); |
|
66 | 70 | ||
67 | //Com o cookie ir buscar a sessao e seguidamente o User e ver se esta logado |
- | |
68 | if(true) |
71 | if(session.getUser() != null) |
69 | return pjp.proceed(); |
72 | return pjp.proceed(); |
- | 73 | //Com o cookie ir buscar a sessao e seguidamente o User e ver se esta logado |
|
- | 74 | ||
- | 75 | ||
70 | throw new NotAuthenticatedException("Access Denied, not authenticated at " + pjp.getSourceLocation().getFileName() + " " + pjp.getSourceLocation().getLine() + " service: " + pjp.getSignature().getName()); |
76 | throw new NotAuthenticatedException("Access Denied, not authenticated at " + pjp.getSourceLocation().getFileName() + " " + pjp.getSourceLocation().getLine() + " service: " + pjp.getSignature().getName()); |
71 | } |
77 | } |
72 | 78 | ||
73 | @Around("hasRolePointCut(hasRole) && executionPointCut()") |
79 | @Around("hasRolePointCut(hasRole) && executionPointCut()") |
74 | public 5+0%2Fdocs%2Fapi+Object">Object hasRoleAdvise(ProceedingJoinPoint pjp, HasRole hasRole) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
80 | public 5+0%2Fdocs%2Fapi+Object">Object hasRoleAdvise(ProceedingJoinPoint pjp, HasRole hasRole) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
75 | { |
81 | { |
76 | //String username = SecurityContextProvider.getInstance().getSecuritySessionContext().getUsername(); |
82 | logger.info("Has Role Aspect"); |
77 | 1.5.0/docs/api/java/lang/String.html">String cookie = SecurityContextProvider.getInstance().getSecuritySessionContext().getRequester(); |
83 | 1.5.0/docs/api/java/lang/String.html">String cookie = SecurityContextProvider.getInstance().getSecuritySessionContext().getRequester(); |
- | 84 | UserSession session = (UserSession) HibernateUtils.getCurrentSession().load(UserSession.class,cookie); |
|
- | 85 | ||
- | 86 | 1.5.0/docs/api/java/lang/String.html">String[] rolesIn = hasRole.role().split(","); |
|
- | 87 | if(session.getUser() == null) |
|
- | 88 | throw new NotAuthenticatedException("Access Denied, not authenticated at " + pjp.getSourceLocation().getFileName() + " " + pjp.getSourceLocation().getLine() + " service: " + pjp.getSignature().getName()); |
|
- | 89 | //session.getUser().get |
|
78 | //verificar se o username é um user com o role que está em hasRole.role() |
90 | //verificar se o username é um user com o role que está em hasRole.role() |
79 | /*if(session.getUser() == null){ |
91 | if(session.getUser().getRoles() == null) |
80 | throw new FailRoleException("Access Denied, does not have role " + hasRole.role() + " at " + pjp.getSourceLocation().getFileName() + " " + pjp.getSourceLocation().getLine() + " service: " + pjp.getSignature().getName()); |
92 | throw new NotAuthenticatedException("Access Denied, missing role at " + pjp.getSourceLocation().getFileName() + " " + pjp.getSourceLocation().getLine() + " service: " + pjp.getSignature().getName()); |
- | 93 | ||
- | 94 | 1.5.0/docs/api/java/lang/String.html">String[] roles = session.getUser().getRoles().split(","); |
|
- | 95 | for(1.5.0/docs/api/java/lang/String.html">String checkRole: rolesIn) |
|
81 | }*/ |
96 | { |
82 | boolean pass = false; |
97 | if(1.5.0/docs/api/java/util/Arrays.html">Arrays.asList(roles).contains(checkRole)) |
83 | if(pass) |
98 | { |
84 | return pjp.proceed(); |
99 | return pjp.proceed(); |
- | 100 | } |
|
- | 101 | } |
|
- | 102 | ||
- | 103 | ||
85 | throw new FailRoleException("Access Denied, does not have role " + hasRole.role() + " at " + pjp.getSourceLocation().getFileName() + " " + pjp.getSourceLocation().getLine() + " service: " + pjp.getSignature().getName()); |
104 | throw new FailRoleException("Access Denied, does not have role " + hasRole.role() + " at " + pjp.getSourceLocation().getFileName() + " " + pjp.getSourceLocation().getLine() + " service: " + pjp.getSignature().getName()); |
86 | } |
105 | } |
87 | 106 | ||
88 | @Around("printParameterExamplePointCut(printParameterExample) && executionPointCut()") |
107 | @Around("printParameterExamplePointCut(printParameterExample) && executionPointCut()") |
89 | public 5+0%2Fdocs%2Fapi+Object">Object printParameterExampleAdvise(ProceedingJoinPoint pjp, PrintParameterExample printParameterExample) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
108 | public 5+0%2Fdocs%2Fapi+Object">Object printParameterExampleAdvise(ProceedingJoinPoint pjp, PrintParameterExample printParameterExample) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
90 | { |
109 | { |
91 | 1.5.0/docs/api/java/lang/System.html">System.out.println("PARAMETRO: " + JointPointUtils.getParameter(pjp,printParameterExample.paramName())); |
110 | 1.5.0/docs/api/java/lang/System.html">System.out.println("PARAMETRO: " + JointPointUtils.getParameter(pjp,printParameterExample.paramName())); |
92 | return pjp.proceed(); |
111 | return pjp.proceed(); |
93 | } |
112 | } |
- | 113 | ||
- | 114 | ||
- | 115 | ||
94 | } |
116 | } |