• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

강사님 하나 이해가 되지 않는 부분이 있어 질문드립니다!

23.05.15 07:37 작성 조회수 418

0

JwtAuthorizationFilter객체에서 권한처리를 하신다고 하셨는데

@Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {

        String jwtHeader = request.getHeader(JwtProperties.HEADER_STRING);

        if (jwtHeader==null || !jwtHeader.startsWith(JwtProperties.TOKEN_PREFIX)){
            chain.doFilter(request,response);
            return;
        }

        String jwtToken = request.getHeader(JwtProperties.HEADER_STRING).replace(JwtProperties.TOKEN_PREFIX,"");
        String username
                = JWT.require(Algorithm.HMAC512(JwtProperties.SECRET)).build().verify(jwtToken)
                .getClaim("username").asString();
        if (username!=null){

            Optional<Customer> optionalCustomer = customerRepository.findByUsername(username);
            if (optionalCustomer.isPresent()){
                Customer customerEntity = optionalCustomer.get();
                PrincipalDetails principalDetails = new PrincipalDetails(customerEntity);
                Authentication authentication =
                        new UsernamePasswordAuthenticationToken(principalDetails,null,principalDetails.getAuthorities());
                System.out.println("*******************"+principalDetails.getAuthorities().);

                SecurityContextHolder.getContext().setAuthentication(authentication);

                chain.doFilter(request,response);
            }
        }
    }

이 코드에서 인증이나 권한이 필요한 주소요청이 있을 경우 해당 필터를 타고 JWT 토큰을 검증해서 정상적인 사용자인지 확인하고 강제로 Security 세션에 접근하여 Authentication 객체를 저장한다고 이해를 하였습니다.

  1. doFilterInternal() 함수의 어디부분에서 권한을 확인을하고 SpringSecurity클래스에서 .antMatchers("/customer/**")부분의 권한을 막아주나요??

     

 

답변 1

답변을 작성해보세요.

1

권한 처리는 저 코드에서 하지 않습니다. 저 코드에서는 권한처리를 위해 세션을 생성하는 필터입니다.

세션만 생성되면 시큐리티가 권한처리를 합니다.