@AuthenticationPrincipal

greenTeaยท2023๋…„ 4์›” 15์ผ
0

@AuthenticationPrincipal null๊ฐ’

๐Ÿ˜ฐ์˜ค๋Š˜ ๊ฐœ์ธ ํ”„๋กœ์ ํŠธ๋กœ ์‹œํ๋ฆฌํ‹ฐ๋ฅผ ์—ฐ์Šตํ•˜๋˜์ค‘ nullpointexception์ด ๋ฐœ์ƒํ•˜์˜€๋‹ค. ์•„๋ž˜๋Š” ๊ทธ ์ฝ”๋“œ์ด๋‹ค.

 @GetMapping("/modify/{id}")
 public String questionModify(QuestionForm questionForm,
@PathVariable Long id,
@AuthenticationPrincipal PrincipalUser auth) {
        log.info("authentication ={}", auth); <--EXCEPTION
        
        ...์ƒ๋žต
}

๐Ÿค”์ฒ˜์Œ์—๋Š” ์ดํ•ด๊ฐ€ ๊ฐ€์ง€ ์•Š์•˜๋‹ค. ๋ถ„๋ช… authentication๊ฐ์ฒด๋ฅผ ์ œ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์•„๋ž˜๋Š” ๊ทธ ๋ฐ˜ํ™˜ ์ฝ”๋“œ๋“ค์ด๋‹ค

 @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        String username = authentication.getName();
        String password = (String) authentication.getCredentials();

        PrincipalUser userDetails = (PrincipalUser) userSecurityService.loadUserByUsername(username);

        if (!passwordEncoder.matches(password, userDetails.getPassword())) {
            throw new BadCredentialsException("๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
        }

        return new UsernamePasswordAuthenticationToken(userDetails.getUsername() , userDetails.getPassword(), userDetails.getAuthorities());
    }

๐Ÿคจ์œ„ ์ฝ”๋“œ์—์„œ PincipalUser๋Š” OAuth2User์™€ UserDetails๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋Š” ์ฝ”๋“œ์ด๋‹ค. ๊ทธ๋ž˜์„œ provider์—์„œ ๋ฐ˜ํ™˜์‹œ UsernamePasswordAuthenticationToken์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•˜์˜€๋Š”๋ฐ ๊ณ„์† null๊ฐ’์ด ๋“ค์–ด์˜ค๋Š” ๊ฒƒ์ด๋‹ค.

๊ทธ๋ž˜์„œ ์ฒ˜์Œ์—๋Š” UsernamePasswordAuthenticationToken์ด ์ œ๋Œ€๋กœ ๋“ค์–ด๊ฐ€์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™์•„ ๋””๋ฒ„๊ทธ๋กœ ๋Œ๋ ค ๋ณด์•˜๋‹ค.

๋งŒ์•ฝ ๋“ค์–ด๊ฐ€์ง€ ์•Š์•˜๋‹ค๋ฉด SecurityContext์—๋Š” ์œ„ ํ† ํฐ๊ฐ’์ด ๋“ค์–ด๊ฐ€์ง€ ์•Š์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๐Ÿซฅ๊ทธ๋Ÿฌ๋‚˜ ์ˆ˜์ฐจ๋ก€ ํ™•์ธํ•ด ๋ณด์•„๋„ ์ธ์ฆ ๊ฐ์ฒด๋Š” ์ œ๋Œ€๋กœ ๋“ค์–ด๊ฐ”๋‹ค.

์›์ธ ํŒŒ์•… ๋ฐ ํ•ด๊ฒฐ

๐Ÿ˜Š๊ทธ๋ž˜์„œ ์ˆ˜๋งŽ์€ ๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด ํ•œ ๋ธ”๋กœ๊ฑฐ ๋‹˜์˜ ๊ธ€์„ ํ†ตํ•ด ์›์ธ์„ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.
์ด์œ ๋Š” ๋ฐ”๋กœ UsernamePasswordAuthenticationToken์˜ ์ฒซ๋ฒˆ์งธ ์ƒ์„ฑ์ž์— String์ธ getUsername๊ฐ’์„ ๋„ฃ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
์ด๋ก ์ธํ•ด @AuthenticationPrincipal์—๋Š” String ๊ฐ’์ด ๋“ค์–ด์˜ค๊ฒŒ ๋˜๊ณ  PrincipalUser์™€๋Š” ํƒ€์ž…์ด ๋งž์ง€ ์•Š์•„ ๊ฐ‘์ด null์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ ๊ฒƒ์ด์—ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์•„๋ž˜์˜ ์ฝ”๋“œ๋กœ ๊ณ ์นœ ๊ฒฐ๊ณผ ์ œ๋Œ€๋กœ ๋œ principal๊ฐ’์ด ๋“ค์–ด๊ฐ”๋‹ค.


@Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        String username = authentication.getName();
        String password = (String) authentication.getCredentials();

        PrincipalUser userDetails = (PrincipalUser) userSecurityService.loadUserByUsername(username);

        if (!passwordEncoder.matches(password, userDetails.getPassword())) {
            throw new BadCredentialsException("๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
        }

//์ฒซ ๋ฒˆ์งธ ๊ฐ’์— ๋‚ด๊ฐ€ ๋งŒ๋“  Principal๊ฐ’ ๋„ฃ๊ธฐ
        return new UsernamePasswordAuthenticationToken(userDetails , userDetails.getPassword(), userDetails.getAuthorities());
    }

์•„๋ž˜ ๋ธ”๋กœ๊ฑฐ๋‹˜ ์•„๋‹ˆ์˜€์œผ๋ฉด ๋งŽ์€ ์‹œ๊ฐ„์„ ํ•ด๋งธ์„ ๊ฒƒ ๊ฐ™๋‹ค.

์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ: Jemni

profile
greenTea์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€