π 2024λ 01μ 30μΌ
- SecurityConfig
/* websecurityconfigureradapterμ μμνλ λ°©μμ μ§μμ’ λ£λμλ€. */ @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) // pre : μ¬μ , post : μ¬ν, μ¬μ .μ¬νμ μΈμ¦/κΆν κ²μ¬ μ΄λ Έν μ΄μ μ¬μ© κ°λ₯ public class SecurityConfig { @Bean public SecurityFilterChain myFilter(HttpSecurity httpSecurity) throws Exception { return httpSecurity .csrf().disable() // csrf 보μ 곡격μ λν μ€μ μ νμ§ μκ² λ€. .authorizeRequests() // νΉμ url λν΄μλ 보μμ μΈμ¦μ²λ¦¬νμ§ μκ³ , νΉμ url λν΄μλ μΈμ¦μ²λ¦¬ νκ² λ€λ μ€μ .antMatchers( "/", "/author/create", "/author/login-page" ) // μΈμ¦νμμλ νμ΄μ§ .permitAll() .anyRequest().authenticated() // κ·Έ μΈλ λͺ¨λ μΈμ¦ νμ .and() // λ§μ½μ μΈμ μ μ¬μ©νμ§ μμΌλ©΄ μλ΄ λ΄μ©μ μ€μ // .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .formLogin() .loginPage("/author/login-page") // λ‘κ·ΈμΈ νμ΄μ§ μ€μ // μ€νλ§ λ΄μ₯ λ©μλλ₯Ό μ¬μ©νκΈ° μν΄ /doLogin url μ¬μ© .loginProcessingUrl("/doLogin") .usernameParameter("email") .passwordParameter("pw") .successHandler(new LoginSuccessHandler()) .and() .logout() // μ€νλ§ λ΄μ₯ λ©μλλ₯Ό μ¬μ©νκΈ° μν΄ /doLogout url μ¬μ© .logoutUrl("/doLogout") .and() .build(); } }
- LoginService
@Slf4j @Service public class LoginService implements UserDetailsService { private final AuthorService authorService; @Autowired public LoginService(AuthorService authorService) { this.authorService = authorService; } @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Author author = authorService.findByEmail(username); List<GrantedAuthority> authorities = new ArrayList<>(); // ROLE_ κΆν ν¨ν΄μΌλ‘ μ€νλ§μμ κΈ°λ³Έμ μΌλ‘ κΆν μ²΄ν¬ authorities.add(new SimpleGrantedAuthority( "ROLE_" + author.getRole().toString() )); /* 맀κ°λ³μ : userEmail, userPassword, authorities ν΄λΉ λ©μλμμ return λλ User κ°μ²΄λ session λ©λͺ¨λ¦¬ μ μ₯μμ μ μ₯λμ΄ κ³μ μ¬μ© κ°λ₯ */ return new User(author.getEmail(), author.getPassword(), authorities); } }
- LoginSuccessHandler
public class LoginSuccessHandler implements AuthenticationSuccessHandler { @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { HttpSession httpSession = request.getSession(); httpSession.setAttribute("email", authentication.getName()); response.sendRedirect("/"); } }
- RedisConfig
@Configuration public class RedisConfig { // Value : ymlμ μλ μ 보λ₯Ό νλ‘κ·Έλ¨μμΌλ‘ κ°μ Έμ€λ μ΄λ Έν μ΄μ @Value("${spring.redis.host}") public String host; @Value("${spring.redis.port}") private int port; @Bean public RedisConnectionFactory redisConnectionFactory(){ RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); redisStandaloneConfiguration.setHostName(host); redisStandaloneConfiguration.setPort(port); return new LettuceConnectionFactory(redisStandaloneConfiguration); } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; } }
λμ€μ λ€μ μ 리ν κ²!