기본 DaoAuthenticationProvider사용시 인증객체 조회 에러
1016
작성한 질문수 12
프로젝트를 다시 만들어 실습을 진행중에 있습니다.
이번 프로젝트에서는 CustomAuthenticationProvider를 생성하지 않고 기본으로 사용되는 DaoAuthenticationProvider를 사용할려고 CustomAuthenticationProvider를 등록하지 않았습니다.
그런데
@GetMapping("/denied")
public String accessDenied(@RequestParam(value = "exception", required = false) String exception, Model model) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Account account = (Account)authentication.getPrincipal();
model.addAttribute("username", account.getUsername());
model.addAttribute("exception", exception);
return "user/login/denied";
}이 컨트롤러에서 인증 객체 조회가 되지 않아 에러가 발생했습니다.
기존 프로젝트의 CustomAuthenticationProvider에서의 인증 로직이 끝나고 UsernamePasswordAuthenticationToken타입으로 인증객체가 return되고 이것이 SecurityContext에 저장되는 흐름과
현재 진행중 프로젝트의 DaoAuthenticationProvider에서 인증 로직이 끝나고UsernamePasswordAuthenticationToken타입으로 인증객체가 return되고 이것이 SecurityContext에 저장되는 흐름이 완전히 동일하다고 생각되어서
기존 프로젝트와 똑같이 컨트롤러에서 인증객체가 조회될 것이라고 생각했는데 그렇지 않습니다.
혹시 인증 흐름상에서 제가 놓치고 있는 부분이 있는건가요?
답변 1
0
네 일단 DaoAuthenticationProvider 가 실행이 되지 않아서 그런 것 같습니다.
왜 인지는 정확히 테스트 해 봐야 될 것 같습니다.
소스 공유 가능할까요?
0
https://github.com/woohyeonjoe/test
그런데 제가 디버깅을 걸어봤을땐 DaoAuthenticationProvider가 작동하는 것으로 확인되였습니다. 그래서 로그인이 정상적으로 작동하였고 이후 로그아웃 코드에서 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
해당 코드가 정상작동하여 인증객체 조회를 하여 로그아웃 처리가 되었습니다.
그런데 이상하게 denied 컨트롤러에서만 Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 이 코드로 인증객체 조회가 불가능합니다.
현 프로젝트는 Mysql로 진행하였고, 로그인시 email/password로 로그인하게 변경하였습니다.
1
네
인증객체 조회가 안되는 것이 아니라 타입캐스팅이 잘못 되어 있었습니다.
@GetMapping("/denied")
public String accessDenied(@RequestParam(value = "exception", required = false) String exception, Model model) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Account account = (Account)authentication.getPrincipal();
model.addAttribute("username", account.getUsername());
model.addAttribute("exception", exception);
return "user/login/denied";
}위에 보시면 다음과 같은 코드가 있는데
Account account = (Account)authentication.getPrincipal();principal 은 Account 가 아니라 AccountContext 로 저장되어 있습니다.
그래서
AccountContext account = (AccountContext)authentication.getPrincipal(); 와 같이 AccountContext 로 캐스팅 하시면 됩니다.
다만 CustomAuthenticationProvider 에서는 아래와 같이
UsernamePasswordAuthenticationToken authenticationToken
= new UsernamePasswordAuthenticationToken(accountContext.getAccount(), null, accountContext.getAuthorities());principal 속성에 accountContext.getAccount() 를 직접 저장했기 때문에 오류가 발생하지 않았습니다.
DaoAuthenticationProvider 는 UserDetails 타입의 AccountContext 객체를 principal 에 저장하는 차이가 있다고 보시면 됩니다.
시큐리티 공부 버전 질문
0
175
1
[해결 방법] MethodSecurityConfig.customMethodSecurityMetadataSource() 호출하지 않는 이슈
0
186
1
AbstractSecurityInterceptor.class.beforeInvocation()를 2번 실행하는 경우
0
174
1
강의 코드가 왜이렇게 뒤죽박죽인가요...
0
249
1
메인 페이지로 접속해도 login url로 리다이렉트가 되지 않습니다..
0
236
1
파라미터값이 넘어가지 않습니다 ....
0
374
1
security filterChain 설정 질문이 있습니다.
0
332
1
소스 부분 질문 드립니다.
0
208
2
섹션4 7번 강의 문제가 있는거 같네요.
0
344
2
파일이 수시로 이름이 바껴있네요 ㄷㄷ
0
304
1
HttpSessionSecurityContextRepository를 사용안하는 문제
0
555
2
error , exception 이 잘 안됩니다.
0
282
2
thymeleaf tag 질문합니다.
0
196
2
버전업하면서 deprecated된 것들이 너무많아요
0
478
1
spring security 패치 관련
0
437
1
모바일을 사용할때 토큰말고 세션
0
846
2
DB 연동한 인가 부분에 대한 질문입니다!
0
264
1
Ajax방식도 똑같이 Session방식을 사용하는건가요?
0
307
1
Config 파일 생성 시 질문이 있습니다.
0
225
1
강사님 몇일동안 구글 검색만 100개 했는데도 이유를 모르겠습니다..
1
430
2
403 에러 뜹니다.
0
813
2
login_proc의 존재에 대한 간략한 설명입니다
0
276
1
top.html에 로그인 링크를 만들어서 로그인을 해봤습니다
0
283
2
안녕하세요. DB에 저장될 때 이해 안 가는 값이 있어서 질문드립니다!
0
189
1





