亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺談SpringSecurity重寫(xiě)默認(rèn)配置

 更新時(shí)間:2025年01月20日 10:04:56   作者:@來(lái)杯咖啡  
這篇文章主要介紹了SpringSecurity重寫(xiě)默認(rèn)配置,包括注入Bean、擴(kuò)展WebSecurityConfigurerAdapter、重寫(xiě)端點(diǎn)授權(quán)配置及實(shí)現(xiàn)AuthenticationProvider,感興趣的可以了解一下

重寫(xiě)UserDetailService組件

1.注入Bean的方式

/**
 * @author: coffee
 * @date: 2024/6/22 21:22
 * @description: 重寫(xiě)springsecurity默認(rèn)組件:注入Bean的方式
 */
 @Configuration
public class ProjectConfig {

    /**
     * 重寫(xiě)userDetailsService組件
     */
     @Bean
    public UserDetailsService userDetailsService () {
        // InMemoryUserDetailsManager實(shí)現(xiàn)并不適用生成環(huán)境,此處進(jìn)作為demo使用
        InMemoryUserDetailsManager userDetailsService = new InMemoryUserDetailsManager();

        // 使用指定用戶(hù)名、密碼和權(quán)限列表構(gòu)建用戶(hù)
        UserDetails user = User.withUsername("john").password("12345").authorities("read").build();

        // 添加該用戶(hù)以便讓UserDetailsService對(duì)其進(jìn)行管理
        userDetailsService.createUser(user);

        return userDetailsService;
    }

    /**
     * 重寫(xiě)UserDetailsService組件也必須重寫(xiě)PasswordEncoder組件,否則會(huì)報(bào):
     *    java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
     */
     @Bean
    public PasswordEncoder passwordEncoder () {
        // NoOpPasswordEncoder實(shí)例會(huì)將密碼視為普通文本,他不會(huì)對(duì)密碼進(jìn)行加密或者h(yuǎn)ash處理
        return NoOpPasswordEncoder.getInstance();
    }
}

2.擴(kuò)展WebSecurityConfigurerAdapter

/**
 * @author: coffee
 * @date: 2024/6/22 21:46
 * @description:
 */
@Configuration
public class ProjectConfig2 extends WebSecurityConfigurerAdapter {


    /**
     * 重寫(xiě)端點(diǎn)授權(quán)配置,就需要擴(kuò)展WebSecurityConfigurerAdapter類(lèi),可以使用HttpSecurity對(duì)象的不同方法更改配置
     */
    @Override
    protected void configure (HttpSecurity httpSecurity) throws Exception {
        httpSecurity.httpBasic();

        // 所有請(qǐng)求都需要身份驗(yàn)證
        // httpSecurity.authorizeRequests().anyRequest().authenticated();

        // permitAll()方法修改授權(quán)配置,無(wú)需憑據(jù)(用戶(hù)名密碼)也可以直接調(diào)用接口。   curl http://localhost:8080/hello
        httpSecurity.authorizeRequests().anyRequest().permitAll();
    }

    /**
     * 重寫(xiě)springsecurity默認(rèn)組件:繼承WebSecurityConfigurerAdapter的方式
     */
    @Override
    protected void configure (AuthenticationManagerBuilder auth) throws Exception {
        // InMemoryUserDetailsManager實(shí)現(xiàn)并不適用生成環(huán)境,此處進(jìn)作為demo使用
        InMemoryUserDetailsManager userDetailsService = new InMemoryUserDetailsManager();

        // 使用指定用戶(hù)名、密碼和權(quán)限列表構(gòu)建用戶(hù)
        UserDetails user = User.withUsername("john").password("12345").authorities("read").build();

        // 添加該用戶(hù)以便讓UserDetailsService對(duì)其進(jìn)行管理
        userDetailsService.createUser(user);

        // AuthenticationManagerBuilder調(diào)用userDetailsService()方法來(lái)注冊(cè)UserDetailsService實(shí)例
        // AuthenticationManagerBuilder調(diào)用passwordEncoder()方法來(lái)注冊(cè)NoOpPasswordEncoder實(shí)例
        auth.userDetailsService(userDetailsService).passwordEncoder(NoOpPasswordEncoder.getInstance());

    }
}

重寫(xiě)端點(diǎn)授權(quán)配置

/**
 * @author: coffee
 * @date: 2024/6/22 21:46
 * @description:
 */
@Configuration
public class ProjectConfig2 extends WebSecurityConfigurerAdapter {


    /**
     * 重寫(xiě)端點(diǎn)授權(quán)配置,就需要擴(kuò)展WebSecurityConfigurerAdapter類(lèi),可以使用HttpSecurity對(duì)象的不同方法更改配置
     */
    @Override
    protected void configure (HttpSecurity httpSecurity) throws Exception {
        httpSecurity.httpBasic();

        // 所有請(qǐng)求都需要身份驗(yàn)證
        // httpSecurity.authorizeRequests().anyRequest().authenticated();

        // permitAll()方法修改授權(quán)配置,無(wú)需憑據(jù)(用戶(hù)名密碼)也可以直接調(diào)用接口。   curl http://localhost:8080/hello
        httpSecurity.authorizeRequests().anyRequest().permitAll();
    }
}

重寫(xiě)AuthenticationProvider實(shí)現(xiàn)

/**
 * @author: coffee
 * @date: 2024/6/22 22:15
 * @description: ...
 */
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String userName = authentication.getName();
        String password = String.valueOf(authentication.getCredentials());

        // 重寫(xiě)身份驗(yàn)證提供者,用if else 替換 UserDetailsService和PasswordEncoder
        if ("john".equals(userName) && "12345".equals(password)) {
            return new UsernamePasswordAuthenticationToken(userName, password, Arrays.asList());
        } else {
            throw new AuthenticationCredentialsNotFoundException("ERROR");
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
    }
}
/**
 * @author: coffee
 * @date: 2024/6/22 21:46
 * @description:
 */
@Configuration
public class ProjectConfig2 extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomAuthenticationProvider customAuthenticationProvider;

    /**
     * 重寫(xiě)端點(diǎn)授權(quán)配置,就需要擴(kuò)展WebSecurityConfigurerAdapter類(lèi),可以使用HttpSecurity對(duì)象的不同方法更改配置
     */
    @Override
    protected void configure (HttpSecurity httpSecurity) throws Exception {
        httpSecurity.httpBasic();

        // 所有請(qǐng)求都需要身份驗(yàn)證
         httpSecurity.authorizeRequests().anyRequest().authenticated();

    }

    /**
     * 重寫(xiě)身份驗(yàn)證提供者
     */
    @Override
    protected void configure (AuthenticationManagerBuilder auth) throws Exception {
       
        auth.authenticationProvider(customAuthenticationProvider);

    }
}

到此這篇關(guān)于淺談SpringSecurity重寫(xiě)默認(rèn)配置的文章就介紹到這了,更多相關(guān)SpringSecurity重寫(xiě)默認(rèn)配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Java中的接口回調(diào)實(shí)例

    Java中的接口回調(diào)實(shí)例

    今天小編就為大家分享一篇關(guān)于Java中的接口回調(diào)實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • SpringBoot過(guò)濾敏感詞的兩種實(shí)現(xiàn)方式

    SpringBoot過(guò)濾敏感詞的兩種實(shí)現(xiàn)方式

    Spring Boot本身并不直接提供過(guò)濾敏感詞的功能,但你可以使用第三方庫(kù)或者自定義過(guò)濾器來(lái)實(shí)現(xiàn)這個(gè)需求,所以本文給大家介紹了SpringBoot過(guò)濾敏感詞的兩種實(shí)現(xiàn)方式,感興趣的朋友可以參考下
    2024-06-06
  • Java IO流之字節(jié)輸入流的使用詳解

    Java IO流之字節(jié)輸入流的使用詳解

    這篇文章主要為大家詳細(xì)介紹了Java IO流中字節(jié)輸入流的使用,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下
    2022-08-08
  • java從控制臺(tái)接收一個(gè)數(shù)字的實(shí)例詳解

    java從控制臺(tái)接收一個(gè)數(shù)字的實(shí)例詳解

    這篇文章主要介紹了java從控制臺(tái)接收一個(gè)數(shù)字的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例代碼,注釋說(shuō)明清晰,需要的朋友可以參考下
    2017-07-07
  • java實(shí)現(xiàn)面板之間切換功能

    java實(shí)現(xiàn)面板之間切換功能

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)面板之間切換功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Java的System.getProperty()方法獲取大全

    Java的System.getProperty()方法獲取大全

    這篇文章主要介紹了Java的System.getProperty()方法獲取大全,羅列了System.getProperty()方法獲取各類(lèi)信息的用法,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-12-12
  • Spring的@Conditional詳解

    Spring的@Conditional詳解

    這篇文章主要介紹了Spring的@Conditional詳解,給想要注入Bean增加限制條件,只有滿(mǎn)足限制條件才會(huì)被構(gòu)造并注入到Spring的IOC容器中,通常和@Bean注解一起使用,需要的朋友可以參考下
    2024-01-01
  • Java實(shí)現(xiàn)驗(yàn)證碼具體代碼

    Java實(shí)現(xiàn)驗(yàn)證碼具體代碼

    這篇文章主要介紹了Java實(shí)現(xiàn)驗(yàn)證碼具體代碼,有需要的朋友可以參考一下
    2013-12-12
  • AsyncHttpClient?ClientStats源碼流程解讀

    AsyncHttpClient?ClientStats源碼流程解讀

    這篇文章主要為大家介紹了AsyncHttpClient?ClientStats源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • 基于SpringBoot框架管理Excel和PDF文件類(lèi)型

    基于SpringBoot框架管理Excel和PDF文件類(lèi)型

    這篇文章主要介紹了基于SpringBoot框架,管理Excel和PDF文件類(lèi)型,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02

最新評(píng)論