Skip to content

프로젝트 환경구성 과정 #28

@Hello-LSY

Description

@Hello-LSY

커스텀 로그인 페이지 오류

오류 상황

  • url 필터처리 성공 → 커스텀 로그인 페이지 이동 → 알맞은 id,pw 기입 → 오류
  • SecurityConfig 설정에서 AuthenticationManager를 빈등록하니까 발생
  • AuthenticationManager는 spring security 최신버전 기준 디폴트로 작동함

AuthenticationManager관련 500 에러

image

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    

해결

@Configuration
@EnableWebSecurity
@ComponentScan(basePackages = "org.project.backend")
public class SecurityConfig {

    private final MemberDetailsService memberDetailsService;
    private final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

    public SecurityConfig(MemberDetailsService memberDetailsService) {
        this.memberDetailsService = memberDetailsService;
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .and()
                .authorizeRequests()
                .antMatchers("/register", "/login", "/public/**", "/api/members/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/perform_login")
                .defaultSuccessUrl("/", true)
                .successHandler(new CustomAuthenticationSuccessHandler())
                .failureHandler(new CustomAuthenticationFailureHandler());

        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        String idForEncode = "bcrypt";
        Map<String, PasswordEncoder> encoders = new HashMap<>();
        encoders.put("bcrypt", new BCryptPasswordEncoder());
        encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());

        return new DelegatingPasswordEncoder(idForEncode, encoders);
    }

    @Bean
    public UserDetailsService userDetailsService() {
        return memberDetailsService;
    }

    @Bean
    public AuthenticationFailureHandler customAuthenticationFailureHandler() {
        return (request, response, exception) -> {
            logger.error("로그인 실패: {}", exception.getMessage());
            response.sendRedirect("/login?error=true&message=" + exception.getMessage());
        };
    }
}
  • userDetailsService를 자동적으로 처리하는데 userDetailsService 부분을 빈등록해서 명시적으로 처리하게 하려는 의도였지만 순환참조된다는것.
  • userDetailsService을 지워서 해결

401(Unauthorized)

image

반대로 빼는 경우

  • 해당 리소스에 유효한 인증 자격 증명이 없기 때문에 요청이 적용되지 않았음을 나타냄
  • 사용자가 로그인 없이 리소스를 요청한 경우

405

image

@Override
protected Filter[] getServletFilters() {
     return new Filter[]{new DelegatingFilterProxy("springSecurityFilterChain")};
}
  • 필터를 주석하면 이렇게나옴 = 얜 무조건 있어야함

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions