DevOps/Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA)
Spring Security 적용
:)jun
2023. 4. 4. 18:45
- 애플리케이션에 spring security jar을 Dependency에 추가
- WebSecurityConfigurerAdapter를 상속받는 Security Configuration 클래스 생성
- @EnableWebSecurity 추가
- Authentication -> configure(AuthenticationManagerBuilder auth) 메서드를 재정의
- Password encode를 위한 BCryptPasswordEncoder 빈 정의
- Authorization -> configure(HttpSecurity http) 메서드를 재정의
1. Dependency 추가
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.5.14</version>
</dependency>
WebSecurityConfigurerAdapter를 상속받아 구현할 수 있는 마지막 버전을 찾아 적용.
추후 최신 버전 Security 설정 적용해보자.
2-3. Security Configuration
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests().antMatchers("/users/**").permitAll();
//h2-console 프레임 단위 엑박뜨는 것 해결
http.headers().frameOptions().disable();
}
}
Authorization 관리하는 configure(HttpSecurity http) 메서드 재정의
5. BCryptPasswordEncoder 빈 정의
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
users db에 encryptedPwd로 저장.