inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링부트 시큐리티 & JWT 강의

스프링부트 시큐리티 27강 - jwt토큰 서버 구축 완료

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

539

qheogus55

작성한 질문수 42

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

     

 

spring spring-security jwt

답변 1

1

최주호

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

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

JWT를 구현한 다음 이 API를 호출해서 사용하는 것은 프론트엔드 쪽에서 하는 역할인가요?

0

98

1

Jwt쓰면 스프링시큐리티는 필수적으로 사용해야하나요?

0

402

1

13:23 system.out 출력문이 다르게 나옵니다.

0

131

1

수료증 문의

0

230

2

9분대에 질문이 있습니다 !

0

117

1

password 비교를 하지 않았는데 어떻게 인증이 통과된 건가요?

0

322

1

이전 강의 참고하라는 말씀

0

253

1

강의 실습하다가 막히는 분들 참고(2024년8월 기준)

2

1118

2

구글 소셜 로그인 302

0

202

1

오류 문의 _ org.springframework.orm.jpa.JpaSystemException: could not deserialize

1

586

1

[자바] 시큐리티 Config 참고

13

954

1

이론강의

0

282

1

SpringSecurity JWT 로그인 URL 2개 설정하는 방법

0

490

1

2024.06기준) 최근 SecurityConfig 설정 문의

0

922

3

구글 로그인시 authentication이 null 값이라고 에러가 발생합니다.

0

681

2

특정 url필터 거는 방법 이슈

0

423

1

강사님께서 말씀하시는 시큐리티세션이 SecurityContext인가요?

0

280

1

25강 마지막 테스트에서 오류

1

1044

2

jwt를 저장하는 위치에 궁금한 점이 있습니다.

0

299

1

mustache를 사용하지 않고 thymeleaf를 사용하려고 하는데

0

700

1

세션 인증방식이 REST 원칙에 위배되는 건가요?

0

343

1

jwt와 실제데이터의 관계

1

247

1

jwt 와 세션ID의 관계

1

314

1

SecurityConfig에서 세션 설정, 인가 설정

0

424

1