2 글 보임 - 1 에서 2 까지 (총 2 중에서)
-
글쓴이글
-
2024년 12월 24일 13:39 #134402
서다솔참가자auth 변수는 null로 나오고 SecurityContextHolder에서 불러오면 출력이 잘 됩니다.. auth는 왜 null로 나오는걸까요?ㅠㅠ -------------- controller -------------------------------- @GetMapping("/my-page/jwt") @ResponseBody String mypageJWT(Authentication auth){ System.out.println(SecurityContextHolder.getContext().getAuthentication()); if (auth == null) auth = SecurityContextHolder.getContext().getAuthentication();
CustomUser user = (CustomUser) auth.getPrincipal(); System.out.println(user); System.out.println(user.getDisplayName()); System.out.println(user.getAuthorities());
return "mypage.html"; } ------------ filter ----------------------
@Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain ) throws ServletException, IOException {
Cookie[] cookies = request.getCookies(); if (cookies == null){ filterChain.doFilter(request,response); return; }
var jwtCookie = ""; for (int i = 0; i < cookies.length; i++){ if (cookies[i].getName().equals("jwt")){ jwtCookie = cookies[i].getValue(); } }
Claims claim; try { claim = JwtUtil.extractToken(jwtCookie); } catch (Exception e) { filterChain.doFilter(request, response); return; }
CustomUser customUser = new CustomUser(claim.get("username").toString() , "" , Arrays.stream(claim.get("authorities").toString().split(",")).map(x -> new SimpleGrantedAuthority(x)).toList() //authorities ); customUser.setDisplayName(claim.get("displayName").toString());
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(customUser, "" ); authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); SecurityContextHolder.getContext().setAuthentication(authToken); // System.out.println(SecurityContextHolder.getContext().getAuthentication());
filterChain.doFilter(request, response); }
-
글쓴이글
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
- 답변은 로그인 후 가능합니다.