Skip to content

Commit

Permalink
πŸ”— :: (#497) CurrentUser λ¦¬νŽ™ν† λ§
Browse files Browse the repository at this point in the history
  • Loading branch information
tedsoftj1123 authored Dec 25, 2023
2 parents df008a6 + 863902f commit e489f70
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
*.* @tedsoftj1123 @gilgeunwoo @alsdl0629
*.* @tedsoftj1123 @geunoo @alsdl0629
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import team.retum.jobis.event.exception.model.ExceptionEvent;
import team.retum.jobis.global.error.exception.GlobalErrorCode;
import team.retum.jobis.global.error.response.ErrorResponse;
import team.retum.jobis.global.security.auth.CurrentUserHolder;
import team.retum.jobis.global.util.LogUtil;

import java.io.IOException;
Expand All @@ -39,8 +38,6 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
eventPublisher.publishEvent(ExceptionEvent.builder().request(request).exception(e).build());
writeErrorResponse(response, GlobalErrorCode.INTERNAL_SERVER_ERROR);
}
} finally {
CurrentUserHolder.clear();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import team.retum.jobis.common.spi.SecurityPort;
import team.retum.jobis.domain.auth.model.Authority;
import team.retum.jobis.domain.company.model.Company;
import team.retum.jobis.domain.company.persistence.entity.CompanyEntity;
import team.retum.jobis.domain.company.persistence.mapper.CompanyMapper;
import team.retum.jobis.domain.student.model.Student;
import team.retum.jobis.domain.student.persistence.entity.StudentEntity;
import team.retum.jobis.domain.student.persistence.mapper.StudentMapper;
import team.retum.jobis.domain.teacher.model.Teacher;
import team.retum.jobis.domain.teacher.persistence.entity.TeacherEntity;
import team.retum.jobis.domain.teacher.persistence.mapper.TeacherMapper;
import team.retum.jobis.domain.user.model.User;
import team.retum.jobis.domain.user.persistence.mapper.UserMapper;
import team.retum.jobis.domain.user.persistence.repository.UserJpaRepository;
import team.retum.jobis.global.security.auth.CurrentUserHolder;
import team.retum.jobis.global.exception.InvalidTokenException;
import team.retum.jobis.global.security.auth.company.CompanyDetails;
import team.retum.jobis.global.security.auth.student.StudentDetails;
import team.retum.jobis.global.security.auth.teacher.TeacherDetails;

@RequiredArgsConstructor
@Component
Expand All @@ -43,7 +44,8 @@ public String encodePassword(String password) {

@Override
public Authority getCurrentUserAuthority() {
return CurrentUserHolder.getCurrentUserAuthority();
UserDetails currentUser = (UserDetails) getCurrentUserDetails();
return Authority.valueOf(currentUser.getAuthorities().iterator().next().getAuthority());
}

@Override
Expand All @@ -53,23 +55,31 @@ public boolean isPasswordMatch(String rawPassword, String encodedPassword) {

@Override
public Company getCurrentCompany() {
return companyMapper.toDomain((CompanyEntity) CurrentUserHolder.getUser());
CompanyDetails companyDetails = (CompanyDetails) getCurrentUserDetails();
return companyMapper.toDomain(companyDetails.getCompany());
}

@Override
public Student getCurrentStudent() {
return studentMapper.toDomain((StudentEntity) CurrentUserHolder.getUser());
StudentDetails studentDetails = (StudentDetails) getCurrentUserDetails();
return studentMapper.toDomain(studentDetails.getStudent());
}

@Override
public Teacher getCurrentTeacher() {
return teacherMapper.toDomain((TeacherEntity) CurrentUserHolder.getUser());
TeacherDetails teacherDetails = (TeacherDetails) getCurrentUserDetails();
return teacherMapper.toDomain(teacherDetails.getTeacher());
}

@Override
public User getCurrentUser() {
Long currentUserId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getName());
return userJpaRepository.findById(currentUserId)
.map(userMapper::toDomain).get();
.map(userMapper::toDomain)
.orElseThrow(() -> InvalidTokenException.EXCEPTION);
}

private Object getCurrentUserDetails() {
return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package team.retum.jobis.global.security.auth.company;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import team.retum.jobis.domain.auth.model.Authority;
import team.retum.jobis.domain.company.persistence.entity.CompanyEntity;

import java.util.Collection;
import java.util.Collections;

@Getter
@RequiredArgsConstructor
public class CompanyDetails implements UserDetails {
private final Long companyId;

private final transient CompanyEntity company;

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Expand All @@ -25,7 +29,7 @@ public String getPassword() {

@Override
public String getUsername() {
return companyId.toString();
return company.getId().toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import team.retum.jobis.domain.company.persistence.entity.CompanyEntity;
import team.retum.jobis.domain.company.persistence.repository.CompanyJpaRepository;
import team.retum.jobis.global.exception.InvalidTokenException;
import team.retum.jobis.global.security.auth.CurrentUserHolder;

@Component
@RequiredArgsConstructor
Expand All @@ -21,8 +20,7 @@ public UserDetails loadUserByUsername(String companyId) throws UsernameNotFoundE
CompanyEntity company = companyJpaRepository.findById(
Long.valueOf(companyId)
).orElseThrow(() -> InvalidTokenException.EXCEPTION);
CurrentUserHolder.setUser(company);

return new CompanyDetails(company.getId());
return new CompanyDetails(company);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package team.retum.jobis.global.security.auth.student;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import team.retum.jobis.domain.auth.model.Authority;
import team.retum.jobis.domain.student.persistence.entity.StudentEntity;

import java.util.Collection;
import java.util.Collections;

@Getter
@RequiredArgsConstructor
public class StudentDetails implements UserDetails {
private final Long studentId;

private final transient StudentEntity student;

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Expand All @@ -25,7 +29,7 @@ public String getPassword() {

@Override
public String getUsername() {
return studentId.toString();
return student.getId().toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import team.retum.jobis.domain.student.persistence.entity.StudentEntity;
import team.retum.jobis.domain.student.persistence.repository.StudentJpaRepository;
import team.retum.jobis.global.exception.InvalidTokenException;
import team.retum.jobis.global.security.auth.CurrentUserHolder;

@Component
@RequiredArgsConstructor
Expand All @@ -21,8 +20,7 @@ public UserDetails loadUserByUsername(String studentId) throws UsernameNotFoundE
StudentEntity studentEntity = studentJpaRepository.findById(
Long.valueOf(studentId)
).orElseThrow(() -> InvalidTokenException.EXCEPTION);
CurrentUserHolder.setUser(studentEntity);

return new StudentDetails(studentEntity.getId());
return new StudentDetails(studentEntity);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package team.retum.jobis.global.security.auth.teacher;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import team.retum.jobis.domain.auth.model.Authority;
import team.retum.jobis.domain.teacher.persistence.entity.TeacherEntity;

import java.util.Collection;
import java.util.Collections;

@Getter
@RequiredArgsConstructor
public class TeacherDetails implements UserDetails {
private final Long teacherId;

private final transient TeacherEntity teacher;

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Expand All @@ -25,7 +29,7 @@ public String getPassword() {

@Override
public String getUsername() {
return teacherId.toString();
return teacher.getId().toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import team.retum.jobis.domain.teacher.persistence.entity.TeacherEntity;
import team.retum.jobis.domain.teacher.persistence.repository.TeacherJpaRepository;
import team.retum.jobis.global.exception.InvalidTokenException;
import team.retum.jobis.global.security.auth.CurrentUserHolder;

@Component
@RequiredArgsConstructor
Expand All @@ -21,8 +20,7 @@ public UserDetails loadUserByUsername(String teacherId) throws UsernameNotFoundE
TeacherEntity teacherEntity = teacherJpaRepository.findById(
Long.valueOf(teacherId)
).orElseThrow(() -> InvalidTokenException.EXCEPTION);
CurrentUserHolder.setUser(teacherEntity);

return new TeacherDetails(teacherEntity.getId());
return new TeacherDetails(teacherEntity);
}
}

0 comments on commit e489f70

Please sign in to comment.