63. 회원수정2
by 볼빵빵오춘기UserApiController
내용 추가 하기전에 회원정보를 수정하더라도 세션이 변경이 되지않아 로그아웃을 하고 다시 로그인해야지만 변경된 정보를 확인할 수 있었다.
⇒ 강제로 session에 값 저장하려 했으나 안됐다.
⇒ SecurityConfig에가서 authenticationManagerBean()을 bean등록과 override 해준 후 사용한다.
더보기

참고

@Autowired private AuthenticationManager authenticationManager;
@PutMapping("/user") public ResponseDto<Integer> update(@RequestBody User user) { userService.회원수정(user); // //여기서는 트랜잭션이 종료되기 때문에 DB에 값은 변경이 됐음. // // 하지만 세션값은 변경되지 않은 상태이기 때문에 우리가 직접 세션값을 변경해줄 것임. // // 강제로 session에 값 저장하려 했으나 안됐음 /* * Authentication authentication= new * UsernamePasswordAuthenticationToken(principal, * null,principal.getAuthorities()); SecurityContext securityContext = * SecurityContextHolder.getContext(); * securityContext.setAuthentication(authentication); * session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext); */ System.out.println("update 호출"); return new ResponseDto<Integer>(HttpStatus.OK.value(), 1);//자바오브젝트를 JSON으로 변환해서 리턴 }
SecurityConfig
- Override/Implement Methods.. (alt+shift+enter[window기준])
- authenticationManagerBean()을 오버라이드 하지만 스프링 시큐리티 5.7.1 버전부터는
- authenticationManager() 를 오버라이드한다.
@Bean public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception { return authenticationConfiguration.getAuthenticationManager(); }
UserApiController
내용 추가 하기전에 회원정보를 수정하더라도 세션이 변경이 되지않아 로그아웃을 하고 다시 로그인해야지만 변경된 정보를 확인할 수 있었다.
@Autowired private AuthenticationManager authenticationManager;
@PutMapping("/user") public ResponseDto<Integer> update(@RequestBody User user) { userService.회원수정(user); // //여기서는 트랜잭션이 종료되기 때문에 DB에 값은 변경이 됐음. // // 하지만 세션값은 변경되지 않은 상태이기 때문에 우리가 직접 세션값을 변경해줄 것임. // // 세션 등록 Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword())); SecurityContextHolder.getContext().setAuthentication(authentication); //강제로 session에 값 저장하려 했으나 안됐음 /* * Authentication authentication= new * UsernamePasswordAuthenticationToken(principal, * null,principal.getAuthorities()); SecurityContext securityContext = * SecurityContextHolder.getContext(); * securityContext.setAuthentication(authentication); * session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext); */ System.out.println("update 호출"); return new ResponseDto<Integer>(HttpStatus.OK.value(), 1);//자바오브젝트를 JSON으로 변환해서 리턴 }

블로그의 정보
Hello 춘기's world
볼빵빵오춘기