-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat:temp save * feat:temp save * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: feedback save * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: AppAuthAspect * feat: DesensitizationResponseType --------- Co-authored-by: yushuwang <[email protected]>
- Loading branch information
Showing
21 changed files
with
277 additions
and
21 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
arex-web-api/src/main/java/com/arextest/web/api/service/aspect/AppAuthAspect.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,84 @@ | ||
package com.arextest.web.api.service.aspect; | ||
|
||
import com.arextest.common.annotation.AppAuth; | ||
import com.arextest.common.context.ArexContext; | ||
import com.arextest.common.model.response.ResponseCode; | ||
import com.arextest.common.utils.JwtUtil; | ||
import com.arextest.common.utils.ResponseUtils; | ||
import com.arextest.config.model.dto.application.ApplicationConfiguration; | ||
import com.arextest.config.repository.impl.ApplicationConfigurationRepositoryImpl; | ||
import com.arextest.web.api.service.controller.Constants; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
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.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import javax.annotation.Resource; | ||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.List; | ||
|
||
/** | ||
* @author wildeslam. | ||
* @create 2023/10/8 17:12 | ||
*/ | ||
@Slf4j | ||
@Aspect | ||
@Component | ||
@ConditionalOnProperty(value = "arex.app.auth.switch", havingValue = "true") | ||
public class AppAuthAspect { | ||
@Resource | ||
private ApplicationConfigurationRepositoryImpl applicationConfigurationRepository; | ||
|
||
@Pointcut("@annotation(com.arextest.common.annotation.AppAuth)") | ||
public void appAuth(){} | ||
|
||
@Around("appAuth() && @annotation(auth)") | ||
public Object doAround(ProceedingJoinPoint point, AppAuth auth) throws Throwable { | ||
ArexContext context = ArexContext.getContext(); | ||
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | ||
HttpServletRequest request = requestAttributes.getRequest(); | ||
String appId = request.getHeader("appId"); | ||
String accessToken = request.getHeader("access-token"); | ||
String userName = JwtUtil.getUserName(accessToken); | ||
context.setAppId(appId); | ||
context.setOperator(userName); | ||
if (appId == null) { | ||
LOGGER.error("header has no appId"); | ||
return reject(point, auth, Constants.NO_APPID); | ||
} | ||
List<ApplicationConfiguration> applications = applicationConfigurationRepository.listBy(context.getAppId()); | ||
if (CollectionUtils.isEmpty(applications)) { | ||
LOGGER.error("error appId"); | ||
return reject(point, auth, Constants.ERROR_APPID); | ||
} | ||
ApplicationConfiguration application = applications.get(0); | ||
Object result; | ||
if (CollectionUtils.isEmpty(application.getOwners()) || application.getOwners().contains(userName)) { | ||
context.setPassAuth(true); | ||
result = point.proceed(); | ||
} else { | ||
context.setPassAuth(false); | ||
result = reject(point, auth, Constants.NO_PERMISSION); | ||
} | ||
ArexContext.removeContext(); | ||
return result; | ||
} | ||
|
||
private Object reject(ProceedingJoinPoint point, AppAuth auth, String remark) throws Throwable { | ||
switch (auth.rejectStrategy()) { | ||
case FAIL_RESPONSE: | ||
return ResponseUtils.errorResponse(remark, ResponseCode.AUTHENTICATION_FAILED); | ||
case DOWNGRADE: | ||
ArexContext.getContext().setPassAuth(false); | ||
default: | ||
return point.proceed(); | ||
} | ||
} | ||
|
||
} |
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
74 changes: 74 additions & 0 deletions
74
...-web-api/src/main/java/com/arextest/web/api/service/controller/ReplayQueryController.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,74 @@ | ||
package com.arextest.web.api.service.controller; | ||
|
||
import com.arextest.common.annotation.AppAuth; | ||
import com.arextest.common.context.ArexContext; | ||
import com.arextest.common.enums.AuthRejectStrategy; | ||
import com.arextest.common.model.response.ResponseCode; | ||
import com.arextest.model.replay.ViewRecordRequestType; | ||
import com.arextest.model.replay.ViewRecordResponseType; | ||
import com.arextest.model.response.Response; | ||
import com.arextest.model.response.ResponseStatusType; | ||
import com.arextest.web.common.HttpUtils; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author wildeslam. | ||
* @create 2023/10/12 20:18 | ||
*/ | ||
@Controller | ||
@RequestMapping("/api/replay/query") | ||
public class ReplayQueryController { | ||
|
||
@Value("${arex.storage.viewRecord.url}") | ||
private String viewRecordUrl; | ||
|
||
@ResponseBody | ||
@GetMapping(value = "/viewRecord/") | ||
public Response viewRecord(String recordId, | ||
@RequestParam(required = false) String category, | ||
@RequestParam(required = false, defaultValue = "Rolling") String srcProvider) { | ||
ViewRecordRequestType recordRequestType = new ViewRecordRequestType(); | ||
recordRequestType.setRecordId(recordId); | ||
recordRequestType.setSourceProvider(srcProvider); | ||
recordRequestType.setCategoryType(category); | ||
return viewRecord(recordRequestType); | ||
} | ||
|
||
@PostMapping("/viewRecord") | ||
@ResponseBody | ||
@AppAuth(rejectStrategy = AuthRejectStrategy.DOWNGRADE) | ||
public Response viewRecord(@RequestBody ViewRecordRequestType requestType) { | ||
ArexContext arexContext = ArexContext.getContext(); | ||
Map<String, String> headers = new HashMap<>(); | ||
boolean downgrade = Boolean.FALSE.equals(arexContext.getPassAuth()); | ||
headers.put("downgrade", Boolean.toString(downgrade)); | ||
ResponseEntity<ViewRecordResponseType> response = HttpUtils.post(viewRecordUrl, requestType, | ||
ViewRecordResponseType.class, headers); | ||
ViewRecordResponseType responseType = new ViewRecordResponseType(); | ||
ResponseStatusType responseStatusType = new ResponseStatusType(); | ||
responseStatusType.setTimestamp(System.currentTimeMillis()); | ||
if (response == null || response.getBody() == null) { | ||
responseStatusType.setResponseDesc("call storage failed"); | ||
responseStatusType.setResponseCode(ResponseCode.REQUESTED_RESOURCE_NOT_FOUND.getCodeValue()); | ||
responseType.setResponseStatusType(responseStatusType); | ||
return responseType; | ||
} | ||
|
||
responseStatusType.setResponseDesc("success"); | ||
responseStatusType.setResponseCode(ResponseCode.SUCCESS.getCodeValue()); | ||
responseType = response.getBody(); | ||
responseType.setResponseStatusType(responseStatusType); | ||
return response.getBody(); | ||
} | ||
} |
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
Oops, something went wrong.