Spring/Security4 Security 로그인 검증 로직 및 csrf 설정 @Service public class CustomUserDetailsService implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { UserEntity userEntity = userRepository.findByUsername(username); if (userEntity != null) { //널이아니면 user가 있는것 return new CustomUserDetails(userEntity); } return null; } }.. 2024. 2. 21. Security 로그인 검증 로직 @Service public class CustomUserDetailsService implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { UserEntity userEntity = userRepository.findByUsername(username); if (userEntity != null) { //널이아니면 user가 있는것 return new CustomUserDetails(userEntity); } return null; } }.. 2024. 2. 21. Security 커스텀 로그인 설정 LoginController /login경로 접근시 login폼 이동하게 작성 SecurityConfig에 다음과같이 코드를 추가한다. http .formLogin((auth) -> auth.loginPage("/login") .loginProcessingUrl("/loginProc") .permitAll() ); http .csrf((auth) -> auth.disable()); 인가되지 않은 경로로 접근시 로그인하지않은 경우 "/login" 경로로 이동, 로그인진행은 /loginProc경로로 요청하며 로그인 이후 모든경로를 모두허용(hasRole권한이 없는 경로는 제외)한다. csrf는 enable시 post요청시에 header에 토큰값을 보내주어야 하므로 임시적으로 disable 한다. // 패스워.. 2024. 2. 21. Security Config 설정 @Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { //메소드명은 자유 http .authorizeHttpRequests((auth) -> auth .requestMatchers("/", "/login").permitAll() .requestMatchers("/admin").hasRole("ADMIN") .requestMatchers("/my/**").hasAnyRole("ADMIN", "USER") .anyRequest().authenticated() ); return http.build(); .. 2024. 2. 21. 이전 1 다음