RestApi ๋ง๋ค๊ธฐ - ์คํ๋ง ์ํ๋ฆฌํฐ ์ ์ฉํ๊ธฐ (12)
- ๊ฐ๋ฐ/restAPI๐ข
- 2021. 3. 17. 21:37

์๋๋ ์คํ๋ง ์ํ๋ฆฌํฐ์ ๋ํด ๊ณต๋ถํ๊ณ ํ๋ ค๋ค๊ฐ
๋๋ฌด ์ค๋ ๊ฑธ๋ฆด ๊ฒ ๊ฐ์ ์๋ตํ๊ณ ์ ์ฉํ๋๊ฒ๋ถํฐ ์งํํ๋ ค๊ณ ํ๋ค.
์๋ฌดํผ ์คํ๋ง ์ํ๋ฆฌํฐ๋ฅผ ์ ์ฉํ๋ ์ด์ ๋
๊ฒ์๊ธ์ ๋ง๋ ์ ์ ๋ง ์์ ์ด ๊ฐ๋ฅํ๊ฒ ๋ง๋ค๊ธฐ ์ํด ์ ์ฉํ๋๊ฑธ๋ก ์๊ณ ์๋ค.
์๋ฌดํผ... ์ ์ฉํด๋ณด์.
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.1.10.RELEASE</version>
</dependency>
์ด๊ฒ์ ์ถ๊ฐํด์ค์ผ ์คํ๋ง ์ํ๋ฆฌํฐ๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค.
(์ด๊ธ์ ๋ฐฑ๊ธฐ์ ๋ ๊ฐ์๋ฅผ ๋ณด๊ณ ์์ฑํ ๊ธ์ด๊ธฐ๋๋ฌธ์ start.security๋ฅผ ์ฌ์ฉํ์ง ๋์ง ์์ต๋๋ค.)
์๊ฐํด๋ณด๋ vo๋ฅผ ๋ง๋ค์ง ์์๋ค.
@Getter @Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String username;
private String password;
@ElementCollection(fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
private Set<AccountRole> roles;
}
์ด์ ํ
์คํธ ์ฝ๋๋ฅผ ์์ฑํด๋ณด์.
@SpringBootTest
@Import(RestDocsConfiguration.class)
@ActiveProfiles("test")
class AccountServiceTest {
@Autowired
private AccountService accountService;
@Test
void loadUser() {
String username = "klom";
String password = "1234";
Account account = Account.builder()
.username(username)
.password(password)
.roles(Set.of(AccountRole.ADMIN, AccountRole.USER))
.build();
UserDetails userDetails = accountService.loadUserByUsername(username);
assertThat(userDetails.getPassword()).isEqualTo(password);
}
}
๊ฐ๋ตํ ์ค๋ช
ํ๋ฉด account์์ ๋ฐ์ ์ ๋ณด๋ accountService์์ ๊ตฌํ ํจ์ค์๋๊ฐ ์ผ์นํ๋์ง ํ์ธํ๋ ํ
์คํธ ์ฝ๋๋ค.
๋ฌผ๋ก ์ด๊ฒ์ ์คํํด๋ดค์ ์๋ฏธ๊ฐ ์๋ค.
1. account๋ db์ ์ ์ฅํ์ง ์์๋ค.
2. accountService๋ฅผ ๊ตฌํํ์ง ์์๋ค.
1๋ฒ ๋ถํฐ ์ ์ฉํด๋ณด์.
์ ์ฉ๋ค ์คํํด๋ณด๋.

nullPointExeption์ด ๋ฐ์ํ๋ค.
๊ทธ๋ฌ๋ฉด userService์ ์์ ํด์ฃผ๋ฉด ๋๋ค.
@Service
public class AccountService implements UserDetailsService {
private final AccountRepository accountRepository;
public AccountService(
AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Optional<Account> optAccount = accountRepository.findByUsername(username);
Account account = optAccount
.orElseThrow(() -> new UsernameNotFoundException("์ ์ ์ ๋ณด๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค."));
return new User(account.getUsername(), account.getPassword(), authorities(account.getRoles()));
}
private Collection<? extends GrantedAuthority> authorities(Set<AccountRole> roles) {
return roles.stream().map(r -> new SimpleGrantedAuthority("_ROLE"+r.name())).collect(Collectors.toSet());
}
}
๊ทธ๋ฆฌ๊ณ ์คํ์ ํด๋ณด๋ฉด...

๋คํํ ์ฑ๊ณตํ๋ค๋๊ฒ์ ์ ์ ์๋ค.
์๊ฐํด๋ณด๋ ํ๊ฐ์ง ์์ธ๋ฅผ ๋นผ๋จน์๋ค.
๋ฐ๋ก ์๋ ์ฌ์ฉ์๋ฅผ ์
๋ ฅํ์ ๊ฒฝ์ฐ๋ ์ด๋ป๊ฒ ์ฒ๋ฆฌ๋ฅผ ํด์ผํ ๊น?
@Test
void loadEmptyUser() {
assertThrows(
UsernameNotFoundException.class,
() -> {
String username = "klomlly";
accountService.loadUserByUsername(username);
}
);
}
์ด๋ ๊ฒ ์์ธ๋ฅผ ์ฒ๋ฆฌํ๋ฉด๋๋ค.
์ด ๊ธฐ๋ฅ์ junit5์ ๋ค์ด์จ ๊ธฐ๋ฅ์ผ๋ก ์์ธ ์ฒ๋ฆฌ๋ฅผ ํ ๋ ์ฌ์ฉํ๋ค๊ณ ํ๋ค.
์ด๋ ๊ฒ ์คํํ๋ฉด ์ฑ๊ณตํ๋ค.
๊ทธ๋ฐ๋ฐ ๊ณผ์ฐ ์ด ์ฝ๋๊ฐ ์ฑ๊ณตํ๋ค๊ณ ์ด๋ป๊ฒ ์ฅ๋ดํ ์ ์์๊น?
๊ทธ๋์ db์ ์กด์ฌํ๋ ์ ์ ๋ฅผ ์
๋ ฅํ์ ๊ฒฝ์ฐ ์ด๋ป๊ฒ ๋ ๊น?
@Test
void loadEmptyUser() {
String username = "klom";
String password = "1234";
Account account = Account.builder()
.username(username)
.password(password)
.roles(Set.of(AccountRole.ADMIN, AccountRole.USER))
.build();
accountRepository.save(account);
assertThrows(
UsernameNotFoundException.class,
() -> {
// String username = "klomlly";
accountService.loadUserByUsername(username);
}
);
}
๋์ถฉ ์ฝ๋๋ ์ด๋ ๊ฒ ์์ ํ์๋ค.
๊ฒฐ๊ณผ๋

์ ์์ธ์ฝ๋๊ฐ ๋ฐ์ํ๋ค๊ณ ํ๋๋ฐ, ์ด์งธ์ ์ ์์ธ ์ฝ๋๊ฐ ๋ค์ด์๋๊ณ ๋งํ๋ ๊ฒ ๊ฐ๋ค.
...
'๊ฐ๋ฐ > restAPI๐ข' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| RestApi ๋ง๋ค๊ธฐ - ์คํ๋ง ์ํ๋ฆฌํฐ ์ค์ (13) (0) | 2021.03.23 |
|---|---|
| RestApi ๋ง๋ค๊ธฐ - ์ด๋ฒคํธ ์์ API๊ตฌํ (11) (0) | 2021.03.11 |
| RestApi ๋ง๋ค๊ธฐ - ์ด๋ฒคํธ ์กฐํ API๊ตฌํ (10) (0) | 2021.03.09 |
| RestApi ๋ง๋ค๊ธฐ - ์ด๋ฒคํธ ๋ชฉ๋ก ์กฐํ API๊ตฌํ (9) (0) | 2021.03.06 |
| RestApi ๋ง๋ค๊ธฐ - API์ธ๋ฑ์ค ์ง์ ๋ง๋ค๊ธฐ (8) (0) | 2021.03.03 |