AbstractAuthenticationProcessingFilter내에서 HttpServletResponse값이 안들어갑니다
@Bean
public SecurityFilterChain httpSecurity(HttpSecurity http) throws Exception {
return http.cors().disable()
.csrf().disable()
.httpBasic().disable()
.formLogin().disable()
.authorizeRequests().anyRequest().authenticated()
.and()
.addFilterAt(authFilter(), UsernamePasswordAuthenticationFilter.class)
.addFilterAt(jwtFilter(), UsernamePasswordAuthenticationFilter.class)
.build();
}
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return web -> {
web.ignoring().requestMatchers().antMatchers("/h2/**");
};
}
@Bean
public AuthFilter authFilter() throws Exception {
var authFilter = new AuthFilter();
authFilter.setAuthenticationManager(authenticationConfiguration.getAuthenticationManager());
authFilter.setAuthenticationSuccessHandler(new SimpleUrlAuthenticationSuccessHandler("/"));
authFilter.setAuthenticationFailureHandler(new SimpleUrlAuthenticationFailureHandler("/error"));
return authFilter;
}설정은 위와 같이 했습니다.
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
Authentication authResult) throws IOException, ServletException {
super.successfulAuthentication(request, response, chain, authResult);
String SECRET = "secrsdkfjhjh4243j234jh2SDdsfjhgsdfhjgjFQQQQdasd1et";
Claims claims = Jwts.claims();
claims.put("username", SecurityContextHolder.getContext().getAuthentication().getName());
String compact = Jwts.builder()
.setClaims(claims)
.setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 10)) // 1시간
.signWith(SignatureAlgorithm.HS256, SECRET)
.compact();
response.addHeader("Authorization", compact);
response.addCookie(new Cookie("token", compact));
}위와 같이 설정을 했는데 http 요청 테스트를 해보면 설정한 값이 하나도 들어가 있지않습니다.
컨트롤러나 OncePerRequestFilter에서 값을 넣어보면 잘 적용됩니다..
답변 2
0
https://github.com/devhanliam/securiy-debug/tree/main/src/main
답변 감사합니다.
이해하신대로 successfulAuthentication attemptAuthentication에서도 response에 헤더에 값을 넣어도 AbstractAuthenticationProcessingFilter를 상속한 필터에서만 그런 현상이 발견됩니다. 조회되지 않습니다.
0
안녕하세요. 호돌맨입니다.
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
Authentication authResult) throws IOException, ServletException {
super.successfulAuthentication(request, response, chain, authResult); // <-- 이 부분
String SECRET = "secrsdkfjhjh4243j234jh2SDdsfjhgsdfhjgjFQQQQdasd1et";
Claims claims = Jwts.claims();
// ... 생략
}위 코드에서 super.successfulAuthentication(request, response, chain, authResult); 라인을 삭제하시기 바랍니다.
위 코드를 추적해보면 내부적으로 인증관련 세션 생성 후 redirect를 먼저 하기 때문에 이후에 godrn1993님이 설정하신 header 설정이 무의미 한 걸 알수 있습니다.
Intellij 디버그 모드 사용 방법을 익히시는 걸 추천드립니다.
감사합니다.
0
안녕하세요. 호돌맨입니다.
질문을 남겨주셔서 감사합니다.
설정한 값이 하나도 들어가 있지않습니다. 라고 말씀하신 거는 응답 헤더에 값이 포함되지 않는다는 말씀이실까용?
코드가 전체적으로 공개되어 있지 않아 최대한 비슷하게 작성 해봤는데 저는 successfulAuthentication 메서드까지 잘 호출이 됩니다.
breakpoint 를 설정하시고 디버그모드로 해당 메서드까지 호출되는지 확인 해보시면 좋을것 같습니다.
코드를 github에 올려주시면 받아서 확인 해보도록 하겠습니다.
감사합니다.
Deprecated 관련 사항들
0
104
2
깃헙 collaboator 초대 관련
0
89
1
강의 듣다가 도커 이미지 생성시 각각도 가능하나 그렇게 사용하는데가 많은지 모르겠다라는 말을 듣고 남김니다
0
157
2
logout 후에 login 페이지 이동은 어디서 시켜주는건가요?
0
233
1
다중 데이터를 삭제 할 때
0
271
2
querydsl Q class 이슈
0
415
2
Windows WSL Vue 설정
2
247
1
Dip, @transactional
0
189
1
[vite] http proxy error: /auth/login
0
1045
2
로그인 하고 나서 GET요청으로 메인페이지 요청
0
234
2
GitHub Collaborator 초대 관련
0
258
2
Window에서 Vue.js 설정
0
321
2
(솔루션 수정)'tsyringe' Error: TypeInfo not known for "클래스명"
0
713
2
collaboator로 초대받을 수 있을까요??
0
283
2
SecurityMockContext 로부터 유저 정보를 가져오기
0
262
1
given 부분이 길어질 때 어떻게 처리하면 좋을까요?
0
318
1
섹션9 프론트의 코드를 보고싶습니다,,,
0
423
1
Spring Security - defaultSuccessUrl 질문
0
627
1
강의 화면이 나오지 않습니다. 음성과 자막만 나와요
0
302
1
JPAQueryFactory(em)의 객체 생성자 오류에 대해서 질문이 있습니다ㅜㅜ
0
690
2
ExceptionHandler가 AccessDeniedHandler(Http403Handler)를 먹어버리는 현상
0
1182
2
섹션10 언제 나오나요?
0
485
1
CommentService에서 Repository를 호출하지 않는데도
0
343
1
Editor....를 활용한 패턴에 질문있습니다.
0
496
1





