Spring Security: Authentication
·
Programming/Spring
Authentication - 세션 영역 안에는 시큐리티 세션 영역이 존재핸다. - 시큐리티 세션에 들어 갈 수 있는 객체 타입은 Authentication이다. - 시큐리티 세션에 Authentication 객체가 들어갔다? = 로그인이 됐다! - Authentication 객체 안에는 2가지 타입만 들어갈 수 있다(UserDetails, OAuth2User) - Authentication가 필요할 때마다 컨트롤에서 DI 할 수 있다. AuthenticationPrincipal이 될 수 있는 2가지 타입 1. UserDetails: 일반 로그인 2. OAuth2User: OAuth 로그인 단점: 컨트롤에서 AuthenticationPrincipal 꺼낼 때 타입을 구분해줘야 한다. 해결: UserDet..
The Custom Authentication Success Handle
·
Programming/Spring
Typically these implementations will determine the URL after login and perform a redirect to that URL. This implementation is going to determine the URL to redirect the user to after login based on the role of the user. First of all, we need to override the onAuthenticationSuccess method: public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler { protected Log..
OAuth2(3) OAuth2 Client 라이브러리 사용 카카오 로그인
·
Programming/Spring
application-oauth.yml spring: security: oauth2: client: registration: kakao: client-id: "Kakao Developers에서 받은 client-id " client-secret: "Kakao Developers에서 받은 client-secret" redirect-uri: "Kakao Developers에서 지정한 redirect-uri " authorization-grant-type: authorization_code client-authentication-method: POST client-name: Kakao CustomOAuth2UserService DefaultOAuth2UserService을 상속 받은 커스텀 클래스. loadU..
OAuth2(2) OAuth2 Client 라이브러리 없이 카카오 로그인
·
Programming/Spring
카카오 로그인 동작 과정 1. 인증 코드 요청 카카오 인증 서버는 해당 사용자에 대한 인가 코드를 발급해 서비스의 redirect_uri에 전달한다. URL GET /oauth/authorize?client_id={REST_API_KEY}&redirect_uri={REDIRECT_URI}&response_type=code HTTP/1.1 Host: kauth.kakao.com 코드 @GetMapping("/login/oauth2/code/kakao") public String getCode(String code) { return code; } 2. 인증 토큰 요청 인증 코드를 요청에 실어 보낸다. 코드 @GetMapping("/login/oauth2/code/kakao") public ResponseEn..
OAuth2(1) OAuth2 구성 및 동작
·
Programming/Spring
OAuth 구성 - 리소스 오너(= 홍길동) - 서버(= 클라이언트): OAuth 서버 입장에서 사이트 서버는 클라이언트. - OAuth 서버(인증 서버): 유저 검증이 끝나면 리다이렉트 주소를 통해 서버에게 코드 전달. - 리소스 서버(자원): 프로필 정보를 가지고 있음. OAuth 동작 - 인증 코드를 받았다= 인증 처리가 완료 됨. - 인증 토큰을 받았다= 권한을 부여 받았다. 리소스 서버에 프로필 정보를 가져올 수 있는 있음.
CORS
·
Programming/Spring
CORS란? Cross Origin Resoure Sharing 브라우저에서 다른 출처 리소스를 공유하는 방법 CORS는 브라우저 구현 스펙에 포함된 정책이다. Cross Origin= 교차 출처= 다른 출처 Origin= Protocol+ Host+ Port 다른 출처 리소스 제한 2가지 정책 SOP(Same Origin Policy) CORS 출처 확인 과정 SOP 지켰어? 아니오 CORS 메커니즘 지켰어? 아니오 브라우저 접근 X 최종 출처 비교는 어디서? 서버 X 브라우저 O 브라우저 → 서버 요청 헤더에 Origin: 출처 추가 서버→ 브라우저 응답 헤더에 Access-Control-Allow-Origin: 허용 출처 추가 브라우저는 Origin과 Access-Control-Allow-Origi..
@DateTimeFormat(pattern = "yyyy-MM-dd")
·
Programming/Spring
@DateTimeFormat이 없으면 에러가 발생한다. it seems to be a conversion problem between String to java.util.Date. Post의 LocalDate이 String으로 들어오는 localDate을 LocalDate으로 변환하지 못하면서 에러가 나는 것이다. @DataTimeFormat을 사용하면 해결 가능하다. @DateTimeFormat은 Spring에서 지원하는 어노테이션으로 LocalDate와 LocalDateTime와 같은 날짜 관련 타입의 직렬화를 지원하는 어노테이션이다.
어노테이션
·
Programming/Spring
스프링이 제공하는 대부분의 어노테이션 특징은 런타임 시, 컴파일러에게 특정 기능을 실행하도록 정보 제공을 한다. 어노테이션 파일 정의 1. 적용 대상 2. 정보유지 되는 대상 3. 어노테이션 이름 스프링에서 초기화 설정 1. xml 이용해서 외부파일에 설정 정보 넣는다. 2. 코드 파일에 설정 정보를 넣는다. 설정 정보= 어노테이션
[Lombok] @RequiredArgsConstructor 의 득과 실?
·
Programming/Spring
백기선 님이 YouTube에 올린 영상 중에 @RequiredArgsConstructor 관한 것이 있다. https://youtu.be/qmI7uJapocw @RequiredArgsConstructor// final로 선언된 멤버 변수를 자동으로 생성합니다. @Service// 서비스임을 선언합니다. public class ProductService { private final ProductRepository productRepository; @Transactional // 메소드 동작이 SQL 쿼리문임을 선언합니다. public Long update(Long id, ProductMypriceRequestDto requestDto){ Product product = productRepository.fi..
JPA Auditing으로 생성일/수정일 자동화하기
·
Programming/Spring
보통 엔티티는 해당 데이터의 생성시간과 수정시간을 포함한다. 그래서 매번 DB에 insert하기 전, update하기 전에 날짜 데이터을 등록/수정하는 코드가 여기저기 들어간다. // 생성일 추가 코드 예제 public void savePosts() { ... posts.setCreateDate(new LocalDate()); postsRepository.save(posts); ... } 이런 단순하고 반복적인 코드가 모든 테이블과 서비스 메서드에 포함되어야 한다고 생각하면 매우 귀찮고 코드가 지저분해진다. 이 문제를 해결할 수 있는 것이 JPA Auditing이다. 적용하기 @MappedSuperclass @EntityListeners(AuditingEntityListener.class) public ..