-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 新增访问私有资源拦截注解,给需要的接口添加登陆校验注解 (#71)
* feat: 新增访问个人私有资源拦截注解 * feat: 给请求加上登陆校验注解和访问个人私有资源拦截注解 * fix: 重构fromId为userId * fix: 增加优先级概念,管理员鉴权放在登陆校验和个人私有资源校验之后
- Loading branch information
1 parent
9c49651
commit 7a9291d
Showing
13 changed files
with
141 additions
and
7 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
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
79 changes: 79 additions & 0 deletions
79
polaris-app/src/main/java/com/achobeta/aop/SelfPermissionVerificationAspect.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,79 @@ | ||
package com.achobeta.aop; | ||
|
||
import com.achobeta.domain.login.model.valobj.TokenVO; | ||
import com.achobeta.types.enums.GlobalServiceStatusCode; | ||
import com.achobeta.types.exception.AppException; | ||
import lombok.extern.slf4j.Slf4j; | ||
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.core.annotation.Order; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* @Author: 严豪哲 | ||
* @Description: 访问个人私有资源权限拦截器 | ||
* @Date: 2024/11/27 21:40 | ||
* @Version: 1.0 | ||
*/ | ||
|
||
@Slf4j | ||
@Component | ||
@Aspect | ||
@Order(Integer.MIN_VALUE+1) | ||
public class SelfPermissionVerificationAspect { | ||
|
||
private final String TOKENINFO = "tokenInfo"; | ||
|
||
/** | ||
* 拦截入口 | ||
*/ | ||
@Pointcut("@annotation(com.achobeta.types.constraint.SelfPermissionVerification)") | ||
public void pointCut(){ | ||
} | ||
|
||
/** | ||
* 拦截处理 | ||
* @param joinPoint joinPoint 信息 | ||
* @return result | ||
* @throws Throwable if any | ||
*/ | ||
@Around("pointCut()") | ||
public Object checkToken(ProceedingJoinPoint joinPoint) throws Throwable { | ||
|
||
//获取当前请求信息 | ||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | ||
HttpServletRequest request = attributes.getRequest(); | ||
|
||
//获取token信息 | ||
TokenVO tokenVO = (TokenVO) request.getAttribute(TOKENINFO); | ||
|
||
//正常不会进到这 因为登陆校验在本校验之前 | ||
if(tokenVO == null || tokenVO.getUserId() == null){ | ||
log.info("登陆校验未通过,tokenInfo为空,无法获取userId"); | ||
throw new AppException(String.valueOf(GlobalServiceStatusCode.LOGIN_UNKNOWN_ERROR.getCode()), GlobalServiceStatusCode.LOGIN_UNKNOWN_ERROR.getMessage()); | ||
} | ||
|
||
//这里如果再从redis里面获取token信息,token可能过期失效,所以这里不获取用登录校验处传来的 | ||
String tokenUserId = String.valueOf(tokenVO.getUserId()); | ||
|
||
// 获取用户ID | ||
Object arg = joinPoint.getArgs()[0]; | ||
String targetUserId = (String) arg.getClass().getMethod("getUserId").invoke(arg); | ||
|
||
// 校验用户ID是否相同 | ||
if (tokenUserId.equals(targetUserId)) { | ||
log.info("当前用户访问的是个人私有资源,用户id相同,可以放行,userId:{}",tokenUserId); | ||
return joinPoint.proceed(); | ||
} else { | ||
log.info("当前用户访问的是个人私有资源,用户id不相同,不可以放行,userId:{}",tokenUserId); | ||
throw new AppException(String.valueOf(GlobalServiceStatusCode.USER_NO_PERMISSION.getCode()), GlobalServiceStatusCode.USER_NO_PERMISSION.getMessage()); | ||
} | ||
|
||
} | ||
} |
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
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
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
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
17 changes: 17 additions & 0 deletions
17
polaris-types/src/main/java/com/achobeta/types/constraint/SelfPermissionVerification.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,17 @@ | ||
package com.achobeta.types.constraint; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @Author: 严豪哲 | ||
* @Description: 访问个人私有资源权限注解 | ||
* @Date: 2024/11/27 21:40 | ||
* @Version: 1.0 | ||
*/ | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface SelfPermissionVerification { | ||
} |