실제 프로젝트에서 AOP가 사용하는 시나리오는 주로 권한 관리, 거래 관리, 보안 관리, 로깅 및 디버깅입니다.
문제는 프로젝트 개발에서 비롯됩니다
최근 프로젝트에서 권한 관리 모듈이 필요합니다. 이전 동료의 접근 방식에 따르면 컨트롤러 계층 호출의 각 인터페이스 전에 논리적 판단을 내리는 것입니다. 이 작업에는 아무런 문제가 없지만 코드 반복 률이 너무 높고 물리적 인 노동입니다. 따라서 제목에 언급 된 바와 같이 Spring AOP를 사용하여 일반 기능의 권한 관리를 달성하기 위해 슬릿 지점을 만들어 프로젝트의 나중에 개발의 확장 성을 줄입니다.
권한 관리를위한 코드 구현 및 구성 파일
코드 수정 정도는 최소한의 코드 수정으로 AOP는 의심 할 여지없이 이상적인 선택입니다. 프로젝트에는 다양한 권한이 있으며 논리 복잡성이 상대적으로 높기 때문에 단계별로 가져갑니다. 권한에는 백엔드 인터페이스에 대한 호출이 포함되므로 저자는 컨트롤러 계층 코드의 섹션을 만들기로 선택하고 탄젠트 포인트는 컨트롤러의 다양한 메소드 블록입니다. 일반 액세스 권한의 경우 실행 표현식을 사용하여 제외합니다.
읽기 전용 관리자 권한 및 포인트 컷 선택의 구현
일반적인 제외를 구현하는 컨트롤러의 경우 저자는 실행 표현 로직 작업을 사용합니다. 읽기 전용 관리자에게는 전역 읽기 권한이 있고 추가, 삭제 및 수정 권한이 있으므로 저자는 점 절단을 사용하여 추가, 삭제 및 수정 방법을 사용합니다. 따라서 현재 표준화 된 방법 이름 지정은 매우 중요합니다. 읽기 전용 관리자가 복잡한 다양한 관리자의 경우 코드에서 특별한 판단을 내릴 수 있습니다. 다음은 Spring AOP의 구성 파일 구성 방법입니다.
<bean id = "userpermissionsAdvice" /> <aop : config> <!-섹션을 정의합니다-> <aop : Aspect id = "authaspect"ref = "usserPermissionSadvice"> <!-Com.thundersoft.metadata.web.controller. com.thundersoft.metadata.web.controller.*. 편집*(..)) 또는 실행 (*com.thundersoft.metadata.web.web.controller.*. edit*(..))) 또는 실행 (*com.thundersoft.metadata.web.controller.*. del*(..)) 또는 실행 (*) com.thundersoft.metadata.web.controller.*. 업데이트*(..)) 또는 실행 (*com.thundersoft.metadata.web.web.controller.*. insert*(..)) 또는 실행 (*com.thundersoft.metadata.web.controller. com.thundersoft.metadata.web.controller.*. down*(..)))) 및 (! execution (*com.thundersoft.metadata.web.controller.findpasswordcontroller.*()) 및! com.thundersoft.metadata.web.controller.homecontroller.*(..)) 및! 실행 (*com.thundersoft.metadata.web.controller.userstatuscontroller.*(..)) 및! com.thundersoft.metadata.web.controller.dashboardcontroller.*(..)) 및! 실행 (*com.thundersoft.metadata.web.controller.maincontroller. PointCut-Ref = "AuthpointCut"/> </aop : Aspect> </aop : config>
읽기 전용 관리자 권한 관리 코드 구현
나는 위에 너무 많이 말했다. 그리고 나는 넌센스를 많이 말하지 않을 것이다. 아래는 읽기 전용 권한 및 다양한 복합 권한을 제어하는 섹션 코드 구현입니다.
/*** AOP 차단 판단은 읽기 전용 관리자 및 해당 복합 관리자에게 수행됩니다. * @param joinpoint 입력 지점. * @throws ioexception * / public void readonly (joinpoint joinpoint)는 ioexception { / ** * 차단 된 메소드를 얻습니다. */ String MethodName = joinpoint.getSignature (). getName (); /*** 차단 된 객체를 가져옵니다. */ Object Object = joinpoint.getTarget (); logger.info ( "권한 관리 AOP, 메소드 이름" + MethodName); httpservletrequest request = ((servletrequestattributes) requestContexTholder.getRequestAttributes (). getRequest (); httpservletresponse reponship = ((servletrequestattributes) requestContexTholder.getRequestAttributes (). getResponse (); 문자열 역할 플래그 = getLoginuserInfor.getLoginusErrole (요청); / *** 슈퍼 관리자*/ if (fermissionsLabeled.super_admin.equals (roleflag)) {return; } / *** 데이터를 권한으로 변경하기위한 읽기 전용 관리자의 판단* / if (hermissionsLabeled.Reader_Admin.equals (roleflag)) {logger.error ( "ReadOnly 관리자가 작동 권한이 없습니다!"); response.sendRedirect (request.getContextPath () + "/auth/readonly"); } / ** * 부서 관리자이며 읽기 전용 관리자입니다. * / if (hermissionsLabeled.dept_Reader_Admin.equals (roleflag)) {if (Object Incastof DepartmentOntroller) {return; } if (usercontroller의 객체 인스턴스) {if (methodName.contains ( "addAdmin")) {response.sendRedirect (request.getContextPath () + "/auth/readonly"); } if (methodName.contains ( "deleteadmin")) {response.sendRedirect (request.getContextPath () + "/auth/readonly"); } if (methodName.contains ( "updateadmin")) {response.sendRedirect (request.getContextPath () + "/auth/readonly"); } if (methodName.contains ( "updateadmin")) {response.sendRedirect (request.getContextPath () + "/auth/readonly"); } if (methodName.contains ( "updateadmin")) {response.sendRedirect (request.getContextPath () + "/auth/readonly"); } 반품; } if (객체 인스턴스의 GroupController) {return; } logger.error ( "부서 관리자 및 읽기 전용 관리자에 대한 작업 권한 없음!"); response.sendRedirect (request.getContextPath () + "/auth/readonly"); } / *** 응용 프로그램 관리자 및 읽기 전용 관리자* / if (hermissionsLabeled.App_Reader_Admin.equals (roleflag)) {if (개체 인스턴스 AppController) {return; } if (AppPolicyController의 개체 인스턴스) {return; } logger.error ( "응용 프로그램 관리자 및 읽기 전용 관리자에 대한 조작 권한 없음!"); response.sendRedirect (request.getContextPath () + "/auth/readonly"); } / *** 부서 관리자 및 응용 프로그램 관리자 및 읽기 전용 관리자* / if (hermissionsLabeled.dept_app_reader_admin.equals (roleflag)) {if (object instanceof departmentof controller) {return; } if (USERCONTROLLER의 개체 인스턴스) {return; } if (객체 인스턴스의 GroupController) {return; } if (AppController의 개체 인스턴스) {return; } if (AppPolicyController의 개체 인스턴스) {return; } logger.error ( "부서 관리자 및 응용 프로그램 관리자 및 읽기 전용 관리자가 작동 권한이 없습니다"); response.sendRedirect (request.getContextPath () + "/auth/readonly"); }}전용 관리자 권한 제어를 통한 포인트 컷 선택
특별 관리자 권한을 갖는 것이 매우 특별하기 때문에 저자는 일반적인 액세스 권한 외에 컨트롤러를 사용합니다. 특별한 상황에서는 코드 로직에서 구현할 수 있습니다. 구성 파일 코드는 다음과 같습니다.
<aop : config> <!-섹션을 정의합니다-> <aop : 측면 id = "authaspect"ref = "userpermissionsAdvice"> <!-진입 점을 정의합니다 (com.thundersoft.metadata.web.web.controller는 호출되기 전에 가로 채 웁니다)-> <aop : pointcut expression = "(* com.thundersoft.metadata.web.controller.*.*(..)) 및 (! execution (*com.thundersoft.metadata.web.controller.findpasswordcontroller. com.thundersoft.metadata.web.controller.selfservicecontroller.*(..)) 및! execution (*com.thundersoft.web.controller.homecontroller.*(..)) 및! execution (*com.thundersoft.metadata.web.controller.dashboard Controller. com.thundersoft.metadata.web.controller.dashboardcontroller.*(..)) 및! execution (*com.thundersoft.metadata.web.controller.dashboardcontroller.*()))) 및! id = "appauthpointcut"/> <!-메소드가 호출되기 전에 실행-> <aop : 이전 메소드 = "appdeptauth"pointcut-Ref = "appauthpointcut"/> </aop : 측면> </aop : config>
## 허가 관리의 주소 코드 구현
/*** 응용 프로그램 관리자 및 부서 관리자에 대한 임의의 차단 판단. * @param joinpoint 입력 지점. * @throws ioexception * / public void appdeptauth (joinpoint joinpoint)는 ioexception { / ** * 가로 채기 된 메소드를 얻습니다. */ String MethodName = joinpoint.getSignature (). getName (); /*** 차단 된 객체를 가져옵니다. */ Object Object = joinpoint.getTarget (); logger.info ( "권한 관리 AOP, 메소드 이름", MethodName); httpservletrequest request = ((servletrequestattributes) requestContexTholder.getRequestAttributes (). getRequest (); httpservletresponse reponship = ((servletrequestattributes) requestContexTholder.getRequestAttributes (). getResponse (); 문자열 역할 플래그 = getLoginuserInfor.getLoginusErrole (요청); / *** 슈퍼 관리자*/ if (fermissionsLabeled.super_admin.equals (roleflag)) {return; } / *** 데이터를 변경하기위한 응용 프로그램 관리자의 판단* / if (hermissionslabeled.app_admin.equals (roleflag)) {if (Object Incastof AppController) {return; } if (AppPolicyController의 개체 인스턴스) {return; } logger.error ( "응용 프로그램 관리자는 작동 권한이 없습니다"); response.sendRedirect (request.getContextPath () + "/auth/readonly"); } else if (hermissionSlabeled.dept_admin.equals (roleflag)) {if (object instanceof departmentcontroller) {return; } if (USERCONTROLLER의 개체 인스턴스) {return; } if (객체 인스턴스의 GroupController) {return; } if ( "getAllDePartments".Equals (MethodName)) {return; } logger.error ( "응용 프로그램 관리자는 작동 권한이 없습니다"); response.sendRedirect (request.getContextPath () + "/auth/readonly"); } else {return; }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.