欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringSecurity自定義AuthenticationProvider無法@Autowire的解決

 更新時(shí)間:2021年12月20日 10:45:05   作者:Epiphanic  
這篇文章主要介紹了SpringSecurity自定義AuthenticationProvider無法@Autowire的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

自定義AuthenticationProvider無法@Autowire的解決

在AuthenticationProvider中使用@Autowired注入時(shí)始終報(bào)Null問題

找了半天發(fā)現(xiàn)應(yīng)該在SecurityConfig配置類中

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

在設(shè)置AuthenticationProvider時(shí)

應(yīng)該使用@Bean的方式設(shè)置

@Bean
    CustomAuthenticationProvider customAuthenticationProvider() {
        return new CustomAuthenticationProvider();
    }   
@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(customAuthenticationProvider());
    }

之前的錯(cuò)誤的設(shè)置方式是

@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(new CustomAuthenticationProvider());
    }

好了,這就可以實(shí)現(xiàn)AuthenticationProvider時(shí)自由的使用@Autowired了

自定義AuthenticationProvider的簡單例子

xml 配置

<authentication-manager>
        <authentication-provider ref="myAuthenticationProvider" />
    </authentication-manager>
 
  <beans:bean id="userDetailsService" class="net.mantis.security.auth.NMUserDetailsService"/>
  <beans:bean id="myAuthenticationProvider" class="net.mantis.security.auth.MyAuthenticationProvider">
       <beans:property name="userDetailsService">
            <beans:bean class="net.mantis.security.auth.NMUserDetailsService">            
            </beans:bean>
        </beans:property>
  </beans:bean>

net.mantis.security.auth.MyAuthenticationProvider

public class MyAuthenticationProvider implements AuthenticationProvider {
 
    UserDetailsService userDetailsService;
    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {
         //username
        System.out.println("user name: "+authentication.getName());
        //password
        System.out.println("password: "+authentication.getCredentials());
        System.out.println("getPrincipal: "+authentication.getPrincipal());
        System.out.println("getAuthorities: "+authentication.getAuthorities());
        System.out.println("getDetails: "+authentication.getDetails());
        UserDetails userDetails = (UserDetails)this.userDetailsService.loadUserByUsername(authentication.getName());
      
        UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(
                userDetails, authentication.getCredentials(),userDetails.getAuthorities());
        return result;
    }
    public boolean supports(Class authentication) {
         return true;
    }
    public void setUserDetailsService(UserDetailsService userDetailsService){
        this.userDetailsService = userDetailsService;
    }
}

net.mantis.security.auth.NMUserDetailsService

public class NMUserDetailsService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String userName)
            throws UsernameNotFoundException {
        ArrayList list = new ArrayList();
        list.add(new SimpleGrantedAuthority("ROLE_SUPERVISOR"));
        User details = new User("rod", "koala", list);
        return details;
    }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java?死鎖解決方案順序鎖和輪詢鎖

    Java?死鎖解決方案順序鎖和輪詢鎖

    這篇文章主要介紹了Java?死鎖解決方案順序鎖和輪詢鎖,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • springboot操作靜態(tài)資源文件的方法

    springboot操作靜態(tài)資源文件的方法

    這篇文章主要介紹了springboot操作靜態(tài)資源文件的方法,本文給大家提到了兩種方法,小編在這里比較推薦第一種方法,具體內(nèi)容詳情大家跟隨腳本之家小編一起看看吧
    2018-07-07
  • idea全局設(shè)置Maven配置的實(shí)現(xiàn)步驟

    idea全局設(shè)置Maven配置的實(shí)現(xiàn)步驟

    本文主要介紹了idea全局設(shè)置Maven配置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Java內(nèi)存溢出實(shí)現(xiàn)原因及解決方案

    Java內(nèi)存溢出實(shí)現(xiàn)原因及解決方案

    這篇文章主要介紹了Java內(nèi)存溢出實(shí)現(xiàn)原因及解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Spring?Boot?項(xiàng)目中?JPA?語法的基本使用方法

    Spring?Boot?項(xiàng)目中?JPA?語法的基本使用方法

    這篇文章主要介紹了?Spring?Boot?項(xiàng)目中?JPA?語法的基本使用方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-10-10
  • java線程封閉之棧封閉和ThreadLocal

    java線程封閉之棧封閉和ThreadLocal

    這篇文章主要介紹了java線程封閉之棧封閉和ThreadLocal,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • SpringBoot中加密模塊的使用

    SpringBoot中加密模塊的使用

    本文主要介紹了SpringBoot中加密模塊的使用,包括對稱加密、非對稱加密和哈希加密等,同時(shí)還會(huì)提供相應(yīng)的代碼示例,感興趣的朋友可以參考一下
    2023-05-05
  • java時(shí)間戳轉(zhuǎn)日期格式的實(shí)現(xiàn)代碼

    java時(shí)間戳轉(zhuǎn)日期格式的實(shí)現(xiàn)代碼

    本篇文章是對java時(shí)間戳轉(zhuǎn)日期格式的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • 淺談一下單體架構(gòu)的缺點(diǎn)是什么

    淺談一下單體架構(gòu)的缺點(diǎn)是什么

    這篇文章主要介紹了單體架構(gòu)的缺點(diǎn)是什么,通常我們所使用的傳統(tǒng)單體應(yīng)用架構(gòu)都是模塊化的設(shè)計(jì)邏輯,程序在編寫完成后會(huì)被打包并部署為一個(gè)具體的應(yīng)用,而應(yīng)用的格式則依賴于相應(yīng)的應(yīng)用語言和框架,需要的朋友可以參考下
    2023-04-04
  • SpringBoot中MockMVC單元測試的實(shí)現(xiàn)

    SpringBoot中MockMVC單元測試的實(shí)現(xiàn)

    Mock是一種用于模擬和替換類的對象的方法,以便在單元測試中獨(dú)立于外部資源進(jìn)行測試,本文主要介紹了SpringBoot中MockMVC單元測試的實(shí)現(xiàn),具有應(yīng)該的參考價(jià)值,感興趣的可以了解一下
    2024-02-02

最新評(píng)論