springboot策略模式登入小demo

2021-10-05 04:48:15 字數 2000 閱讀 7124

在策略模式(strategy pattern)中,乙個類的行為或其演算法可以在執行時更改。這種型別的設計模式屬於行為型模式。

在策略模式中,我們建立表示各種策略的物件和乙個行為隨著策略物件改變而改變的 context 物件。策略物件改變 context 物件的執行演算法。

多種第三方登入登入介面

登入驗證介面和實現方法

/**

* 登入校驗介面

*/public

inte***ce

loginservice

@slf4j

@service

public

class

loginwechatserviceimpl

implements

loginservice

}

微博登入實現類
@slf4j

@service

public

class

loginweiboserviceimpl

implements

loginservice

}

logincontext實現
@slf4j

@component

public

class

logincontext

public loginservice getloginservice

(string componentname)

}

新增列舉
public

inte***ce

loginconstant

for(typeenum typeenum : typeenum.

values()

)}return null;}}

}

實現介面
@restcontroller

("/admin/login"

)@slf4j

public

class

logincontroller")

public string login

(@pathvariable

("type"

) integer type)

logincontext.

getloginservice

(typeenum.

getclassname()

).checkuser()

; log.

info

("使用者登入成功");

return

"ok";}

catch

(exception e)

", type, e);}

return

"fail";}

}

成果

這種方式注入是以實現類注入名稱獲取的,例如

loginwechatserviceimpl 注入型別就是 loginwechatserviceimpl

demo位址

github

SpringBoot中使用策略模式

策略模式 定義一組演算法,將每個演算法都封裝起來,相互之間可以互換。策略模式需要乙個抽象演算法介面,一組演算法,乙個容器類 context 假設我們有乙個場景 根據傳值type的不同,呼叫不同的介面 加減乘除 抽象演算法介面 public inte ce izstrategy演算法1 slf4j c...

springboot基於token實現登入認證

最近因為專案需求,需採用token的方式實現登入認證,而不再使用session的方式登入,因而採用springboot整合jwt生成token實現登入認證。1.首先新增jwt所需jar包 io.jsonwebtoken groupid jjwt artifactid 0.7.0 version de...

Springboot(三)redis儲存與登入攔截

autowired private stringredistemplate stringredistemplate redis操作類 若存在,將使用者openid存入redis資料庫,並設定過期時間 string token key uuid.randomuuid tostring integer ...