728x90
61강 회원수정 2
Auttenticaion객체를 세션에 저장하기 위한 흐름
UserApiController.java
package com.cos.blog.controller.api;
import com.cos.blog.config.auth.PrincipalDetail;
import com.cos.blog.dto.ResponseDto;
import com.cos.blog.model.RoleType;
import com.cos.blog.model.User;
import com.cos.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController
public class UserApiController {
@Autowired
private UserService userService;
@Autowired
private AuthenticationManager authenticationManager;
@PostMapping("/auth/joinProc")
public ResponseDto<Integer> save(@RequestBody User user){ // username, password, email
System.out.println("UserApiController : save 호출됨");
// 실제로 DB에 insert를 하고 아래에서 return이 되면 되요.
userService.회원가입(user);
return new ResponseDto<Integer>(HttpStatus.OK.value(),1); // 자바오브젝트를 JSON으로 변환해서 리턴 (Jackson)
}
// 전통적인 방법
// @PostMapping("/api/user/login")
// public ResponseDto<Integer> login(@RequestBody User user, HttpSession session) {
// System.out.println("UserApiController : login 호출됨");
// User principal = userService.로그인(user); // principal (접근주체)
//
// if(principal != null) {
// session.setAttribute("principal",principal);
// }
// return new ResponseDto<Integer>(HttpStatus.OK.value(),1); // 자바오브젝트를 JSON으로 변환해서 리턴 (Jackson)
// }
@PutMapping("/user")
public ResponseDto<Integer> update(@RequestBody User user){ // Json 데이터 받고 싶으면 @RequestBody적음
userService.회원수정(user);
// 여기서는 트랜잭션이 종료되기 때문에 DB에 값은 변경이 됐음.
// 하지만 세션값은 변경되지 않은 상태이기 때문에 우리가 직접 세션값을 변경해줄 것임.
// 세션 등록
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authentication);
return new ResponseDto<Integer>(HttpStatus.OK.value(),1);
}
}
user.js 수정 및 추가
update:function(){
//alert('user의 save함수 호출됨');
let data = {
id: $("#id").val(),
username: $("#username").val(),
password: $("#password").val(),
email: $("#email").val(),
};
62강 카카오 로그인 환경설정
카카오 api 웹서버주소 : http://localhost:8080 클라이언트 키 : cad86e1e500fe39e339d28a970e951bc
로그인요청 콜백 주소 : http://localhost:8080/auth/kakao/callback
User 오브젝트 : id(번호), username, password, email 카카오로부터 받을 정보 : profile정보(필수), email(선택)
63강 카카오 로그인 OAuth2.0 개념이해
64강 카카오 로그인 엑세스토큰 받기
'스프링부트 with JPA' 카테고리의 다른 글
스프링부트 with JPA 블로그 3(41~60강) (0) | 2024.02.22 |
---|---|
스프링부트 with JPA 블로그 2(21~40강) (0) | 2024.02.22 |
스프링부트 with JPA 블로그 1(1~20강) (0) | 2024.02.22 |