inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA)

Users Microservice - Spring Security 연동

2023.08.10 기준 Spring Boot 3버전 대 Security Config 설정 파일 공유합니다.

3101

psw

작성한 질문수 4

5

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class WebSecurity {

    private final UserService userService;
    private final BCryptPasswordEncoder bCryptPasswordEncoder;
    private final Environment environment;

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.csrf(AbstractHttpConfigurer::disable)
            .authorizeHttpRequests(request ->{
                request.requestMatchers(antMatcher("/actuator/**")).permitAll();
                request.requestMatchers(antMatcher("/**")).permitAll();})
            //            .headers(header -> header.frameOptions(
            //                frameOptionsConfig -> frameOptionsConfig.disable()))
            .apply(new MyCustomSecurity());

        return http.build();
    }

    public class MyCustomSecurity extends AbstractHttpConfigurer<MyCustomSecurity, HttpSecurity> {

        @Override
        public void configure(HttpSecurity http) throws Exception {

            AuthenticationManager authenticationManager = http.getSharedObject(
                AuthenticationManager.class);
            AuthenticationFilter authenticationFilter = new AuthenticationFilter(authenticationManager, userService, environment);
            http.addFilter(authenticationFilter);
        }

        protected void configure2(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(userService).passwordEncoder(bCryptPasswordEncoder);
        }

    }

}


해당 코드는 앞 내용까지 포함하여 설정이 적용된 버전입니다. 

저같은 경우는 처음부터 mysql로 진행하여서 h2 콘솔을 사용하지 않아 frameOptions를 주석처리 하였으나, 혹여나 h2 콘솔 사용하시는 분은 주석 해제 후 사용하시면 되고, 23.08.10 기준으로 hasIpAddress는 사용 불가능합니다.

spring-boot jpa 아키텍처 spring-cloud kafka msa

답변 1

0

인프런 AI 인턴

안녕하세요, 인프런 AI 인턴이에요.

Spring Boot 3 버전 대의 Security Config 설정 파일을 공유해주셨네요. 해당 설정 파일은 Spring Security를 사용하여 애플리케이션 보안에 대한 설정을 하는 것으로 보입니다.

WebSecurity라는 클래스가 @Configuration과 @EnableWebSecurity 어노테이션을 가지고 있으며, 필요한 의존성들과 환경 설정들을 주입받고 있습니다.

filterChain 메서드에서는 HttpSecurity 객체를 사용하여 보안 설정을 구성합니다. csrf를 disable하고, 요청 패턴에 따라 권한을 설정하고, MyCustomSecurity 클래스를 적용하는 것으로 보입니다.

MyCustomSecurity는 AbstractHttpConfigurer를 상속받은 클래스로, HttpSecurity를 구성하는 configure 메서드를 오버라이드하여 필터를 추가하는 작업을 수행하는 것 같습니다.

AuthenticationFilter 클래스를 생성하여 필터를 추가하고 있는데, 이 필터는 AuthenticationManager, UserService, Environment 등을 사용하여 인증 작업을 수행하는 것으로 보입니다.

해당 설정 파일을 사용하려면 해당 클래스를 프로젝트에 추가하고 필요한 의존성을 설정해야할 것 같습니다. 추가적인 정보나 다른 질문이 있으시면 말씀해주세요!

kafka 업데이트 강의 듣고 시포요

0

83

1

강의 교안

0

71

1

마이크로서비스간 통신 시, 인증 처리

0

79

1

api gateway 에서 인증 처리

0

64

1

섹션 19 질문드립니다

0

51

1

강의 자료 업데이트

0

81

1

부하분산 강의 섹션

0

56

1

강의자료는 어디에서?

0

69

1

강의 자료는 어디서 다운 받을 수 있나요?

0

109

1

전체 사용자 조회시 오류

0

57

1

혹시 pk 외 별도의 id 를 부여한 이유가 있을까요 ??

0

108

2

학습 방향

0

94

2

카프카 커넥터 사용 목적 문의

0

85

2

kafka 강의

0

106

2

서비스 디스커버리 종류

0

86

2

강의 자료에 대해서 궁금해요

0

115

2

GlobalFilter, LoggingFilter가 동작하지 않습니다.

0

88

2

Kafka Source Connect 버전 에러

0

83

2

소스커넥터는 사용안한 거 맞죠?

0

81

2

강의자료 업데이트 문의

0

94

2

강의에서 BCryptPasswordEncoder 에 역할(5-2)

0

56

1

강의 업데이트 계획이 궁금합니다.

0

111

2

MSA 애플리케이션에 Spring Web과 Spring Data JPA를 사용하는 것이 바람직한지 궁금합니다. (MSA 설계와 관련된 질문입니다)

0

160

2

어떤 것이 업데이트 된 건가요?

0

162

2