SpringBoot 2.X 정리
  • README
  • 1. SpringBoot 처음 사용할 때 + Lombok 설명
  • 2. Spring Data JPA
  • 3. Spring Data JPA 를 이용한 쿼리 연습
  • 4. JPA 연관관계 처리
  • 5. Thymeleaf 사용법
  • 6. Spring MVC 를 이용한 게시판 구현연습
  • 7. Spring Security 4 JDBC 를 이용한 로그인 인증방법
  • 8. Maven -> Gradle 변경작업
  • 9. React 구성하기
  • 10. SpringSecurity 인증 후 로그인 객체는 어떻게?
  • 11. SpringBoot에서 Amazon SES 서비스 사용하기
  • 12. SpringBoot에서 ElasticBeanStalk ebextensions 파일 만들기
  • 오류 상황과 대처법
    • 1. Querydsl
    • 2. MYSQL 오류 대처법
    • 3.Ajax CSRF 대처법
    • 4. Heroku 배포하기
    • 5. 프로젝트 이름 변경
    • 6. Gradle 오류 대처법
    • 7. React 오류 대처법
    • 8. Tymeleaf LocalDateTime
    • 9. JpaAuditing을 통한 시간 동기화
    • 10. AWS RDS timezone과 charset 변경
Powered by GitBook
On this page
  • 1. Bean을 통해 가져오기
  • 2. Controller 에서 사용자 정보를 얻는다.
  • 3. @Authentication Principal 을 사용한다.

Was this helpful?

10. SpringSecurity 인증 후 로그인 객체는 어떻게?

SpringBoot 에서 Security 인증 후에 우리는 어떻게 로그인 한 객체의 정보를 받아올 수 있을까?

  1. Bean을 통해 사용자 정보를 가져온다.

  2. Controller 에서 사용자 정보를 얻는다.

  3. @Authentication Principal 을 사용한다.

  4. 설명은 하지만 본인이 실제로 해본 작업은 2번이다. 2번에 대해서는 작업한 내용을 보면서 설명한다.

1. Bean을 통해 가져오기

SecurityContextHolder를 통해 가져오는 방법이다.

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
UserDetails userDetails = (UserDetails)principal; 
String username = principal.getUsername(); 
String password = principal.getPassword();

2. Controller 에서 사용자 정보를 얻는다.

Principal 객체에 접근해 정보를 가져온다.

@Controller 
public class SecurityController { 
    @GetMapping("/username") 
    @ResponseBody 
    public String currentUserName(Principal principal) { 
        return principal.getName(); 
    } 
}

본인의 경우 다음처럼 활용을 했었다. Authentication을 통해서 현재 로그인한 사용자의 id를 통해 DB에서 사용자 내역을 받아오게 했다.

public int addActivity(@RequestParam("name") String name,Activity activity,Hash hash,Authentication authentication) {
    //현재 로그인한 유저의 정보를 받아옵니다.
    UserDetails userDetails = (UserDetails) authentication.getPrincipal();
    Member m = memRepo.findOneByMid(userDetails.getUsername());
    List<Activity> activityList = m.getActivities();

JDBC Authorization을 사용했을 때, 아래와 같이 userDetails 정보를 받아왔음.

org.springframework.security.core.userdetails.User@5b2ffc6: 
    Username: djunnni; 
    Password: [PROTECTED]; 
    Enabled: true; 
    AccountNonExpired: true; 
    credentialsNonExpired: true; 
    AccountNonLocked: true; 
    Granted Authorities: ROLE_MEMBER

3. @Authentication Principal 을 사용한다.

Previous9. React 구성하기Next11. SpringBoot에서 Amazon SES 서비스 사용하기

Last updated 5 years ago

Was this helpful?

이에 대해서는 를 참고하길 바란다.

[Spring Security] 현재 로그인한 사용자 정보 가져오기