• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

top.html에 로그인 링크를 만들어서 로그인을 해봤습니다

24.02.24 22:44 작성 조회수 87

0

안녕하세요.


지금 개발환경은 스프링부트, 시큐리티 모두 최신 버전입니다.

permitAll()로 static resource를 허용하고 싶은데요. 설정을 분리하니까 login 하고 나서 css 파일이 화면에 나타납니다. 로그인 버튼은 top.html에 링크를 추가했습니다.

ignoring()을 쓰거나 설정을 통합하면 문제가 해결되긴 합니다.

    http.authorizeHttpRequests(registry -> registry
        .requestMatchers("/mypage").hasRole("USER")
        .requestMatchers("/messages").hasRole("MANAGER")
        .requestMatchers("/config").hasRole("ADMIN")
        .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
        .requestMatchers("/").permitAll()
        .anyRequest().authenticated());

하지만 저는 설정을 두 개로 유지하면서, permitAll()을 써도 문제가 없었으면 하는데, 제가 아는 선에서는 방법을 찾기 어려워 질문드리게 됐습니다.

  @Order(0)
  @Bean
  SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

    http.authorizeHttpRequests(registry -> registry
        .requestMatchers("/mypage").hasRole("USER")
        .requestMatchers("/messages").hasRole("MANAGER")
        .requestMatchers("/config").hasRole("ADMIN")
        .requestMatchers("/").permitAll()
        .anyRequest().authenticated());

    http.formLogin(Customizer.withDefaults());

    http.logout(config -> config.logoutSuccessUrl("/"));

    http.userDetailsService(userDetailsService);

    return http.build();
  }

  @Order(1)
  @Bean
  SecurityFilterChain resource(HttpSecurity http) throws Exception {

    http.authorizeHttpRequests(registry -> registry
        .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
        .anyRequest().authenticated());

    return http.build();
  }

감사합니다.

답변 2

·

답변을 작성해보세요.

1

이 부분도 제가 직접 테스트 해 봐야 할 수 있을 것 같습니다.

전체 소스를 공유 부탁드립니다.

그리고 스프링 시큐리티 최신 버전에 관련된 질문은 경우에 따라서는 강의 범위를 넘어설 수 있기 때문에 정확한 답변들 드리기 어려운 점 양해 부탁드립니다.

literate_t님의 프로필

literate_t

질문자

2024.02.24

넵! 강의 범위를 벗어난 점 알고 있습니다ㅠ도저히 이유를 모르겠어서 여쭙게 되었습니다..

브랜치 주소입니다!

https://github.com/literate-t/corespringsecurity_/tree/question/login-error

아래는 application.yml입니다!

application.yml

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: postgres
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true
    properties:
      hibernate:
        format_sql: true

  thymeleaf:
    cache: false

  devtools:
    livereload:
      enabled: true
    restart:
      enabled: true

음..

테스트를 해 보면 정상적으로 로그인되고 홈 화면으로 이동하고 있는데요...

css 파일은 나타나지 않는데요..

이게 저와는 다른 테스트 환경이 될 수도 있어서 동일하게 재현이 안되는군요

오류가 났을 때 어떤 상황인지 좀 더 자세한 설명이 가능할까요?

image

literate_t님의 프로필

literate_t

질문자

2024.02.27

아아 정상 동작이 되시나요? 신기하군요..최초 질문글에 올렸던 사진처럼 로그인 이후에 css 파일 경로도 페이지가 리다이렉트 되어 버립니다..ㅋㅋ

실습 진행하면서 security filter chain을 여러 개 구성하면서도 문제가 없게끔 계속 다듬어 가고 있는데, 일단 정적 자원을 허용하는 코드는 따로 분리 안 하고 진행하고 있습니다!

0

김윤서님의 프로필

김윤서

2024.04.19

저도 동일한 현상 나타납니다! css/base.css 경로로 이동합니다ㅠㅠ