spring-security 使用AuthenticationProvider配置自定義登錄選項(xiàng)

我們?cè)谑褂?code>spring sec的時(shí)候,一般會(huì)繼承WebSecurityConfigurerAdapter類妖胀,然后選擇覆蓋protected void configure(AuthenticationManagerBuilder auth)protected void configure(HttpSecurity http)方法

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(studentService).passwordEncoder(encoder);
    auth.userDetailsService(teacherService).passwordEncoder(encoder);
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
        .authorizeRequests()
        .antMatchers("/**").authenticated()
        .and()
        .formLogin().loginPage("/login").permitAll()
        .successHandler(new MySavedRequestAwareAuthenticationSuccessHandler())
        .failureHandler(new SimpleUrlAuthenticationFailureHandler())
        .and()
        .logout().logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK))
        .and()
        .exceptionHandling().authenticationEntryPoint(new Http401AuthenticationEntryPoint("login first"))
        .accessDeniedHandler(new AccessDeniedHandlerImpl());
  }

一般而言登錄的數(shù)據(jù)我們?cè)?code>protected void configure(AuthenticationManagerBuilder auth)中聚蝶,我們?cè)?code>studentService中配置一個(gè)

  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    Student student = studentRepository.findByStudentId(username)
                                       .orElseThrow(() -> new UsernameNotFoundException("Not found: " + username));
    return new StudentDetails(student);
  }

方法就好嘹叫。

但是遇到一個(gè)問題蓬痒,這樣的話用戶名和密碼都是定死的,我們拿不到form-data數(shù)據(jù)闯睹,如果因?yàn)榍岸说膯栴}戏羽,這種密碼登錄方式以外,我們還要稍微修改提交給我們的form-data中的密碼數(shù)據(jù)瞻坝,做一下處理蛛壳,自定義一個(gè)登錄呢。

這個(gè)時(shí)候就需要用到AuthenticationProvider了所刀。
這是一個(gè)接口,提供了兩種方法


public interface AuthenticationProvider {
  
  Authentication authenticate(Authentication authentication)
          throws AuthenticationException;

  boolean supports(Class<?> authentication);
}

通過第一個(gè)方法我們可以拿到form-data的數(shù)據(jù)捞挥,并且返回一個(gè)UserDetails如果登錄成功的話浮创,或者返回null如果登錄失敗。

@Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String userName = (String) authentication.getPrincipal(); //拿到username
        String password = (String) authentication.getCredentials(); //拿到password
       
        UserDetails userDetails = studentService.loadUserByUsername(userName);
        if (/*自定義的驗(yàn)證通過*/) {
            return new UsernamePasswordAuthenticationToken(userDetails, null,userDetails.getAuthorities());
        }
        /*驗(yàn)證不通過*/
        return null;

第二個(gè)方法是告訴spring sec我們這個(gè)驗(yàn)證支持哪種驗(yàn)證砌函。
你可能疑惑這個(gè)還分?
其實(shí)是這樣斩披。

 auth.userDetailsService(studentService).passwordEncoder(encoder);

這種嚴(yán)重屬于Dao驗(yàn)證。

還有UsernamePasswordAuthentication驗(yàn)證讹俊。
我們的是UsernamePassword的驗(yàn)證方式垦沉,所以在第二個(gè)方法中一般會(huì)這么寫

   @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }

下面就是注冊(cè)要configure方法中了
只要加入

auth.authenticationProvider(new MyAuthenticationProvider());

就好了。仍劈。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末厕倍,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子贩疙,更是在濱河造成了極大的恐慌讹弯,老刑警劉巖况既,帶你破解...
    沈念sama閱讀 222,378評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異组民,居然都是意外死亡棒仍,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門臭胜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來莫其,“玉大人,你說我怎么就攤上這事耸三÷叶福” “怎么了?”我有些...
    開封第一講書人閱讀 168,983評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵吕晌,是天一觀的道長蛋褥。 經(jīng)常有香客問我,道長睛驳,這世上最難降的妖魔是什么烙心? 我笑而不...
    開封第一講書人閱讀 59,938評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮乏沸,結(jié)果婚禮上淫茵,老公的妹妹穿的比我還像新娘。我一直安慰自己蹬跃,他們只是感情好匙瘪,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,955評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蝶缀,像睡著了一般丹喻。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上翁都,一...
    開封第一講書人閱讀 52,549評(píng)論 1 312
  • 那天碍论,我揣著相機(jī)與錄音,去河邊找鬼柄慰。 笑死鳍悠,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的坐搔。 我是一名探鬼主播藏研,決...
    沈念sama閱讀 41,063評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼概行!你這毒婦竟也來了蠢挡?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,991評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎袒哥,沒想到半個(gè)月后缩筛,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,522評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡堡称,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,604評(píng)論 3 342
  • 正文 我和宋清朗相戀三年瞎抛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片却紧。...
    茶點(diǎn)故事閱讀 40,742評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡桐臊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出晓殊,到底是詐尸還是另有隱情断凶,我是刑警寧澤,帶...
    沈念sama閱讀 36,413評(píng)論 5 351
  • 正文 年R本政府宣布巫俺,位于F島的核電站认烁,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏介汹。R本人自食惡果不足惜却嗡,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,094評(píng)論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望嘹承。 院中可真熱鬧窗价,春花似錦、人聲如沸叹卷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽骤竹。三九已至帝牡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蒙揣,已是汗流浹背否灾。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評(píng)論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留鸣奔,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,159評(píng)論 3 378
  • 正文 我出身青樓惩阶,卻偏偏與公主長得像挎狸,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子断楷,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,747評(píng)論 2 361

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