
RESTful APIλ λ€λ₯Έ κ°λ°μκ° API κΈ°λ₯κ³Ό μλ λ°©μμ μ½κ² μ΄ν΄ν μ μλλ‘ λ¬Έμνκ° νμν©λλ€.
κ°λ°μκ° νΌμ μ°λ €κ³ λ§λ€λλΌλ λ¬Έμλ νμν©λλ€.
μ΄λ²μλ OpenAPI 3λ₯Ό μ΄μ©νμ¬:
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
π‘ μΈλΆ λΌμ΄λΈλ¬λ¦¬ μ¬μ© μ
build.gradledependenciesμ μΆκ°κ° νμν©λλ€.
com.example.cardatabase ν¨ν€μ§μ OpenApiConfig ν΄λμ€ μμ±:
@Configuration
public class OpenApiConfig {
@Bean
public OpenAPI carDatabaseOpenApi() {
return new OpenAPI()
.info(new Info()
.title("Car REST API")
.description("My car Stock")
.version("1.0")
);
}
}
@Bean : μ΄ λ©μλμμ λ°ννλ κ°μ²΄λ₯Ό Spring IoC 컨ν
μ΄λκ° κ΄λ¦¬νλλ‘ λ±λ‘new Info() λ΄λΆ: λΉλ ν¨ν΄κ³Ό λ©μλ 체μ΄λ νμ©spring.application.name=cardatabase
spring.datasource.url=jdbc:mariadb://localhost:3310/cardb
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.data.rest.basePath=/api
springdoc.swagger-ui.enabled=true
springdoc.swagger-ui.path=/swagger-ui.html
springdoc.api-docs.path=/api-docs
@Bean@Bean μ λν
μ΄μ
@Configuration ν΄λμ€ μμμ μ¬μ©@Configuration
public class OpenApiConfig {
@Bean
public OpenAPI carDatabaseOpenApi() {
return new OpenAPI()
.info(new Info()
.title("Car REST API")
.description("My car Stock")
.version("1.0")
);
}
}
@Bean μ¬μ© μ¬λΆ λΉκ΅public class MyController {
private MyService myService1 = new MyService();
public void handleRequest() {
myService1.doSomething();
}
}
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
@Controller
public class MyController {
private final MyService myService;
public MyController(MyService myService) {
this.myService = myService;
}
public void handleRequest() {
myService.doSomething();
}
}
κΈ°λ³Έ κΈ°λ₯
/css, /images λ± μ μ 리μμ€ μ κ·Ό νμ©implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.boot:spring-boot-starter-security'
/login νμ΄μ§ μλ μμ±@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public InMemoryUserDetailsManager userDetailsService() {
UserDetails user = User.builder()
.username("user")
.password(passwordEncoder().encode("password"))
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
@EnableWebSecurity : Spring Security κΈ°λ³Έ λ³΄νΈ κ΅¬μ± ν΄μ β 컀μ€ν
κ°λ₯PasswordEncoder : λΉλ°λ²νΈ μνΈν (bcrypt)AppUser)@Entity
@NoArgsConstructor(force = true)
@RequiredArgsConstructor
@Getter
@Setter
public class AppUser {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable=false,unique=true)
private final String username;
@Column(nullable=false)
private final String password;
@Column(nullable=false)
private final String role;
}
@RepositoryRestResource(exported = false)
public interface AppUserRepository extends JpaRepository<AppUser, Long> {
Optional<AppUser> findByUsername(String username);
}
Optional : null μ²λ¦¬ μμ νκ² κ°λ₯@RepositoryRestResource(exported = false) : REST μλ λ
ΈμΆ λ°©μ§@Service
public class UserDetailsServiceImpl implements UserDetailsService {
private final AppUserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Optional<AppUser> user = userRepository.findByUsername(username);
if (user.isPresent()) {
AppUser currentUser = user.get();
return User.withUsername(username)
.password(currentUser.getPassword())
.roles(currentUser.getRole())
.build();
} else {
throw new UsernameNotFoundException("User not found");
}
}
}
UserBuilder νμ©, μΈν°νμ΄μ€ κ°μ ꡬν@Configuration
@EnableWebSecurity
public class SecurityConfig {
private final UserDetailsServiceImpl userDetailsService;
public SecurityConfig(UserDetailsServiceImpl userDetailsService) {
this.userDetailsService = userDetailsService;
}
public void configGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
/api/cars β μΈμ¦ νμ β 401 Unauthorizedπ‘ μ 리 ν¬μΈνΈ
@Bean / Spring IoC β κ°μ²΄ κ΄λ¦¬ λ° DI