The authentication method has been integrated with every REST calls in the API. I have been trying to implement an authentication method via Spring AOP so that I can remove all the duplicate code from end-points and have one single advise to look for all public methods in Controllers.
Please check the below my code,
@Aspect
public class EndpointAccessAspect {
/**
* All the request mappings in controllers need to authenticate and validate end-point access
*/
@Before("execution(public * com.xxxx.webapi.controllers.MenuController.getCategory(HttpServletRequest)) && args(request)")
public void checkTokenAccess(HttpServletRequest request){
String re =(String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
System.out.println(" %%%%%%%%%%%%%%%%%%% checkTokenAccess %%%%%%%%%%%%%%%" + re);
}
public void checkEndPointPermission(){
System.out.println(" $$$$$$$$$$$$$$$$$$ checkEndPointPermission &&&&&&&&&&&&&");
}
}
However, I saw Intelij gives error near getCategory(HttpServletRequest)) && args(request)
saying can not resolve symbol HttpServletRequest. I need the request to distingues each REST end-points. There are more variables than HttpServletRequest variable in the method but only that variable is needed.
The code is compiling when I test the functionality I noticed it doesn't reach to the advise. Can anybody help me to fix this? I found this from Spring documentation Spring doc
any join point (method execution only in Spring AOP) which takes a single parameter, and where the argument passed at runtime is Serializable
Does this mean I can not use methods that have multiple parameters?
Controller end-point
@RequestMapping(value = "{menuId}/categories/{categoryId}", method = RequestMethod.GET)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successful retrieval of a category requested", response = ProductGroupModel.class),
@ApiResponse(code = 500, message = "Internal server error") })
public ProductGroupModel getCategory(
@ApiParam(name = "menuId", value = "Numeric value for menuId", required = true) @PathVariable(value = "menuId") final String menuId,
@ApiParam(name = "categoryId", value = "Numeric value for categoryId", required = true) @PathVariable(value = "categoryId") final String categoryId,
final HttpServletRequest request) {
HttpServletRequest
. – Abhijit Sarkar