강의

멘토링

커뮤니티

Inflearn Community Q&A

qheogus555834's profile image
qheogus555834

asked

Spring Boot Security & JWT Lecture

Spring Boot Security Lecture 27 - JWT Token Server Setup Complete

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

Written on

·

533

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/**")부분의 권한을 막아주나요??

     

 

springspring-securityjwt

Answer 1

1

metacoding님의 프로필 이미지
metacoding
Instructor

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

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

qheogus555834's profile image
qheogus555834

asked

Ask a question