я определил этот класс аспект, который работает отлично за услуги (не разрешает доступ, если пользователь не имеет роли MANAGER, но нет restricton для контроллера (?)Spring AOP срез точек в контроллере
@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);
}
}
}
и 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;
}
}
Когда я вошел в UserRole.MANAGER метод ранее (JoinPoint _jp) вызывается, в противном случае не
Я вижу, метод перед тем (JoinPoint _jp) не вызывается, когда я просто вставить URL в браузере .... http://127.0.0.1:7001/devices/manage/import.do
Вы отметили вопрос с помощью «spring-security», так почему бы вам не использовать Spring безопасности для этого? – zeroflagL
Просто советую - я бы подумал о том, чтобы скрывать имена пакетов вашей компании в любом примерном коде, который вы предоставляете. –
Не ответ, а просто другое предложение - поскольку ваши названные pointcuts находятся в том же классе, что и ваш метод '@ Before', вам не нужно полностью квалифицировать их в выражении pointcut -' @Before ("handleImportController() || handleDeviceServiceMethod() || и т. д.) должен работать и быть более кратким/читаемым. –