I have defined this class aspect that is working fine for the services (don't allow access unless the user has the role MANAGER, but there is no restricton for the controller (?)
@Aspect
public class DeviceAspect extends ServiceSupport {
@Pointcut("execution(* fo.belecam.services.client.ManageLicenseService.*(..))")
public void manage() {
}
@Pointcut("execution(* fo.belecam.services.client.AwardService.*(..))")
public void award() {
}
@Pointcut("execution(* fo.belecam.services.client.DeviceService.*(..))")
public void handleDeviceServiceMethod() {
}
@Pointcut("execution(* fo.belecam.controller.manage.ImportController.*(..))")
public void handleImportController() {
}
@Before("fo.belecam.services.aop.DeviceAspect.handleImportController() || fo.belecam.services.aop.DeviceAspect.handleDeviceServiceMethod() || fo.belecam.services.aop.DeviceAspect.manage() || fo.belecam.services.aop.DeviceAspect.award()")
@After ("fo.belecam.services.aop.DeviceAspect.handleImportController() || fo.belecam.services.aop.DeviceAspect.handleDeviceServiceMethod() || fo.belecam.services.aop.DeviceAspect.manage() || fo.belecam.services.aop.DeviceAspect.award()")
public void before(JoinPoint _jp) {
User user = getUser();
if(user == null || user.getUserRole() != UserRole.MANAGER) {
throw new NoSufficientRoleException(user == null ? null : user.getUserRole(), UserRole.MANAGER);
}
}
}
and the ImportController:
@SuppressWarnings("deprecation")
public class ImportController extends AbstractFormController {
private String view;
private String successView;
@Autowired
protected UserService userService;
@Autowired
private ManageDeviceService manageDeviceService;
public String getView() {
return view;
}
public void setView(String view) {
this.view = view;
}
public String getSuccessView() {
return successView;
}
public void setSuccessView(String successView) {
this.successView = successView;
}
@Override
public ModelAndView processFormSubmission(final HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
final ModelAndView mav = new ModelAndView(getView());
FileUploadCommand file = (FileUploadCommand)command;
MultipartFile multipartFile = file.getFile();
if(multipartFile!=null && multipartFile.getSize()>0) {
Workbook workbook = Workbook.getWorkbook(multipartFile.getInputStream());
DataCollector dataCollector = new XLSDataCollector(workbook, true);
final List<Application> applications = manageDeviceService.loadApplications (dataCollector.getDataCollection());
List<ApplicationImporterError> importationErrors = manageDeviceService.validateApplications(applications);
savedApplications.add(manageDeviceService.getApplicationById(application.getId(), true));
}
return mav;
}
@Override
public ModelAndView showForm(HttpServletRequest request, HttpServletResponse arg1, BindException errors)
throws Exception {
return new ModelAndView(getView());
}
}
/**
* @param applications
* @param competentBody
* @return
* @throws Exception
*/
private List<Application> saveApplications(List<Application> applications,User user) throws Exception {
return manageDeviceService.saveImportedApplications (applications, user);
}
/**
* @param session
* @return
*/
public User getUser(HttpSession session) {
User user = (User) session.getAttribute(Const.SESSION_USER);
if (user == null) {
user = new User();
}
return user;
}
}
When I logged as UserRole.MANAGER the method before(JoinPoint _jp) is invoked, otherwise is not
I see, the method before(JoinPoint _jp) is not invoked when I just paste the URL in the browser .... http://127.0.0.1:7001/devices/manage/import.do
@Before
method, you don't need to fully qualify them in the pointcut expression -@Before("handleImportController() || handleDeviceServiceMethod() || etc)
should work and is more concise / readable.@Before
method is actually being invoked, or whether theuser
object is coming back null fromgetEcasUser()
for some reason - in either case, you won't know as there's no action taken unless the user is not in theMANAGER
role.