보시면 그냥 페이지 접속시 403error가 발생하고 아이디와 비밀번호 db와 매칭이 되면
session에 정보가 저장되어서 로그인이 되어 main페이지를 열어줍니다.
UsersDetailService 코드
@Service
public class UsersDetailService implements UserDetailsService {
@Autowired
private UsersDao usersDao;
public UsersDetailService(){
}
public UsersDetailService(UsersDao usersDao){
this.usersDao=usersDao;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//유저 데이터 가져오기
Users user = usersDao.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found with username: " + username);
} //user.getLevel()
// 사용자 정보를 UserDetails 객체로 변환하여 반환user.getRoles()
String role = "ROLE_ADMIN";
if(user.getLevel()==0){
role = "ROLE_ADMIN";
}else if(user.getLevel()==1){
role = "ROLE_CUSTOM";
}else{
role = "";
}
System.out.println(role);
List<String> roles=new LinkedList<>();
roles.add(role);
return new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
getAuthorities(roles)
);
}
private List<GrantedAuthority> getAuthorities(List<String> roles) {
// 사용자의 권한(role) 정보를 GrantedAuthority 객체로 변환합니다.
List<GrantedAuthority> authorities = new ArrayList<>();
for (String role : roles) {
authorities.add(new SimpleGrantedAuthority(role));
}
return authorities;
}
}
음 지금 AuthenticationManager클래스까지는 지정했고요. 내일이나 모레쯤 마무리 지을수 있을것 같습니다. 이전에는 xml로 처리했었는데요 요번에는 spring framework내에서 하려고하니 좀 많이 빡세네요 계획은 어드민, 고객으로 나눌꺼고요 session 로그인시 role_admin,role_custom으로 나눌예정입니다. 지금 계속 403 error가 나서 영상은 좀 나중에 올릴게요 권한쪽 부여가 계속 잘 안되서 문제가 되고잇습니다.
빈등록 authenticationManager
@Configuration
@EnableWebSecurity
public class SecurityConfig {
....
@Bean
public AuthenticationManager authenticationManager(AuthenticationProvider authenticationProvider) {
return new CustomAuthenticationManager(authenticationProvider);
}
....
}
기존에 webSecurityConfigurerAdapter가 굉장히 많은 역할을 해줬다는게 좀 느껴지네요.
Field authenticationManager in kr.co.kshproject.webDemo.interfaces.loginController required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.
에러가나는데 지금 아래와 같이 변경해서 작업중인데 에러가 나는군요 ㅠ webSecurityConfigurerAdapter가 그립네요 ㅎ ㅠ 퇴근하고 공부하는거라 많이 시간이 안나오네요 이번주 주말까지해서 회원가입시 SHA 256이용해서 비밀번호 변경하고 session을 이용해서 웹페이지마다 관리자가 이용하는 페이지 고객이 이용하는 페이지로 만들겁니다.