" />

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

新版SpringSecurity安全配置說(shuō)明

 更新時(shí)間:2023年07月11日 08:30:13   作者:卑微小鐘  
這篇文章主要介紹了新版SpringSecurity安全配置說(shuō)明,在 Spring Security 5.7.0-M2 中,我們棄用了WebSecurityConfigurerAdapter,因?yàn)槲覀児膭?lì)用戶轉(zhuǎn)向基于組件的安全配置,需要的朋友可以參考下

新版SpringSecurityConfig

在使用SpringBoot2.7或者SpringSecurity5.7以上版本時(shí),會(huì)提示:

在 Spring Security 5.7.0-M2 中,我們棄用了WebSecurityConfigurerAdapter,因?yàn)槲覀児膭?lì)用戶轉(zhuǎn)向基于組件的安全配置。

所以之前那種通過(guò)繼承WebSecurityConfigurerAdapter的方式的配置組件是不行的。

同時(shí)也會(huì)遇到很多問(wèn)題,例如:

在向SpringSecurity過(guò)濾器鏈中添加過(guò)濾器時(shí)(例如:JWT支持,第三方驗(yàn)證),我們需要注入AuthenticationManager對(duì)象等問(wèn)題。

故在此記錄一下SpringSecurity的一些基礎(chǔ)配置項(xiàng):

1 網(wǎng)絡(luò)安全配置,忽略部分路徑(如靜態(tài)文件路徑)

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
     return (web) -> web.ignoring().antMatchers("/ignore1", "/ignore2");
}

2 設(shè)置中文配置

@Bean
public ReloadableResourceBundleMessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    // 設(shè)置中文配置
    messageSource.setBasename("classpath:org/springframework/security/messages_zh_CN");
    return messageSource;
}

3 設(shè)置密碼編碼器

@Bean
@ConditionalOnMissingBean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

4 取消ROLE_ prefix

@Bean
@ConditionalOnMissingBean
public GrantedAuthorityDefaults grantedAuthorityDefaults() {
    // Remove the ROLE_ prefix
    return new GrantedAuthorityDefaults("");
}

5 暴露本地認(rèn)證管理器(AuthenticationManager)

/**
 * 認(rèn)證管理器,登錄的時(shí)候參數(shù)會(huì)傳給 authenticationManager
 */
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
    return authenticationConfiguration.getAuthenticationManager();
}

6 其他配置

import com.example.websocket.chat.security.filer.CustomUsernamePasswordAuthenticationFilter;
import com.example.websocket.chat.security.filer.JwtAuthenticationFilter;
import com.example.websocket.chat.security.handler.*;
import com.example.websocket.chat.security.service.JwtStoreService;
import com.example.websocket.chat.security.service.impl.UserDetailsServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.core.GrantedAuthorityDefaults;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutFilter;
import javax.annotation.Resource;
/**
 * @author zhong
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SpringSecurityConfig {
    @Resource
    private CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;
    @Resource
    private CustomAuthenticationFailureHandler customAuthenticationFailureHandler;
    @Resource
    private CustomAuthenticationEntryPoint customAuthenticationEntryPoint;
    @Resource
    private CustomLogoutHandler customLogoutHandler;
    @Resource
    private CustomLogoutSuccessHandler customLogoutSuccessHandler;
    @Resource
    private CustomAccessDeniedHandler customAccessDeniedHandler;
    @Resource
    private SecurityProperties securityProperties;
    @Resource
    private JwtStoreService jwtStoreService;
    @Resource
    private UserDetailsServiceImpl userDetailsService;
    @Resource
    private AuthenticationConfiguration authenticationConfiguration;
    /**
     * 靜態(tài)文件放行
     */
    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().antMatchers(securityProperties.getStaticPaths());
    }
    /**
     * 取消ROLE_前綴
     */
    @Bean
    public GrantedAuthorityDefaults grantedAuthorityDefaults() {
        // Remove the ROLE_ prefix
        return new GrantedAuthorityDefaults("");
    }
    /**
     * 設(shè)置密碼編碼器
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    /**
     * 設(shè)置中文配置
     */
    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:org/springframework/security/messages_zh_CN");
        return messageSource;
    }
    /**
     * 認(rèn)證管理器,登錄的時(shí)候參數(shù)會(huì)傳給 authenticationManager
     */
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }
    /**
     * 設(shè)置默認(rèn)認(rèn)證提供
     */
    @Bean
    public DaoAuthenticationProvider daoAuthenticationProvider() {
        final DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
        authenticationProvider.setUserDetailsService(userDetailsService);
        authenticationProvider.setPasswordEncoder(passwordEncoder());
        return authenticationProvider;
    }
    /**
     * 安全配置
     */
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http, AuthenticationConfiguration authenticationConfiguration) throws Exception {
        // 表單
        http.formLogin()
                // 登錄成功處理器
                .successHandler(customAuthenticationSuccessHandler)
                // 登錄錯(cuò)誤處理器
                .failureHandler(customAuthenticationFailureHandler)
                .and()
                //添加登錄邏輯攔截器,不使用默認(rèn)的UsernamePasswordAuthenticationFilter
                .addFilterBefore(
                        new CustomUsernamePasswordAuthenticationFilter(
                                authenticationManager(),
                                customAuthenticationSuccessHandler,
                                customAuthenticationFailureHandler
                        )
                        , UsernamePasswordAuthenticationFilter.class)
                //添加token驗(yàn)證過(guò)濾器
                .addFilterBefore(new JwtAuthenticationFilter(jwtStoreService), LogoutFilter.class);
        //退出
        http
                .logout()
                // URL
                .logoutUrl("/user/logout")
                // 登出處理
                .addLogoutHandler(customLogoutHandler)
                // 登出成功處理
                .logoutSuccessHandler(customLogoutSuccessHandler);
        //攔截設(shè)置
        http
                .authorizeRequests()
                //公開以下urls
                .antMatchers(securityProperties.getPublicPaths()).permitAll()
                //其他路徑必須驗(yàn)證
                .anyRequest().authenticated();
        //異常處理
        http
                .exceptionHandling()
                // 未登錄處理
                .authenticationEntryPoint(customAuthenticationEntryPoint)
                // 無(wú)權(quán)限處理
                .accessDeniedHandler(customAccessDeniedHandler);
        //關(guān)閉session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        // 關(guān)閉cors
        http.cors().disable();
        // 關(guān)閉csrf
        http.csrf().disable();
        // 關(guān)閉headers
        http.headers().frameOptions().disable();
        return http.build();
    }
}

到此這篇關(guān)于新版SpringSecurity安全配置說(shuō)明的文章就介紹到這了,更多相關(guān)SpringSecurity安全配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MyBatisPlus3如何向數(shù)據(jù)庫(kù)中存入List

    MyBatisPlus3如何向數(shù)據(jù)庫(kù)中存入List

    本文主要介紹了Mybatis Plus的類型處理器的使用,通過(guò)User.java和UserMapper.xml示例進(jìn)行詳細(xì)的解析,并提供了JSON解析器的使用方法,希望通過(guò)這篇文章,可以幫助大家更好的理解和掌握Mybatis Plus的類型處理器
    2024-10-10
  • Java 多線程實(shí)例詳解(三)

    Java 多線程實(shí)例詳解(三)

    本文主要介紹 java 線程安全的知識(shí),這里整理了相關(guān)資料及實(shí)現(xiàn)示例代碼,有興趣的小伙伴可以參考下
    2016-09-09
  • Java利用happen-before規(guī)則如何實(shí)現(xiàn)共享變量的同步操作詳解

    Java利用happen-before規(guī)則如何實(shí)現(xiàn)共享變量的同步操作詳解

    這篇文章主要給大家介紹了關(guān)于Java利用happen-before規(guī)則實(shí)現(xiàn)共享變量的同步操作的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • 在SpringBoot中添加Redis及配置方法

    在SpringBoot中添加Redis及配置方法

    這篇文章主要介紹了在SpringBoot中添加Redis及配置redis的代碼,需要的朋友可以參考下
    2018-10-10
  • Java?導(dǎo)出Excel增加下拉框選項(xiàng)

    Java?導(dǎo)出Excel增加下拉框選項(xiàng)

    這篇文章主要介紹了Java?導(dǎo)出Excel增加下拉框選項(xiàng),excel對(duì)于下拉框較多選項(xiàng)的,需要使用隱藏工作簿來(lái)解決,使用函數(shù)取值來(lái)做選項(xiàng),下文具體的操作詳情,需要的小伙伴可以參考一下
    2022-04-04
  • Java中System.setProperty()用法與實(shí)際應(yīng)用場(chǎng)景

    Java中System.setProperty()用法與實(shí)際應(yīng)用場(chǎng)景

    System.setProperty是Java中用于設(shè)置系統(tǒng)屬性的方法,它允許我們?cè)谶\(yùn)行時(shí)為Java虛擬機(jī)(JVM)或應(yīng)用程序設(shè)置一些全局的系統(tǒng)屬性,下面這篇文章主要給大家介紹了關(guān)于Java中System.setProperty()用法與實(shí)際應(yīng)用場(chǎng)景的相關(guān)資料,需要的朋友可以參考下
    2024-04-04
  • java正則替換img標(biāo)簽中src值的方法

    java正則替換img標(biāo)簽中src值的方法

    今天小編就為大家分享一篇java正則替換img標(biāo)簽中src值的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • Nacos配置文件使用經(jīng)驗(yàn)及CAP原則詳解

    Nacos配置文件使用經(jīng)驗(yàn)及CAP原則詳解

    這篇文章主要為大家介紹了Nacos配置文件使用經(jīng)驗(yàn)及CAP規(guī)則詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-02-02
  • IDEA如何快速構(gòu)建UML類圖

    IDEA如何快速構(gòu)建UML類圖

    UML類圖是一種用于描述軟件系統(tǒng)靜態(tài)結(jié)構(gòu)的可視化建模語(yǔ)言,它通過(guò)類、屬性、方法以及它們之間的關(guān)系來(lái)表示系統(tǒng),類圖主要用于面向?qū)ο笤O(shè)計(jì),幫助理解系統(tǒng)的結(jié)構(gòu)和類之間的關(guān)系,IDEA提供了自動(dòng)生成UML類圖的功能,但其局限性在于只能基于現(xiàn)有代碼生成
    2025-02-02
  • 使用kotlin編寫spring cloud微服務(wù)的過(guò)程

    使用kotlin編寫spring cloud微服務(wù)的過(guò)程

    這篇文章主要介紹了使用kotlin編寫spring cloud微服務(wù)的相關(guān)知識(shí),本文給大家提到配置文件的操作代碼,給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09

最新評(píng)論

娱乐| 嘉荫县| 城步| 云安县| 宁城县| 汝城县| 库尔勒市| 孟津县| 璧山县| 开远市| 边坝县| 油尖旺区| 固安县| 承德市| 宁南县| 贵溪市| 象山县| 抚顺市| 张家川| 永靖县| 镇康县| 岑溪市| 元朗区| 阿拉善盟| 琼结县| 凉城县| 信丰县| 文安县| 土默特左旗| 阿拉善左旗| 察雅县| 读书| 沙洋县| 广安市| 新竹市| 宝清县| 沂水县| 凤凰县| 临清市| 龙游县| 车险|