springMVC自定義註解實現使用者行為驗證

2021-09-09 01:50:15 字數 1146 閱讀 3072

最近在進行專案開發的時候需要對介面做session驗證

1、自定義乙個註解@authcheckannotation

@documented

@target(elementtype.method)

@inherited

@retention(retentionpolicy.runtime)

public @inte***ce

authcheckannotation

2、定義乙個相應的***,在springmvc配置檔案中進行配置

***:

spring為我們提供了org.springframework.web.servlet.handler.handlerinterceptoradapter這個介面卡,繼承此類,可以非常方便的實現自己的***。可以根據我們的需要重寫prehandle、posthandle、aftercompletio方法。

分別實現預處理、後處理(呼叫了service並返回modelandview,但未進行頁面渲染)、返回處理(已經渲染了頁面)

在prehandle中,可以進行編碼、安全控制等處理;

在posthandle中,有機會修改modelandview;

在aftercompletion中,可以根據ex是否為null判斷是否發生了異常,進行日誌記錄。

public

class authcheckinteceptor extends

handlerinterceptoradapter

userinfo user=(userinfo)request.getsession().getattribute(constants.session_user);

try

catch

(exception e)

}}

在springmvc.xml檔案中新增***

3、在springmvc controller中使用例項

spring mvc引數自定義註解開發

b 前言 b 在使用過程中,發現spring mvc的restful架構,有一些滿足不了需求。因此研究了一下自定義註解開發滿足需求。自定義註解,可以無縫輸入引數到controller中,尤其是http的put請求。b 一 搭建標準的spring mvc環境 b 略。b 二 開發註解詳細過程 b 1....

自定義註解

target elementtype.field retention retentionpolicy.runtime public inte ce setvalue以上就是乙個自定義的註解,下面來進行說明。target elementtype.field 表示支援該註解的程式元素,field就是屬性...

自定義註解

三個重要元註解 target 即註解的作用域,用於說明註解的使用範圍 即註解可以用在什麼地方,比如類的註解,方法註解,成員變數註解等等 elemenettype.constructor 構造器宣告 elemenettype.field 域宣告 包括 enum 例項 elemenettype.loca...