実際のプロジェクトでAOPが使用するシナリオは、主に権限管理、取引管理、セキュリティ管理、ロギング、デバッグです。
問題はプロジェクト開発に起因します
最近、プロジェクトでは許可管理モジュールが必要です。以前の同僚のアプローチによると、コントローラーレイヤーコールの各インターフェイスの前に論理的な判断を下すことです。これを行うことには何の問題もありませんが、コードの繰り返し率が高すぎて、それは身体的な労働です。したがって、タイトルで述べたように、Spring AOPを使用してスリットポイントを作成して、一般的な機能の許可管理を実現し、プロジェクトの後の開発のスケーラビリティを低下させます。
許可管理のためのコード実装および構成ファイル
コード変更の最小度があるため、AOPは間違いなく理想的な選択です。プロジェクトにはさまざまな許可があり、ロジックの複雑さは比較的高いため、段階的に進みます。許可にはバックエンドインターフェイスへの呼び出しが含まれるため、著者はコントローラーレイヤーコードにセクションを作成することを選択し、接線点はコントローラー内のさまざまなメソッドブロックです。一般的なアクセス許可については、実行式を使用してそれらを除外します。
読み取り専用の管理者許可とポイントカット選択の実装
一般的な除外を実装するコントローラーの場合、著者は実行式ロジック操作を使用します。読み取り専用の管理者にはグローバルな読み取り権限があり、追加、削除、変更の権限のために、著者はポイントカットを使用して追加、削除、変更の方法を使用します。したがって、現時点では標準化された方法の命名は非常に重要です。読み取り専用の管理者と複合されたさまざまな管理者の場合、コードで特別な判断を下すことができます。以下は、スプリングAOPの構成ファイル構成方法です。
<bean id = "userspermissionsadvice" /> <aop:config> <! - define section-> <aop:aspect id = "authaspect" ref = "userspermissionsadvice"> <! - エントリポイントを定義します(com.thundersoft.metadata.web.controller。 com.thundersoft.metadata.web.controller。*。編集*(..))または実行(*com.thundersoft.metadata.web.controller。 com.thundersoft.metadata.web.controller。*。更新*(..))または実行(*com.thundersoft.metadata.web.controller。 com.thundersoft.metadata.web.controller。*。ダウン*(..)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))および(! !execution(* com.thundersoft.metadata.web.controller.homecontroller。*(..))および!execution(* com.thundersoft.metadata.web.controller.userstatusController。 com.thundersoft.metadata.web.controller.userstatusController。*(..)))およびid = "authpointcut"/> <! - メソッドが呼び出される前に実行 - > <aop:method = "readonly" pointcut-ref = "authpointcut"/> </aop:aspect> </aop:config>
読み取り専用管理者許可管理コードの実装
私は上記のことをたくさん言ったが、それほどナンセンスは言うことはない。以下は、読み取り専用の権限とさまざまな化合物許可を制御するセクションコードの実装です。
/*** AOP傍受判断は、読み取り専用管理者とその複合管理者で実行されます。 * @Param JoinPointエントリポイント。 * @throws ioexception * / public void readonly(joinpoint joinpoint)throws ioexception { / ** *傍受されたメソッドを取得します。 */ string methodname = joinpoint.getSignature()。getName(); /***インターセプトされたオブジェクトを取得します。 */ object object = joinpoint.getTarget(); logger.info( "許可管理aop、メソッド名" + methodname); httpservletrequest request =((servletRequestattributes)requestContextholder.getRequestattributes())。getRequest(); httpservletResponse応答=((servletRequestattributes)requestContExtholder.getRequestattributes())。getResponse(); string roleflag = getloginuserinfor.getloginuserrole(request); / ***スーパー管理者*/ if(permissionsLabeled.super_admin.equals(roleflag)){return; } / ***権限のデータ変更を行うための読み取り専用管理者の判断Response.sendredirect(request.getContextPath() + "/auth/readonly"); } / ** *部門管理者、および読み取り専用管理者である * / if(permissionsLabeled.dept_reader_admin.equals(roleflag)){if(object instanceof departmentcontroller){return; } if(object instanceof usercontroller){if(methodname.contains( "addadmin")){respons.sendredirect(request.getContextPath() + "/auth/readonly"); } if(methodname.contains( "leteteadmin")){respons.sendredirect(request.getContextPath() + "/auth/readonly"); } if(methodname.contains( "updateadmin")){respons.sendredirect(request.getContextPath() + "/auth/readonly"); } if(methodname.contains( "updateadmin")){respons.sendredirect(request.getContextPath() + "/auth/readonly"); } if(methodname.contains( "updateadmin")){respons.sendredirect(request.getContextPath() + "/auth/readonly"); } 戻る; } if(object instanceof groupcontroller){return; } logger.error( "部門管理者、および読み取り専用管理者の操作許可なし!"); Response.sendredirect(request.getContextPath() + "/auth/readonly"); } / ***アプリケーション管理者、および読み取り専用管理者* / if(permissionsLabeled.app_reader_admin.equals(roleflag)){if(object instanceof appcontroller){return; } if(object instanceof apppolicycontroller){return; } logger.error( "アプリケーション管理者、および読み取り専用管理者の操作権限なし!"); Response.sendredirect(request.getContextPath() + "/auth/readonly"); } / ***部門管理者、およびアプリケーション管理者、および読み取り専用管理者* / if(permissionsLabeled.dept_app_reader_admin.equals(roleflag)){if(object instanceof departmentcontroller){return; } if(object instanceof usercontroller){return; } if(object instanceof groupcontroller){return; } if(object instanceof appcontroller){return; } if(object instanceof apppolicycontroller){return; } logger.error( "部門管理者、アプリケーション管理者、および読み取り専用管理者には操作権限がありません"); Response.sendredirect(request.getContextPath() + "/auth/readonly"); }}専用の管理者特権制御を備えたポイントカット選択
特別な管理者の権利を持つことは非常に特別なため、著者は一般的なアクセス権に加えてコントローラーを使用します。特別な状況では、コードロジックに実装できます。構成ファイルコードは次のとおりです。
<aop:config> <! - セクションを定義します - > <aop:aspect id = "authaspect" ref = "userspermissionsionsadvice"> <! - エントリポイントを定義します(com.thundersoft.metadata.web.controllerは呼び出される前に傍受されます) - > <aop:pointcut = " com.thundersoft.metadata.web.controller。* com.thundersoft.metadata.web.controller.SelfServiceController !execution(* com.thundersoft.metadata.web.controller.dashboardcontroller。*(..)))およびcom.thundersoft.metadata.web.controller.maincontroller。*(..)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
##アドレスコード許可管理の実装
/***アプリケーション管理者および部門管理者について、任意の傍受判断を行います。 * @Param JoinPointエントリポイント。 * @throws ioexception * / public void appdeptauth(joinpoint joinpoint)throws ioexception { / ** *傍受メソッドを取得します。 */ string methodname = joinpoint.getSignature()。getName(); /***インターセプトされたオブジェクトを取得します。 */ object object = joinpoint.getTarget(); logger.info( "許可管理aop、メソッド名"、methodname); httpservletrequest request =((servletRequestattributes)requestContextholder.getRequestattributes())。getRequest(); httpservletResponse応答=((servletRequestattributes)requestContExtholder.getRequestattributes())。getResponse(); string roleflag = getloginuserinfor.getloginuserrole(request); / ***スーパー管理者*/ if(permissionsLabeled.super_admin.equals(roleflag)){return; } / ***データ変更を行うアプリケーション管理者の判断権限} if(object instanceof apppolicycontroller){return; } logger.error( "アプリケーション管理者には操作権限がありません"); Response.sendredirect(request.getContextPath() + "/auth/readonly"); } else if(permissionsLabeled.dept_admin.equals(roleflag)){if(object instanceof departmentcontroller){return; } if(object instanceof usercontroller){return; } if(object instanceof groupcontroller){return; } if( "getalldepartments" .equals(methodname)){return; } logger.error( "アプリケーション管理者には操作権限がありません"); Response.sendredirect(request.getContextPath() + "/auth/readonly"); } else {return; }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。