SpringSecurity的安全過(guò)濾器鏈功能詳解
主要是用于配置Spring Security的安全過(guò)濾器鏈(SecurityFilterChain
),以及定義用戶認(rèn)證和授權(quán)的相關(guān)配置。具體來(lái)說(shuō),代碼實(shí)現(xiàn)了以下功能:
- 配置安全過(guò)濾器鏈:定義了哪些URL路徑需要進(jìn)行認(rèn)證,哪些路徑可以匿名訪問(wèn),以及如何處理登錄、注銷(xiāo)和
CSRF
防護(hù)等。 - 用戶認(rèn)證:使用內(nèi)存中的用戶詳情服務(wù)(
InMemoryUserDetailsManager
)來(lái)管理用戶信息,并使用BCrypt
密碼編碼器(BCryptPasswordEncoder
)對(duì)用戶密碼進(jìn)行加密
@Configuration(proxyBeanMethods = false) public class SecuritySecureConfig { private final AdminServerProperties adminServer; private final SecurityProperties security; public SecuritySecureConfig(AdminServerProperties adminServer, SecurityProperties security) { this.adminServer = adminServer; this.security = security; } /** * 用于配置Spring Security的安全過(guò)濾器鏈,以及定義用戶認(rèn)證和授權(quán)的相關(guān)配置。 * 通過(guò)這些配置,可以實(shí)現(xiàn)對(duì)不同URL路徑的訪問(wèn)控制,以及用戶的登錄、注銷(xiāo)和“記住我”等功能 */ @Bean protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception { // 配置安全過(guò)濾器鏈:定義了哪些URL路徑需要進(jìn)行認(rèn)證,哪些路徑可以匿名訪問(wèn),以及如何處理登錄、注銷(xiāo)和CSRF防護(hù) SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); successHandler.setDefaultTargetUrl(this.adminServer.path("/")); http.authorizeHttpRequests((authorizeRequests) -> authorizeRequests // .requestMatchers(new AntPathRequestMatcher(this.adminServer.path("/assets/**"))) .permitAll() .requestMatchers(new AntPathRequestMatcher(this.adminServer.path("/actuator/info"))) .permitAll() .requestMatchers(new AntPathRequestMatcher(adminServer.path("/actuator/health"))) .permitAll() .requestMatchers(new AntPathRequestMatcher(this.adminServer.path("/login"))) .permitAll() .dispatcherTypeMatchers(DispatcherType.ASYNC) .permitAll() .anyRequest() .authenticated()) /* formLogin:配置表單登錄。 loginPage:指定自定義的登錄頁(yè)面。 successHandler:指定認(rèn)證成功后的處理器。 logout:配置注銷(xiāo)功能。 httpBasic:?jiǎn)⒂肏TTP基本認(rèn)證。 */ .formLogin( (formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler)) .logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))) .httpBasic(Customizer.withDefaults()); /* * addFilterAfter:在指定的過(guò)濾器之后添加自定義的CSRF過(guò)濾器。 * csrf:配置CSRF防護(hù)。 * csrfTokenRepository:設(shè)置CSRF令牌的存儲(chǔ)方式。 * csrfTokenRequestHandler:設(shè)置CSRF令牌的請(qǐng)求處理器。 * ignoringRequestMatchers:忽略某些URL路徑的CSRF防護(hù)。 */ http.addFilterAfter(new CustomCsrfFilter(), BasicAuthenticationFilter.class) .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .csrfTokenRequestHandler(new CsrfTokenRequestAttributeHandler()) .ignoringRequestMatchers( new AntPathRequestMatcher(this.adminServer.path("/instances"), "POST"), new AntPathRequestMatcher(this.adminServer.path("/instances/*"), "DELETE"), new AntPathRequestMatcher(this.adminServer.path("/actuator/**")) )); http.rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600)); return http.build(); } /* rememberMe:配置“記住我”功能。 key:設(shè)置“記住我”功能的密鑰。 tokenValiditySeconds:設(shè)置“記住我”令牌的有效期。 */ @Bean public InMemoryUserDetailsManager userDetailsService(PasswordEncoder passwordEncoder) { UserDetails user = User.withUsername("macro") .password(passwordEncoder.encode("123456")) .roles("USER") .build(); return new InMemoryUserDetailsManager(user); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } }
自定義CSRF過(guò)濾器
public class CustomCsrfFilter extends OncePerRequestFilter { public static final String CSRF_COOKIE_NAME = "XSRF-TOKEN"; /** * 它是一個(gè)過(guò)濾器(Filter)的內(nèi)部實(shí)現(xiàn)。 * 該過(guò)濾器的主要功能是處理跨站請(qǐng)求偽造(CSRF)防護(hù),確保每個(gè)請(qǐng)求都包含有效的CSRF令牌 */ @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName()); if (csrf != null) { Cookie cookie = WebUtils.getCookie(request, CSRF_COOKIE_NAME); String token = csrf.getToken(); if (cookie == null || token != null && !token.equals(cookie.getValue())) { cookie = new Cookie(CSRF_COOKIE_NAME, token); cookie.setPath("/"); response.addCookie(cookie); } } filterChain.doFilter(request, response); } }
用于配置Spring Security的安全過(guò)濾器鏈,以及定義用戶認(rèn)證和授權(quán)的相關(guān)配置。通過(guò)這些配置,可以實(shí)現(xiàn)對(duì)不同URL路徑的訪問(wèn)控制,以及用戶的登錄、注銷(xiāo)和“記住我”等功能。
到此這篇關(guān)于SpringSecurity的安全過(guò)濾器鏈的文章就介紹到這了,更多相關(guān)SpringSecurity的安全過(guò)濾器鏈內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java SpringBoot的相關(guān)知識(shí)點(diǎn)詳解
這篇文章主要介紹了SpringBoot的相關(guān)知識(shí)點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-10-10Spring注解@RestControllerAdvice原理解析
這篇文章主要介紹了Spring注解@RestControllerAdvice原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11JDK1.8源碼下載及idea2021導(dǎo)入jdk1.8源碼的詳細(xì)步驟
這篇文章主要介紹了JDK1.8源碼下載及idea2021導(dǎo)入jdk1.8源碼的詳細(xì)步驟,在文章開(kāi)頭就給大家分享了JDK1.8源碼下載地址和下載步驟,告訴大家idea2021.1.3導(dǎo)入JDK1.8源碼步驟,需要的朋友可以參考下2022-11-11Java向Runnable線程傳遞參數(shù)方法實(shí)例解析
這篇文章主要介紹了Java向Runnable線程傳遞參數(shù)方法實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06SpringCloud客戶端報(bào)錯(cuò):- was unable to send&nb
這篇文章主要介紹了SpringCloud客戶端報(bào)錯(cuò):- was unable to send heartbeat!的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05maven配置多個(gè)倉(cāng)庫(kù)的實(shí)現(xiàn)
本文主要介紹了maven配置多個(gè)倉(cāng)庫(kù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01spring boot中的條件裝配bean的實(shí)現(xiàn)
這篇文章主要介紹了spring boot中的條件裝配bean的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12