芋道 Spring Boot 安全框架 Spring Security 入门 | 芋道源码 —— 纯源码解析博客
按照该文章学习,不过依赖有点过时,现在已经是springboot 3.54, spring security 6.5.2。自己按照官方文档修改,发上来存个档:
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
http
// 禁止 csrf
.csrf(csrf->csrf.disable())
// 登录失败处理
.exceptionHandling((exceptionHandling)->exceptionHandling
.accessDeniedHandler(unauthorizedHandler)
)
// 设置为不需要 session
.sessionManagement((sessionManagement)-> sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(
// 对不同的路径实行不同的策略,之前类似于 /**/*.html 格式的要修改成 /*/*.html 不然会出错
(authorizeHttpRequests)->authorizeHttpRequests
.requestMatchers("/login","/captchaImage").anonymous()
.requestMatchers(HttpMethod.GET,"/*.html","/*/*.html","/*/*.css","/*/*.js").permitAll()
//.requestMatchers("/test").permitAll()
.requestMatchers("/profile/*").anonymous()
.requestMatchers("/common/download*").anonymous()
.requestMatchers("/swagger-ui.html").anonymous()
.requestMatchers("/swagger-resources/*").anonymous()
.requestMatchers("/webjars/*").anonymous()
.requestMatchers("/*/api-docs").anonymous()
.requestMatchers("/druid/*").anonymous()
.anyRequest().authenticated()
)
// 每个请求头部进行定制
.headers((headers)-> headers
.frameOptions((frameOptions)-> frameOptions.disable()))
// 指定退出的路径和成功退出后的处理类
.logout((logout)-> logout
.logoutUrl("/logout")
.logoutSuccessHandler(logoutSuccessHandler))
;
return http.build();
}
欢迎评论,大家探讨学习。
1112

被折叠的 条评论
为什么被折叠?



