반응형
이번에는 자동으로 생성된 id 와 password가 아닌 사용자가 직접 생성한 id와 password를 이용해 인증처리를 해보려고 한다.
먼저 application.yml 파일에 가서 설정을 해준다.
spring:
messages:
basename: messages
mvc:
pathmatch:
matching-strategy: ant_path_matcher
security:
user:
name: username
password: passw0rd
서버를 재기동하고 postman에서 확인을 해보면
다음과 같이 401 Unautorized 상태인 것을 확인해볼 수 있다.
application.yml 파일이 설정한대로 id 와 password를 입력해보면
다음과 같이 200으로 정상적인 실행이 되는 것을 확인할 수 있다.
만약 고정되어있는 id가 아닌 데이터베이스나 LDAP 을 연동하여 가져올 수 있도록 수정을 해줄 필요가 있다.
1. config 패키지 아래에 SecurityConfig class 를 생성 후 다음과 같이 코드를 작성한다.
package com.example.restfulwebservice.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
@Configuration
//memory에 설정정보를 같이 로딩
public class SecurityConfig extends WebSecurityConfiguration {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception{
auth.inMemoryAuthentication()
.withUser("kenneth")
.password("{noop}test1234") //{noop} 인코딩 없이 사용할 수 있는
.roles("USER");
}
}
음.. 강의에서는 제대로 기동이 되었는데,, 나는 config 클래스 생성 후 오류가 난다..
조금 더 살펴보고 글을 수정해야할 것 같다..
반응형
'프로그래밍 > SPRING BOOT' 카테고리의 다른 글
[ QueryDSL : Q class ] Q class import 안될 때 해결 방법 (0) | 2023.08.28 |
---|---|
[ 기타 ] 제목미정 (0) | 2023.01.19 |
[인프런 : spring-boot] Spring Security를 이용한 인증 처리 (0) | 2022.09.07 |
[인프런 : spring-boot] HAL Browser를 이용한 HATEOAS 기능 구현 (0) | 2022.09.07 |
[인프런 : spring-boot] Monitoring을 위한 Actuator 설정 (0) | 2022.09.07 |