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

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

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

重寫UserDetailService組件

1.注入Bean的方式

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

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

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

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

        return userDetailsService;
    }

    /**
     * 重寫UserDetailsService組件也必須重寫PasswordEncoder組件,否則會報:
     *    java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
     */
     @Bean
    public PasswordEncoder passwordEncoder () {
        // NoOpPasswordEncoder實(shí)例會將密碼視為普通文本,他不會對密碼進(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 {


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

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

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

    /**
     * 重寫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();

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

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

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

    }
}

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

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


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

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

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

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

        // 重寫身份驗(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;

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

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

    }

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

    }
}

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

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

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

    Spring的@Conditional詳解

    這篇文章主要介紹了Spring的@Conditional詳解,給想要注入Bean增加限制條件,只有滿足限制條件才會被構(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文件類型

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

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

最新評論