일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #딥러닝 #AI #서버 #자동화 #SQL #기본문법 #데이터베이스 #DBMS #Oracle #MongoDB #아키텍쳐 #DB
- 쓰레드 #쓰레드풀 #프로세스
- 인가 #
- 공간복잡도 #공간자원 #캐시메모리 #SRAM #DRAM #시간복잡도
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #tdd #테스트 주도개발 #테스트코드 #유닛테스트
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #딥러닝 #AI #서버 #자동화 #SQL #기본문법 #데이터베이스
- 스파르타 코딩클럽 #내배캠 #최종프로젝트 #로그인 #인증인가 #jwt? #토큰인증 #액세스토큰 #리프레시토큰 #쿠키 #파싱 #서명키의 중요성 #security context holder
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #최종프로젝트 #aws s3 #프로필 이미지 수정 #자동삭제
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #AI #서버 #자동화 #SQL #KDT #기본문법 #데이터베이스 #Computer #Science #CPU #메모리
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #트랜잭션전파 #transaction
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #대용량트래픽 #처리방법 #캐싱 #코드최적화 #db최적화 #트래픽 #로드밸런서
- spring security #jwt 토큰 #json web token #token #직렬화 #인증
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #OSI #ISO #AI #서버 #자동화 #SQL #기본문법 #데이터베이스 #DBMS #Oracle #MongoDB #아키텍쳐 #DB
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #AI #서버 #자동화 #SQL #기본문법 #데이터베이스
- 스파르타코딩클럽 #부트캠프 #IT #백엔드 #머신러닝 #AI #서버 #자동화 #SQL #기본문법 #데이터베이스 #웹개발
- 프로세스 #CPU #시공유 #커널
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #ci #ioc #의존성
- 비트 #바이트 #이진수
- 챌린저스 #bod
- 스파르타 코딩클럽 #내배캠 #최종프로젝트 #4개월삭제 #국비지원 #자바
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #ci #cd
- 스프링 #백엔드 #자바
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #get방식 #post방식 #http프로토콜 #클라이언트 #백엔드 #api
- 스파르타 코딩클럽 #인메모리db #h2 #연동이슈 #문제해결 #방법 #spring security #header #
- java5기
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #aop #관점지향프로그래밍 #유지보수
- 보안 #이슈
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #소셜로그인 #네이버 #기능구현 #vue.js #spring boot #네이버로그인 #연동하기
- ci/cd파이프라인
- 스파르타 코딩클럽 #내배캠 #모의면접 #예상질문 #http 메서드 종류 #메서드 #post #put #get #patch #springmvc패턴 #model #view #controller
- Today
- Total
요리사에서 IT개발자로
스파르타 부트캠프 Spring Master 3강 Spring Security 로그인 본문
Spring Security 사용 전
Spring Security 사용 후
Client의 요청은 모두 Spring Security를 거친다.
Spring Security의 역할
인증 / 인가
성공시
Controller로 Client 요청 을 전달한다.
Client 요청 + 사용자 정보(UserDetails)
실패시
Controller로 Client 요청을 전달하지않는다
(Error Response)
로그인 처리 과정
Client가 로그인을 시도한다
로그인 시도할 username과 password정보를 HTTP body로 전달한다(POST)
인증 관리자(Authentication Manager)는
UserDetailsService에게 username을 전달하고
회원상세 정보를 요청한다.
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
// CSRF 설정
http.csrf((csrf) -> csrf.disable());
http.authorizeHttpRequests((authorizeHttpRequests) ->
authorizeHttpRequests
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() // resources 접근 허용 설정
.anyRequest().authenticated() // 그 외 모든 요청 인증처리
);
// 로그인 사용
http.formLogin((formLogin) ->
formLogin
// 로그인 처리 (POST /api/user/login)
.loginProcessingUrl("/api/user/login").permitAll()
);
return http.build();
}
UserDetatilsService는
회원 DB에서 회원을 조회한다.
회원 정보가 없으면 Error를 발생한다.
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("Not Found " + username));
조회된 회원 정보(user)를 UserDetails로 변환한다.
UserDetails userDetails = new UserDetailsImpl(user);
UserDetails를
인증관리자 (Authentication Manager) 에게 전달한다
인증관리자가 인증처리를 할때
Client가 로그인 시도한 username, password 일치 여부확인한다.
userDetailsService가 전달해준 UserDetails의 username, password 일치 여부를 확인한다.
password를 비교할 때
Client가 보낸 password는 평문, UserDetails의 password는 암호문이다.
Client가 보낸 password를 암호화해서 비교한다.
인증을 성공하면
세션에 로그인 정보를 저장하고
실패하면 Error를 발생한다.
로그인 구현
로그인 처리URL 설정
로그인 처리 URL을 설정한다.(Spring Security를 사용하면 인가처리가 편해진다)
.requestMatchers("/api/user/**").permitAll() // '/api/user/'로 시작하는 요청 모두 접근 허가
이 요청들은
로그인, 회원가입 관련 요청이기에 비회원, 회원 누구나 접근이 가능하다.
.anyRequest().authenticated() // 그 외 모든 요청 인증처리
인증이 필요한 URL들도 간편하게 처리할 수 있다.
DB회원 정보 조회도
Spring Security 인증 관리자에게 전달한다.
@AuthenticationPrincipal
@Controller
@RequestMapping("/api")
public class ProductController {
@GetMapping("/products")
public String getProducts(@AuthenticationPrincipal UserDetailsImpl userDetails) {
User user = userDetails.getUser();
System.out.println("user.getUsername() = " + user.getUsername());
System.out.println("user.getEmail() = " + user.getEmail());
return "redirect:/";
}
}
Authentication의 Pricipal에 저장된
UserDetailsImpl을 가져올 수 있다.
UserDetailsImpl에 저장된 인증된 사용자인 User객체를 사용할 수 있다.
@AuthenticationPrincipal을 사용하여 메인페이지 사용자 이름 반영
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model, @AuthenticationPrincipal UserDetailsImpl userDetails) {
model.addAttribute("username", userDetails.getUsername());
return "index";
}
}
'Spring' 카테고리의 다른 글
스파르타 부트캠프 Spring Master 3강 Entity @ManyToOne 단방향, 양방향 정리 (0) | 2024.05.28 |
---|---|
스파르타 부트캠프 Spring Master 3강 Entity @OneToOne 단방향, 양방향 정리 (0) | 2024.05.28 |
스파르타 부트캠프 Spring Master 3강 Spring Security 프레임워크란 (0) | 2024.05.26 |
스파르타 부트캠프 Spring Master 3강 필터(Filter) (0) | 2024.05.26 |
스파르타 부트캠프 Spring Master 3강 패스워드 암호화 이해하기 (0) | 2024.05.25 |