0
votes

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) {
1
Apart from the fact that you're trying to reinvent Spring Security, your question doesn't make sense to me. IntelliJ can't give any error in the pointcut definition because a string is a string is a string - it's not compilable code. Perhaps you need to add servlet-api to your classpath to resolve HttpServletRequest.Abhijit Sarkar
servelet-api is already added and I have been using in controllers. If I remove the args then the IDE remove the message. Right now it gives this advice advises no methodnewday
Then explain the statement I saw Intelij gives error near getCategory(HttpServletRequest)) && args(request) saying can not resolve symbol HttpServletRequest and post the actual error. Otherwise, remove it. Show a controller method (the join point).Abhijit Sarkar
@ Abhijit Sarkar when I reduced the arguments to single parameter it works. Now I need to figure how to do this with multiple parametersnewday

1 Answers

0
votes

The following syntax, resolved the above issue. Basically, I had to modify the code to deal with multiple parameters in the advise.

@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);
}