HTTP 기본 인증은 HTTP 헤더를 사용하는 인증 방법으로, 간단하게 username과 password를 사용해서 인증을 진행하는 방법입니다.
이때 Base64를 통해 간단한 인코딩을 하여 보내는 것이 일반적입니다.
이렇게 인코딩 된 인증 정보를 HTTP 헤더의 Authorization에 실어서 전송을 합니다.
좀 더 자세한 정보는 아래의 페이지를 참고해 주세요
https://engineerinsight.tistory.com/69
[HTTP] HTTP 기본 인증 (Basic Authentication): 개념과 사용 방법
💋 인증(Authentication) 인증은 사용자의 신원을 확인하는 과정이다. 누군지? 를 보는 것이다. 스프링에서 인증은 보안과 직결되기 때문에 매우 중요한 부분이며, 아래와 같은 절차를 따른다. 1. 사
engineerinsight.tistory.com
매우 간단하게 기본 인증을 적용하는 방법을 사용해보겠습니다.
인증을 받기 위한 계정을 DB가 아닌 코드에 직접 넣어놓는 방식입니다.
DB와 연동하여 기본 인증을 적용하는 방법은 아래의 글을 참고해주세요
https://wellsbabo.tistory.com/43
스프링 시큐리티와 JPA를 활용한 Basic Auth
HTTP 기본 인증은 HTTP 헤더를 사용하는 인증 방법으로, 간단하게 username과 password를 사용해서 인증을 진행하는 방법입니다. 이때 Base64를 통해 간단한 인코딩을 하여 보내는 것이 일반적입니다. 이
wellsbabo.tistory.com
1. 의존성 추가
우선 스프링 시큐리티 의존성을 추가합니다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2. 스프링 시큐리티 설정
스프링 시큐리티 관련 설정 파일은 메인 클래스와 같은 패키지 안에 포함되어 있어야 합니다.
// SecurityConfig.java
@Configuration
@RequiredArgsConstructor
public class SecurityConfig{
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.authorizeRequests(authorize -> {
authorize.requestMatchers("/error/**").permitAll();
authorize.anyRequest().authenticated();
})
.httpBasic(Customizer.withDefaults())
.build();
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean
public UserDetailsService userDetailsService(PasswordEncoder passwordEncoder){
User.UserBuilder users = User.builder();
UserDetails user = users
.username("testUser")
.password(passwordEncoder.encode("test1234"))
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
}
3. 실행 테스트
위에 스프링 시큐리티 설정까지 하면 완성입니다.
이제는 임의의 메서드를 만들어서 테스트해보면 됩니다.
테스트는 위에 링크로 걸어놓은 '스프링 시큐리티와 JPA를 활용한 Basic Auth' 글을 참고해주세요!
감사합니다.
참고
https://sg-choi.tistory.com/m/642
[Spring Security] Basic Authentication
Basic Authentication이란? Authorization 헤더를 활용하는 방식 요청시마다 Authorization: Basic {base64(usename:password)} 형식의 헤더를 포함하여 요청하여 인증을 처리 BasicAuthenticationFilter를 사용 Authorization 헤더
sg-choi.tistory.com
'방구석 컴퓨터 > 방구석 스프링' 카테고리의 다른 글
@RestController (0) | 2024.11.19 |
---|---|
구글 플레이 인앱 결제와 영수증 처리 프로세스 (2) | 2024.10.30 |
스프링 시큐리티와 JPA를 활용한 Basic Auth (0) | 2023.12.06 |
@RequestMapping (0) | 2023.09.13 |
Lombok (0) | 2023.08.17 |