Spring Boot Actuator
를 추가해준다.
@EnableWebSecurity
@Configuration
@RequiredArgsConstructor
public class WebSecurity {
private final BCryptPasswordEncoder passwordEncoder;
private final UserService userService;
private final Environment env;
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception{
AuthenticationManagerBuilder authenticationManagerBuilder =
http.getSharedObject(AuthenticationManagerBuilder.class);
authenticationManagerBuilder.userDetailsService(userService).passwordEncoder(passwordEncoder);
AuthenticationManager authenticationManager= authenticationManagerBuilder.build();
http.csrf(AbstractHttpConfigurer::disable);
http.formLogin(form->form
.loginPage("/login")
.permitAll());
http.authorizeHttpRequests(request -> request
.requestMatchers("/actuator/**").permitAll()
.requestMatchers("/h2-console/**").permitAll()
.requestMatchers("/**").permitAll()
);
http.authenticationManager(authenticationManager);
http.addFilter(getAuthenticationFilter(authenticationManager));
http.headers(header -> header
.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable));
return http.build();
}
private AuthenticationFilter getAuthenticationFilter(AuthenticationManager authenticationManager) throws Exception{
return new AuthenticationFilter(userService,authenticationManager,env);
}
}
requestMatchers에 /actuator/**
를 추가하여 /actuator 하위 경로로 오는 요청을 허용해준다. (실제 운영단계에서는 admin Role을 가진 사람만 허용되도록 해야 할 것 같다.)
management:
endpoints:
web:
exposure:
include: info, refresh, health, beans
management.endpoints.web.exposure.include
에 actuator 에서 활성화할 기능을 명시해준다.
refresh(POST): 빌드된 파일은 그대로인데 설정 정보가 변경되었을 때 변경사항을 반영해주기 위해 사용.
health(GET): 현재 서비스의 상태를 표시해줌.
beans(GET): 현재 스프링 컨텍스트에 등록된 모든 빈에 대한 정보를 제공한다. 주로 디버깅과 모니터링 목적으로 사용한다.
httpexchanges(GET): 요청이 어떻게 처리되었는지에 대한 정보가 표시된다.
기본적으로는 컨텍스트에 등록되어있지 않기 때문에 직접 스프링 빈으로 등록시켜주어야한다.