728x90
์๋ฌ ์ค๋ช
์๋ฐ ํ๋ก์ ํธ ์ค 2๊ฐ์ ํด๋์ค๊ฐ ์๋ก๋ฅผ ์ฐธ์กฐํ๋ ํํ๋ฅผ ๊ฐ์ง ๋ ์๊ธฐ๋ ์๋ฌ
SecurityConfig.java
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private PrincipalOauth2UserService principalOauth2UserService;
// ๋ฌธ์ ๋๋ ๋ถ๋ถ
@Bean
public BCryptPasswordEncoder encodePwd() {
return new BCryptPasswordEncoder();
}
}
PrincipalOauth2UserService.java
@Service
public class PrincipalOauth2UserService extends DefaultOAuth2UserService {
// ๋ฌธ์ ๋๋ ๋ถ๋ถ
private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
}
- SecurityConfig --> PrincipalOauth2UserService --> BCryptPasswordEncoder --> SecurityConfig ๊ฐ ๋์ด ์ํ ์ฐธ์กฐ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
ํด๊ฒฐ
1. ์ฐธ์กฐ๋๋ ๋ฉ์๋๋ฅผ main์ผ๋ก ์ด๋ํ๊ธฐ
GuguMain.java
@SpringBootApplication
@EnableJpaAuditing
public class GuguMain {
// ์ด๋ฐ์์ผ๋ก main ํด๋์ค์์ ๋น์ ๋ฑ๋ก์ํค๊ธฐ
@Bean
public BCryptPasswordEncoder encodePwd() {
return new BCryptPasswordEncoder();
}
public static void main(String[] args) {
SpringApplication.run(GuguMain.class, args);
}
}
2. ์ฐธ์กฐ๋๋ ๋ฉ์๋๋ฅผ ๋ค๋ฅธ ๊ฐ๋ณ ํด๋์ค๋ก ์ด๋ํ๊ธฐ
CustomBCryptPasswordEncoder.java
@Component
public class CustomBCryptPasswordEncoder {
// ์๋์ฒ๋ผ ๋น ๋ฑ๋กํ ์ ์์
public class CustomBCryptPasswordEncoder extends BCryptPasswordEncoder {
}
SecurityConfig.java
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private PrincipalOauth2UserService principalOauth2UserService;
}
- ์์์ ๋ฌธ์ ๋์๋ ์ฝ๋ ์ ๊ฑฐ
PrincipalOauth2UserService.java
@Service
public class PrincipalOauth2UserService extends DefaultOAuth2UserService {
// ๋น ์ปจํ
์ด๋์ ๋ฑ๋ก๋์ด ์๋ BCryptPasswordEncoder ์ฌ์ฉํ๊ธฐ
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
}
- SecurityConfig.java ์์ ๋ ์ด์ BCryptPasswordEncoder ๋น ๋ฑ๋ก์ ํ์ง ์๊ธฐ ๋๋ฌธ์ ์๋ก ์ฐธ์กฐํ๋ ํํ๊ฐ ์๋๋ผ SecurityConfig --> PrincipalOauth2UserService --> BCryptPasswordEncoder ๊ฐ ๋์ด ์ํ ์ฐธ์กฐ๊ฐ ํด๊ฒฐ๋๋ค.
๋๊ธ