inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[개정판 2023-11-27] Spring Boot 3.x 를 이용한 RESTful Web Services 개발

API 사용을 위한 사용자 인증 처리 구현

데이터베이스까지 연동.. 질문드립니다!

177

고재석

작성한 질문수 2

0

안녕하세요. 강의 열심히 잘 보고 있는 취업 준비생입니다!

2개 강의에 걸친 스프링 시큐리티 사용법은 yml, config 에 username, password를 따로 명시하는 방법으로 설명해주셨는데요. 그렇다면 데이터베이스의 특정 테이블의 특정 데이터로 매칭하는 방법은 없나요??

예를 들어 유저가 로그인 할 때는 직접 username과 password를 입력하게 될 텐데 db에 적절한 데이터가 있는 경우에는 그 데이터로 인증을 수행하고, 없는 경우에는 exception을 날리는 식의 프로세스를 생각해보았습니다. 어떤 방법이 있는지 궁금합니다..!

rest-api spring-boot

답변 1

2

Dowon Lee

안녕하세요, 이도원입니다. 

말씀하신 내용은 사용자가 직접 Security 라이브러리를 이용하여 인증처리를 할 수 있는지 여부를 말씀하시는거라면, 가능합니다. 아래와 같이 직접 WebSecurityConfigurerAdapter 를 구현한 클래스를 사용하시면 됩니다.

@Configuration
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private MyUserDetailsService myUserDetailsService;

@Autowired
private AccessDeniedHandler myAccessDeniedHandler;

@Bean
public BCryptPasswordEncoder passwordEncoder() {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
return bCryptPasswordEncoder;
}
...

위 예제의 MyUserDetailService 클래스에서 아래와 같은 loadUserByUsername() 메소드를 구현하실 수 있습니다.

@Service("myUserDetailsService")
public class MyUserDetailsService implements UserDetailsService {

...

@Transactional
@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {

MyUser myUser = myService.getByAdminId(username);
List<GrantedAuthority> authorities = new ArrayList<>();
if (center == null) {
throw new UsernameNotFoundException("User " + username + " was not found in the database");
}
authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));

return buildUserForAuthentication(myUser, authorities);
}

감사합니다.

0

고재석

자세한 답변 감사합니다!

JPA

0

71

1

jpa dependency를 추가하고 SecurityConfig클래스에서 오류가 납니다.

0

84

1

웹 브라우저 400 bad request

0

83

1

@Size는 되는데 @Past는 안 됩니다.

0

76

1

pdf 자료는 없나요?

0

91

2

locale 정보가 null 이면 무조건 messages_ko.properties이 호출 되는 문제

0

99

2

Swagger 강의, Unable to infer base url 이거 뜨시는 분들 도움되시라고

0

133

1

강의에서나온 화면 피피티

0

169

1

HelloWorldBean 관련 에러

0

194

2

Swagger API 3.x 오류..

0

230

1

java: variable message not initialized in the default constructor 에러는 어찌하면 좋을까요?

1

288

1

현재 GIT에 올리신 소스를 실행해봤습니다.

0

203

2

고양이 소리가 귀엽네요 !!

0

149

2

git에서 소스받고 실습중인데

0

127

1

post가 안되요

0

115

1

한국어 같은 경우 언어코드인 messages_ko.properties 로 생성하는게 더 좋지 않나요?

0

176

2

리턴타입으로서 EntityModel<User> 와 ResponseEntity질문

0

112

1

예외처리쪽 관련 질문있습니다.

0

165

2

엔티티가 바로 응답으로 나가도 되나요??

0

239

2

안녕하세여 Cannot invoke "co.kr.joneconsulting.resfulservice.repository.PostRepository.save(Object)" because "this.postRepository" is null

0

150

2

사용자 등록하고 나서 H2 에서 보면 신규 사용자의 password, ssn 이 null 로 되어 있습니다.

0

146

2

ApplictionContext 질문

0

199

2

롬북이 안먹히는것같아요

0

161

1

인텔리제이에서스프링부트 파일 실행하면

0

244

1