[TIL] 33. 로그인Security

김지수·2024년 6월 7일

TIL

목록 보기
33/53
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyUserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/register", "/login").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

@EnableWebSecurity 웹 보안 기능을 활성화
MyUserDetailsService는 사용자 정보를 로드하는 서비스로, UserDetailsService 인터페이스를 구현한 클래스
configure(AuthenticationManagerBuilder auth) 사용자 인증을 설정합니다.
userDetailsService(userDetailsService) 사용자 정보를 로드하는 서비스로 MyUserDetailsService를 사용합니다.
passwordEncoder(passwordEncoder())비밀번호를 암호화하는 방식을 설정합니다. 여기서는 BCryptPasswordEncoder를 사용합니다.

configure(HttpSecurity http) HTTP 요청에 대한 보안 설정을 정의합니다.
authorizeRequests() URL 접근 권한을 설정합니다.
antMatchers("/register", "/login").permitAll() /register와 /login 경로는 모든 사용자가 접근할 수 있도록 허용합니다.
anyRequest().authenticated() 다른 모든 요청은 인증된 사용자만 접근할 수 있도록 설정합니다.
formLogin() 폼 기반 로그인을 설정합니다.
loginPage("/login") 커스텀 로그인 페이지를 설정합니다.
permitAll() 로그인 페이지에 대한 접근을 모든 사용자에게 허용합니다.
logout() 로그아웃 설정을 정의합니다.
permitAll() 로그아웃 요청을 모든 사용자에게 허용합니다.


오늘의 회고

과제를 역할분담에서 로그인과 로그아웃부분을 담당하게 되었다.역할 분담을 통해 내 역할을 좀 더 알아보고 과제에 집중해야겠다.

profile
서툴고 부족한 점이 많지만, 배우고 발전하며 성장하기 위해 노력하겠습니다.

0개의 댓글