inflearn logo
강의

Course

Instructor

Spring Security

5) Handling authentication and authorization exceptions - AjaxLoginUrlAuthenticationEntryPoint, AjaxAccessDeniedHandler

최신 스프링 시큐리티 - Ajax 로그인 시도 시 쿠키가 오지 않을 때 삽질 기록

872

ttasjwi

13 asked

5

증상

### 로그인(일반 회원)
POST http://localhost:8080/api/login
Content-Type: application/json
X-Requested-With: XMLHttpRequest

{
  "username": "user",
  "password": "1111"
}

### 로그인(매니저)
POST http://localhost:8080/api/login
Content-Type: application/json
X-Requested-With: XMLHttpRequest

{
  "username": "manager",
  "password": "1111"
}

### messages 요청
GET http://localhost:8080/api/messages
Content-Type: application/json
X-Requested-With: XMLHttpRequest

원인

    @Bean
    fun ajaxLoginProcessingFilter(): AjaxLoginProcessingFilter {
        val filter = AjaxLoginProcessingFilter(objectMapper, authenticationManager())
        filter.setSecurityContextRepository(securityContextRepository())
        filter.setAuthenticationSuccessHandler(ajaxAuthenticationSuccessHandler())
        filter.setAuthenticationFailureHandler(ajaxAuthenticationFailureHandler())
        return filter
    }

    @Bean
    fun securityContextRepository(): SecurityContextRepository = DelegatingSecurityContextRepository(HttpSessionSecurityContextRepository(), RequestAttributeSecurityContextRepository())


    @Bean
    fun ajaxAuthenticationProvider() = AjaxAuthenticationProvider(userDetailsService, passwordEncoder())

    @Bean
    fun ajaxAuthenticationSuccessHandler() = AjaxAuthenticationSuccessHandler(objectMapper)

    @Bean
    fun ajaxAuthenticationFailureHandler() = AjaxAuthenticationFailureHandler(handlerExceptionResolver)

    @Bean
    fun ajaxAuthenticationEntryPoint() = AjaxAuthenticationEntryPoint(objectMapper)

    @Bean
    fun ajaxAccessDeniedHandler() = AjaxAccessDeniedHandler(objectMapper)

java spring-boot spring-security

Answer 3

0

dragonbead956795

감사합니다. 덕분에 문제를 해결하였습니다.

저 같은 경우에도 AjaxLoginProcessingFilter의 SecurityContextRepository를 주입하지 않아서 일반 사용자로 로그인한 후 "/api/messages" 요청시 ExceptionTranslationFilter 부분에서 계속 익명사용자로 판단되어 401 에러로 응답되는 문제가 있었습니다. SecurityContext에 사용자의 인증 정보가 저장되지 않아서라는 것은 판단하였는데 폼방식에서 Ajax 방식으로 진행하면서 SecurityContextRepository를 주입하지 않은 것을 놓쳤네요.

0

kyoungik lee

도움이 많이 되었습니다. 감사합니다.

 

0

nahud

감사합니다 덕분에 많은 도움되었습니다!!!🙇‍♂

시큐리티 공부 버전 질문

0

173

1

[해결 방법] MethodSecurityConfig.customMethodSecurityMetadataSource() 호출하지 않는 이슈

0

183

1

AbstractSecurityInterceptor.class.beforeInvocation()를 2번 실행하는 경우

0

172

1

강의 코드가 왜이렇게 뒤죽박죽인가요...

0

246

1

메인 페이지로 접속해도 login url로 리다이렉트가 되지 않습니다..

0

233

1

파라미터값이 넘어가지 않습니다 ....

0

372

1

security filterChain 설정 질문이 있습니다.

0

328

1

소스 부분 질문 드립니다.

0

206

2

섹션4 7번 강의 문제가 있는거 같네요.

0

342

2

파일이 수시로 이름이 바껴있네요 ㄷㄷ

0

302

1

HttpSessionSecurityContextRepository를 사용안하는 문제

0

553

2

error , exception 이 잘 안됩니다.

0

275

2

thymeleaf tag 질문합니다.

0

194

2

버전업하면서 deprecated된 것들이 너무많아요

0

476

1

spring security 패치 관련

0

435

1

모바일을 사용할때 토큰말고 세션

0

843

2

DB 연동한 인가 부분에 대한 질문입니다!

0

262

1

Ajax방식도 똑같이 Session방식을 사용하는건가요?

0

305

1

Config 파일 생성 시 질문이 있습니다.

0

223

1

강사님 몇일동안 구글 검색만 100개 했는데도 이유를 모르겠습니다..

1

427

2

403 에러 뜹니다.

0

809

2

login_proc의 존재에 대한 간략한 설명입니다

0

272

1

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

0

275

2

안녕하세요. DB에 저장될 때 이해 안 가는 값이 있어서 질문드립니다!

0

186

1