현재 버전

Principal 객체

public class UserAccount extends User {

	private static final long serialVersionUID = 1L;
	private final TbUser account;

    public UserAccount(TbUser tbuser) {

        super(tbuser.getId(), tbuser.getPwd(), Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + tbuser.getAdminGpNo())));

        this.account = tbuser;
    }

    public TbUser getAccount() {
        return account;
    }





}



AccountService 로그인 부분



@Service
@Slf4j
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class AccountService implements UserDetailsService {

	// Injection
	private final tbUserRepository tbUserRepository;
	
	/// ...

    @Override
    public UserDetails loadUserByUsername(String id) throws UsernameNotFoundException {
        Optional<TbUser> account = tbUserRepository.findById(id);
        if (account.isPresent()) {
            log.info(account.get().getName() + "님이 로그인을 시도합니다.");
            return new UserAccount(account.get());
        } else {
            ...
        }
    }


@CurrentUser

// Principal 객체에 담겨 있는 UserAccount의 account를 반환합니다.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : account")
public @interface CurrentUser {

}


@CurrentUser 사용

@Controller
public class LoginOutController {

    @Autowired
    private AccountService accountService;

    /*
     * 로그인 페이지
     */
    @GetMapping(value = "/login")
    public String loginForm(@CurrentUser TbUser user) {

    }

}



수정 이력

2021-11-29 16:33:29 에 아래 내용에서 변경 됨 #3

Principal 객체

public class UserAccount extends User {

	private static final long serialVersionUID = 1L;
	private final TbUser account;

    public UserAccount(TbUser tbuser) {

        super(tbuser.getAdminId(), tbuser.getAdminPwd(), Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + tbuser.getAdminGpNo())));

        this.account = tbuser;
    }

    public TbUser getAccount() {
        return account;
    }





}



AccountService 로그인 부분



@Service
@Slf4j
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class AccountService implements UserDetailsService {

	// Injection
	private final tbUserRepository tbUserRepository;
	
	/// ...

    @Override
    public UserDetails loadUserByUsername(String id) throws UsernameNotFoundException {
        Optional<TbUser> account = tbUserRepository.findById(id);
        if (account.isPresent()) {
            log.info(account.get().getName() + "님이 로그인을 시도합니다.");
            return new UserAccount(account.get());
        } else {
            ...
        }
    }


@CurrentUser

// Principal 객체에 담겨 있는 UserAccount의 account를 반환합니다.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : account")
public @interface CurrentUser {

}


@CurrentUser 사용

@Controller
public class LoginOutController {

    @Autowired
    private AccountService accountService;

    /*
     * 로그인 페이지
     */
    @GetMapping(value = "/login")
    public String loginForm(@CurrentUser TbUser user) {

    }

}


2021-11-29 16:32:55 에 아래 내용에서 변경 됨 #2

Principal 객체

public class UserAccount extends User {

	private static final long serialVersionUID = 1L;
	private final TbUser account;

    public UserAccount(TbUser tbuser) {

        super(tbuser.getAdminId(), tbuser.getAdminPwd(), Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + tbuser.getAdminGpNo())));

        this.account = tbuser;
    }

    public TbUser getAccount() {
        return account;
    }





}



AccountService 로그인 부분



@Service
@Slf4j
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class AccountService implements UserDetailsService {

	// Injection
	private final tbUserRepository tbUserRepository;
	
	/// ...

    @Override
    public UserDetails loadUserByUsername(String id) throws UsernameNotFoundException {
        Optional<TbUser> account = tbUserRepository.findById(id);
        if (account.isPresent()) {
            log.info(account.get().getName() + "님이 로그인을 시도합니다.");
            return new UserAccount(account.get());
        } else {
            ...
        }
    }


@CurrentUser

// Principal 객체에 담겨 있는 UserAccount의 account를 반환합니다.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : account")
public @interface CurrentUser {

}


@CurrentUser 사용

@Controller
public class LoginOutController {

    @Autowired
    private AccountService accountService;

    /*
     * 로그인 페이지
     */
    @GetMapping(value = "/login")
    public String loginForm(@CurrentUser @Controller
public class LoginOutController {

    @Autowired
    private AccountService accountService;

    /*
     * 로그인 페이지
     */
    @GetMapping(value = "/login")
    public String loginForm(@CurrentUser TbUser user) {

    }

}
user) {

    }

}


2021-11-29 16:28:44 에 아래 내용에서 변경 됨 #1

Principal 객체

public class UserAccount extends User {

	private static final long serialVersionUID = 1L;
	private final TbUser account;

    public UserAccount(TbUser tbuser) {

        super(tbuser.getAdminId(), tbuser.getAdminPwd(), Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + tbuser.getAdminGpNo())));

        this.account = tbuser;
    }

    public TbUser getAccount() {
        return account;
    }





}



AccountService 로그인 부분



@Service
@Slf4j
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class AccountService implements UserDetailsService {

	// Injection
	private final tbUserRepository tbUserRepository;
	
	/// ...

    @Override
    public UserDetails loadUserByUsername(String id) throws UsernameNotFoundException {
        Optional<TbUser> account = tbUserRepository.findById(id);
        if (account.isPresent()) {
            log.info(account.get().getName() + "님이 로그인을 시도합니다.");
            return new UserAccount(account.get());
        } else {
            ...
        }
    }


@CurrentUser

// Principal 객체에 담겨 있는 UserAccount의 account를 반환합니다.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : account")
public @interface CurrentUser {

}


@CurrentUser 사용

@Controller
public class LoginOutController {

    @Autowired
    private AccountService accountService;

    /*
     * 로그인 페이지
     */
    @GetMapping(value = "/login")
    public String loginForm(@CurrentUser TblofficeAdmin user) {

    }

}