最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談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();

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

        // 添加該用戶以便讓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ù)(用戶名密碼)也可以直接調(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();

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

        // 添加該用戶以便讓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ù)(用戶名密碼)也可以直接調(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)文章

  • springBoot整合rabbitmq測(cè)試常用模型小結(jié)

    springBoot整合rabbitmq測(cè)試常用模型小結(jié)

    這篇文章主要介紹了springBoot整合rabbitmq并測(cè)試五種常用模型,本文主要針對(duì)前五種常用模型,在spirngboot框架的基礎(chǔ)上整合rabbitmq并進(jìn)行測(cè)試使用,需要的朋友可以參考下
    2022-01-01
  • 詳解spring security四種實(shí)現(xiàn)方式

    詳解spring security四種實(shí)現(xiàn)方式

    這篇文章主要介紹了詳解spring security四種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • SpringBoot全局異常處理之多個(gè)處理器匹配順序(最新推薦)

    SpringBoot全局異常處理之多個(gè)處理器匹配順序(最新推薦)

    這篇文章主要介紹了SpringBoot全局異常處理之多個(gè)處理器匹配順序(最新推薦),調(diào)試源碼可見(jiàn)匹配順序?yàn)椋寒惓蛹?jí)高者優(yōu)先,再清楚點(diǎn),子類(lèi)異常處理器優(yōu)先,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-03-03
  • 通過(guò)實(shí)例解析Spring argNames屬性

    通過(guò)實(shí)例解析Spring argNames屬性

    這篇文章主要介紹了通過(guò)實(shí)例解析Spring argNames屬性,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Java字符串的intern方法有何奧妙之處

    Java字符串的intern方法有何奧妙之處

    intern() 方法返回字符串對(duì)象的規(guī)范化表示形式。它遵循以下規(guī)則:對(duì)于任意兩個(gè)字符串 s 和 t,當(dāng)且僅當(dāng) s.equals(t) 為 true 時(shí),s.intern() == t.intern() 才為 true
    2021-10-10
  • mybatis中的if?test判斷入?yún)⒌闹祮?wèn)題

    mybatis中的if?test判斷入?yún)⒌闹祮?wèn)題

    這篇文章主要介紹了mybatis中的if?test判斷入?yún)⒌闹祮?wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • SpringSecurity rememberme功能實(shí)現(xiàn)過(guò)程解析

    SpringSecurity rememberme功能實(shí)現(xiàn)過(guò)程解析

    這篇文章主要介紹了SpringSecurity rememberme功能實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • java環(huán)境變量配置超詳細(xì)圖文教程

    java環(huán)境變量配置超詳細(xì)圖文教程

    在我們學(xué)習(xí)Java語(yǔ)言的時(shí)候,要在命令提示符里運(yùn)用Java和Javac,用到這兩個(gè)命令的時(shí)候就要配置Java環(huán)節(jié)變量才可以,這篇文章主要給大家介紹了關(guān)于java環(huán)境變量配置的相關(guān)資料,需要的朋友可以參考下
    2023-10-10
  • Java 關(guān)鍵字 速查表介紹

    Java 關(guān)鍵字 速查表介紹

    下面小編就為大家?guī)?lái)一篇Java 關(guān)鍵字 速查表介紹。小編覺(jué)得聽(tīng)不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • 在Java中使用Jwt的示例代碼

    在Java中使用Jwt的示例代碼

    這篇文章主要介紹了在Java中使用Jwt的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評(píng)論

林甸县| 咸宁市| 夏河县| 无为县| 邵东县| 巫溪县| 惠水县| 淮滨县| 龙岩市| 昭苏县| 海兴县| 台东县| 桃园市| 军事| 大埔区| 洛扎县| 治县。| 商都县| 尖扎县| 确山县| 无极县| 梁河县| 平遥县| 伊通| 汉川市| 水富县| 乡宁县| 治县。| 环江| 武平县| 内乡县| 博野县| 中阳县| 宁海县| 扬州市| 鄂托克旗| 梁山县| 宁波市| 固镇县| 开原市| 洛阳市|