보시면 그냥 페이지 접속시 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을 이용해서 웹페이지마다 관리자가 이용하는 페이지 고객이 이용하는 페이지로 만들겁니다.
위의 그림과 같이 개발을 했었는데 spring에서 버전업을 하게 되면 더이상 사용하지 않게 된다고 하네요 현재 공부 하는 버전은 5.3버전입니다 . 그래서 몇가지를 찾아봣는데요. 그중에서 SecurityFilterChain를 이용하는 방식이 가장 좋은 방법이라 생각했습니다.
일단 이게 일반적인 예시인듯 하고요 로그인할떄 jwt? session 방법이 있다던데 저는 일단 session방법만 알아서 session은 확실히 안전을 보장한다는건 알아서요. jwt는 보안 안정성에 대한 이슈가 있는지는 정확히 저도 찾아봐야 알겠네요. db에서 사용자 정보를 확인후 session에 로그인이 되도록 할겁니다. 오늘은 여기까지하고 내일 로그인 백엔드 쪽 구현하도록 하겟습니다