Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated methods of Spring Security with new alternatives #101

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1212,8 +1212,10 @@ import io.github.wimdeblauwe.errorhandlingspringbootstarter.mapper.ErrorMessageM
import io.github.wimdeblauwe.errorhandlingspringbootstarter.mapper.HttpStatusMapper;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;

public class WebSecurityConfiguration {
@Bean
Expand All @@ -1227,11 +1229,11 @@ public class WebSecurityConfiguration {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http,
UnauthorizedEntryPoint unauthorizedEntryPoint) throws Exception {
http.httpBasic().disable();
http.httpBasic(AbstractHttpConfigurer::disable);

http.authorizeHttpRequests().anyRequest().authenticated();
http.authorizeHttpRequests(customizer -> customizer.anyRequest().authenticated());

http.exceptionHandling().authenticationEntryPoint(unauthorizedEntryPoint);//<.>
http.exceptionHandling(customizer -> customizer.authenticationEntryPoint(unauthorizedEntryPoint));//<.>

return http.build();
}
Expand All @@ -1258,6 +1260,7 @@ import io.github.wimdeblauwe.errorhandlingspringbootstarter.mapper.HttpStatusMap
import org.springframework.context.annotation.Bean;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;

public class WebSecurityConfiguration {

Expand All @@ -1272,11 +1275,11 @@ public class WebSecurityConfiguration {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http,
AccessDeniedHandler accessDeniedHandler) throws Exception {
http.httpBasic().disable();
http.httpBasic(AbstractHttpConfigurer::disable);

http.authorizeHttpRequests().anyRequest().authenticated();
http.authorizeHttpRequests(customizer -> customizer.anyRequest().authenticated());

http.exceptionHandling().accessDeniedHandler(accessDeniedHandler);//<.>
http.exceptionHandling(customizer -> customizer.accessDeniedHandler(accessDeniedHandler));//<.>

return http.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.AccessDeniedHandler;
Expand Down Expand Up @@ -130,15 +131,15 @@ public AccessDeniedHandler accessDeniedHandler(HttpStatusMapper httpStatusMapper
public SecurityFilterChain securityFilterChain(HttpSecurity http,
UnauthorizedEntryPoint unauthorizedEntryPoint,
AccessDeniedHandler accessDeniedHandler) throws Exception {
http.httpBasic().disable();
http.httpBasic(AbstractHttpConfigurer::disable);

http.authorizeHttpRequests()
.requestMatchers("/test/spring-security/admin-global").hasRole("ADMIN")
.anyRequest().authenticated();
http.authorizeHttpRequests(customizer -> customizer
.requestMatchers("/test/spring-security/admin-global").hasRole("ADMIN")
.anyRequest().authenticated());

http.exceptionHandling()
.authenticationEntryPoint(unauthorizedEntryPoint)
.accessDeniedHandler(accessDeniedHandler);
http.exceptionHandling(customizer -> customizer
.authenticationEntryPoint(unauthorizedEntryPoint)
.accessDeniedHandler(accessDeniedHandler));

return http.build();
}
Expand Down
Loading