基于SpringSecurity google 二次驗(yàn)證

主要就是 增加安全性火的,類似于 短信二次驗(yàn)證一樣壶愤,不過Google 二次驗(yàn)證 提供的是開源一套算法,節(jié)約成本馏鹤,很多網(wǎng)站為了真加安全性,都開啟了二次驗(yàn)證 娇哆。

java 具體思路
  1. 網(wǎng)站或者服務(wù)端 開啟二次驗(yàn)證 ,引入開源工具包
  1. 編寫對應(yīng)的工具類湃累,生成二維碼鏈接勃救,用戶掃描綁定 秘鑰key

  2. 自定義 AuthenticationProvider,UsernamePasswordAuthenticationToken 在校驗(yàn)完用戶密碼后再 處理 google 校驗(yàn)邏輯

代碼
  1. 修改配置SpringSecurity
httpSecurity.authenticationProvider(new CustomerAuthenticationProvider(userDetailsService,bCryptPasswordEncoder()));
  1. 自定義 CustomerAuthenticationProvider治力,CustomerUsernamePasswordAuthenticationToken 直接繼承重寫父類方法就行

    
    
    public class CustomerAuthenticationProvider extends DaoAuthenticationProvider {
    
        public CustomerAuthenticationProvider(UserDetailsService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder) {
            super();
            setUserDetailsService(userDetailsService);
            setPasswordEncoder(bCryptPasswordEncoder);
        }
    
        protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
            if (authentication.getCredentials() == null) {
                this.logger.debug("Failed to authenticate since no credentials provided");
                throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
            } else {
                String presentedPassword = authentication.getCredentials().toString();
                if (!getPasswordEncoder().matches(presentedPassword, userDetails.getPassword())) {
                    this.logger.debug("Failed to authenticate since password does not match stored value");
                    throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
                }
                googleAuthenticator((LoginUser) userDetails, (CustomerUsernamePasswordAuthenticationToken) authentication);
    
            }
        }
    
        /**
         * Google 二次驗(yàn)證
         * @param userDetails
         * @param authentication
         */
        private void googleAuthenticator(LoginUser userDetails, CustomerUsernamePasswordAuthenticationToken authentication) {
            // Google 二次驗(yàn)證
            LoginUser loginUser = userDetails;
            SysUser user = loginUser.getUser();
            String googleAuthSecret = user.getGoogleAuthSecret();
            if(StringUtils.isBlank(googleAuthSecret)){
                throw new ServiceException(GOOGLE_AUTHENTICATOR_401001.getMsg(),GOOGLE_AUTHENTICATOR_401001.getCode());
            }
            CustomerUsernamePasswordAuthenticationToken customerToken = authentication;
            String code = customerToken.getCode();
            boolean valid = GoogleAuthenticatorUtils.valid(googleAuthSecret, Integer.valueOf(code).intValue());
            if(!valid){
                throw new ServiceException("Google Authenticator 驗(yàn)證碼錯(cuò)誤");
            }
        }
    
    }
    
    public class CustomerUsernamePasswordAuthenticationToken extends UsernamePasswordAuthenticationToken {
        /**
         * Google 二次驗(yàn)證 生成 code
         */
        private String code;
    
        public CustomerUsernamePasswordAuthenticationToken(Object principal, Object credentials) {
            super(principal, credentials);
        }
    
        public CustomerUsernamePasswordAuthenticationToken(Object principal, Object credentials,String code) {
            super(principal, credentials);
            this.code = code;
        }
    
        public CustomerUsernamePasswordAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities) {
            super(principal, credentials, authorities);
    
        }
    
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    }
    
    // 調(diào)用自定義 CustomerUsernamePasswordAuthenticationToken
    authentication = authenticationManager
                        .authenticate(new CustomerUsernamePasswordAuthenticationToken(username, password,code));
    
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蒙秒,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子宵统,更是在濱河造成了極大的恐慌晕讲,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件马澈,死亡現(xiàn)場離奇詭異瓢省,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)痊班,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進(jìn)店門勤婚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人涤伐,你說我怎么就攤上這事馒胆。” “怎么了凝果?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵祝迂,是天一觀的道長。 經(jīng)常有香客問我器净,道長型雳,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任掌动,我火速辦了婚禮四啰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘粗恢。我一直安慰自己柑晒,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布眷射。 她就那樣靜靜地躺著匙赞,像睡著了一般。 火紅的嫁衣襯著肌膚如雪妖碉。 梳的紋絲不亂的頭發(fā)上涌庭,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天,我揣著相機(jī)與錄音欧宜,去河邊找鬼坐榆。 笑死,一個(gè)胖子當(dāng)著我的面吹牛冗茸,可吹牛的內(nèi)容都是我干的席镀。 我是一名探鬼主播匹中,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼豪诲!你這毒婦竟也來了顶捷?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤屎篱,失蹤者是張志新(化名)和其女友劉穎服赎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體交播,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡重虑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了堪侯。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嚎尤。...
    茶點(diǎn)故事閱讀 39,731評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖伍宦,靈堂內(nèi)的尸體忽然破棺而出芽死,到底是詐尸還是另有隱情,我是刑警寧澤次洼,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布关贵,位于F島的核電站,受9級特大地震影響卖毁,放射性物質(zhì)發(fā)生泄漏揖曾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一亥啦、第九天 我趴在偏房一處隱蔽的房頂上張望炭剪。 院中可真熱鬧,春花似錦翔脱、人聲如沸奴拦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽错妖。三九已至,卻和暖如春疚沐,著一層夾襖步出監(jiān)牢的瞬間暂氯,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工亮蛔, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留痴施,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像晾剖,于是被迫代替她去往敵國和親锉矢。 傳聞我的和親對象是個(gè)殘疾皇子梯嗽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容