springMVC 註解 列舉方式實現許可權控制

2021-06-27 17:23:49 字數 1686 閱讀 5477

springmvc註解方式實現許可權思路:

1.使用者登入時將許可權表存入session,功能值用2的n次方累加.如:刪除的修改的許可權值為1,刪除的許可權值為2,增加的許可權值為4,驗證時使用:總值&許可權值==許可權值的方式(1+2+4)&2==2

2.實現自定註解:

@target(elementtype.method)//註解可用範圍

@retention(retentionpolicy.runtime)

@documented

public @inte***ce authority

3.建立列舉類,將所有功能資訊寫入,並提供get方法:

public enum poweritem 

public string getname()

public int getpvalue()

public int getpowerid()

4.實現spring***,在prehander中驗證,返回true或false:

/**

* ***

* */

public class interceptor extends handlerinterceptoradapter

if(loginuser == null)else

log.info("使用者超時,攔截");

return false;

} handlermethod handler2=(handlermethod) handler;

authority authority= handler2.getmethodannotation(authority.class);

if(authority == null)

log.info("驗證許可權,authority:="+authority+",請求許可權名:"+authority.power().getname()+",值:"+authority.power().getpvalue());

//註解申明的power

int apowervalue=authority.power().getpvalue();

int apowerid=authority.power().getpowerid();

//驗證

if(powerutil.haspower(authority.power()))

log.info("許可權驗證未通過,攔截");

//如果是ajax請求

}}

5.控制器中註解

/**

* 暫停繳費

* @throws sqlexception

*/@responsebody

@authority(power=poweritem.currentrecord_supendpay)//許可權驗證

public string suspendpay(string id,string vers) throws sqlexception

SpringMVC異常處理註解方式(3)

方式一 步驟一 建立異常類 public class gyexception extends exception public gyexception string exception public string getexception public void setexception strin...

SpringMVC常用註解,返回方式,路徑匹配形式

這樣訪問http localhost 8080 login login 就可訪問到改方法裡了。2 繫結引數 1 引數繫結 public string finddepatment requestparam departmentid string departmentid 形如這樣的訪問形式 depar...

SpringMVC常用註解

一,controller 負責註冊乙個bean 到spring 上下文中,bean 的id 預設為類名稱開頭字母小寫。1 在springmvc 的配置檔案中定義mycontroller 的bean 物件。2 在springmvc 的配置檔案中告訴spring 該到 去找標記為 controller ...