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

淺談SpringSecurity重寫默認配置

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

重寫UserDetailService組件

1.注入Bean的方式

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

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

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

        // 添加該用戶以便讓UserDetailsService對其進行管理
        userDetailsService.createUser(user);

        return userDetailsService;
    }

    /**
     * 重寫UserDetailsService組件也必須重寫PasswordEncoder組件,否則會報:
     *    java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
     */
     @Bean
    public PasswordEncoder passwordEncoder () {
        // NoOpPasswordEncoder實例會將密碼視為普通文本,他不會對密碼進行加密或者hash處理
        return NoOpPasswordEncoder.getInstance();
    }
}

2.擴展WebSecurityConfigurerAdapter

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


    /**
     * 重寫端點授權配置,就需要擴展WebSecurityConfigurerAdapter類,可以使用HttpSecurity對象的不同方法更改配置
     */
    @Override
    protected void configure (HttpSecurity httpSecurity) throws Exception {
        httpSecurity.httpBasic();

        // 所有請求都需要身份驗證
        // httpSecurity.authorizeRequests().anyRequest().authenticated();

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

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

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

        // 添加該用戶以便讓UserDetailsService對其進行管理
        userDetailsService.createUser(user);

        // AuthenticationManagerBuilder調用userDetailsService()方法來注冊UserDetailsService實例
        // AuthenticationManagerBuilder調用passwordEncoder()方法來注冊NoOpPasswordEncoder實例
        auth.userDetailsService(userDetailsService).passwordEncoder(NoOpPasswordEncoder.getInstance());

    }
}

重寫端點授權配置

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


    /**
     * 重寫端點授權配置,就需要擴展WebSecurityConfigurerAdapter類,可以使用HttpSecurity對象的不同方法更改配置
     */
    @Override
    protected void configure (HttpSecurity httpSecurity) throws Exception {
        httpSecurity.httpBasic();

        // 所有請求都需要身份驗證
        // httpSecurity.authorizeRequests().anyRequest().authenticated();

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

重寫AuthenticationProvider實現(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());

        // 重寫身份驗證提供者,用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;

    /**
     * 重寫端點授權配置,就需要擴展WebSecurityConfigurerAdapter類,可以使用HttpSecurity對象的不同方法更改配置
     */
    @Override
    protected void configure (HttpSecurity httpSecurity) throws Exception {
        httpSecurity.httpBasic();

        // 所有請求都需要身份驗證
         httpSecurity.authorizeRequests().anyRequest().authenticated();

    }

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

    }
}

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

相關文章

  • Java中的接口回調實例

    Java中的接口回調實例

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

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

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

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

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

    java從控制臺接收一個數(shù)字的實例詳解

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

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

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

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

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

    Spring的@Conditional詳解

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

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

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

    AsyncHttpClient?ClientStats源碼流程解讀

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

    基于SpringBoot框架管理Excel和PDF文件類型

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

最新評論