1. 스프링 커스텀마이징 하는 법을 순서대로 정리해 보세요.
package edu.sejong.ex.vo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Setter
public class UserDetailsVO implements UserDetails {
private String username;
private String password;
private GrantedAuthority authority;
public UserDetailsVO(UserAuthorityVO user) {
this.setUsername(user.getUsername());
this.setPassword(user.getPassword());
this.setAuthority(user);
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
list.add(new SimpleGrantedAuthority(authority.getAuthority()));
return list;
}
@Override
public String getPassword() {
return this.password;
}
@Override
public String getUsername() {
return this.username;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
package edu.sejong.ex.security;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import edu.sejong.ex.mapper.UserMapper;
import edu.sejong.ex.vo.UserAuthorityVO;
import edu.sejong.ex.vo.UserDetailsVO;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class CustomUserDetailsService implements UserDetailsService {
@Autowired
private UserMapper userMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
log.warn("Load User By UserAuthorityVO member: " + username);
List<UserAuthorityVO> users = userMapper.getUser(username);
return users == null ? null : new UserDetailsVO(users.get(0));
}
}
2. admin3를 ROLE_ADMIN 권한을 가진 패스워드를 암호화 시켜 저장후 admin/adminHome 에 접근하는지 테스트 하시오.
@Test
void testInsertUser() {
UserVO user = new UserVO();
user.setUsername("admin3");
user.setPassword(new BCryptPasswordEncoder().encode("admin3"));
user.setEnabled(1);
userMapper.insertUser(user);
userMapper.insertAuthoritiesAdmin(user);
}
<insert id="insertAuthoritiesAdmin">
<![CDATA[
insert into authorities values (
#{username},
'ROLE_ADMIN'
)
]]>
</insert>