-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from bardia-p/issue65_milestone3
Resolve Issue 65 Login Aspect
- Loading branch information
Showing
6 changed files
with
114 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/com/opinionowl/opinionowl/aspect/LoginAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.opinionowl.opinionowl.aspect; | ||
|
||
import com.opinionowl.opinionowl.controllers.CookieController; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
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.stereotype.Component; | ||
|
||
/** | ||
* Class LoginAspect for defining an aspect for all login checks within APIController and PageController. | ||
*/ | ||
@Aspect | ||
@Component | ||
public class LoginAspect { | ||
|
||
/** | ||
* Method for defining the annotation needsLogin. | ||
* @param needsLogin NeedsLogin object | ||
*/ | ||
@Pointcut("@annotation(needsLogin)") | ||
public void callAt(NeedsLogin needsLogin) { | ||
} | ||
|
||
/** | ||
* Method for checking the type of the NeedsLogin annotation and proceeding with the actual login check through the aspect. | ||
* @param pjp A ProceedingJoinPoint pjp. | ||
* @param needsLogin A NeedsLogin needsLogin. | ||
* @return The type of the aspect annotation | ||
* @throws Throwable An error | ||
*/ | ||
@Around("callAt(needsLogin)") | ||
public Object around(ProceedingJoinPoint pjp, NeedsLogin needsLogin) throws Throwable { | ||
Object[] args = pjp.getArgs(); | ||
HttpServletRequest request = null; | ||
for (Object arg : args) { | ||
if (arg instanceof HttpServletRequest) { | ||
request = (HttpServletRequest) arg; | ||
} | ||
} | ||
if (request == null) { | ||
return "redirect:/"; | ||
} | ||
String res = CookieController.getUsernameFromCookie(request); | ||
if (res == null) { | ||
return getReturnType(needsLogin); | ||
} | ||
return pjp.proceed(); | ||
} | ||
|
||
/** | ||
* Method for getting the return type for the around method. | ||
* @param needsLogin A needsLogin object | ||
* @return Html, string or int type | ||
*/ | ||
public Object getReturnType(NeedsLogin needsLogin) { | ||
if (needsLogin.type().equals("html")) { | ||
return "redirect:/"; | ||
} else if (needsLogin.type().equals("string")) { | ||
return ""; | ||
} else if (needsLogin.type().equals("int")) { | ||
return 400; | ||
} else { | ||
return null; | ||
} | ||
} | ||
} | ||
|
21 changes: 21 additions & 0 deletions
21
src/main/java/com/opinionowl/opinionowl/aspect/NeedsLogin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.opinionowl.opinionowl.aspect; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Interface for the NeedsLogin aspect. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface NeedsLogin { | ||
/* | ||
"html" | ||
"string" | ||
"int" | ||
*/ | ||
String type() default "html"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters