@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Embeddable
public class Password {
private static final String PASSWORD_PATTERN = "^(?=.*[a-zA-Z])(?=.*[!@#$%^&*+=-])(?=.*[0-9]).{8,12}$";
private static final Pattern PASSWORD_MATCHER = Pattern.compile(PASSWORD_PATTERN);
private static final int REPEATING_LIMIT = 3;
private static final int CONSECUTIVE_LIMIT = 3;
@Column(name = "password", nullable = false, length = 200)
private String value;
private Password(String value) {
this.value = value;
}
public static Password encrypt(String value, PasswordEncoder encoder) {
validatePassword(value);
return new Password(encoder.encode(value));
}
private static void validatePassword(String value) {
if (isNotValidPattern(value)) {
throw BaseException.type(UserErrorCode.INVALID_PASSWORD_PATTERN);
} }
private static boolean isNotValidPattern(String password) {
return !PASSWORD_MATCHER.matcher(password).matches();
}
public boolean isSamePassword(String comparePassword, PasswordEncoder encoder) {
return encoder.matches(comparePassword, this.value);
}
}
public class PasswordEncoderUtils {
public static final PasswordEncoder ENCODER = PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
사용 예시
private void validatePassword(String comparePassword, Password saved) {
if(!saved.isSamePassword(comparePassword, ENCODER)) {
throw BaseException.type(AuthErrorCode.WRONG_PASSWORD);
}
}
security config
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
prePostEnabled = true,
securedEnabled = true,
jsr250Enabled = true
)
@RequiredArgsConstructor
public class SecurityConfig {
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
private final JwtAccessDeniedHandler jwtAccessDeniedHandler;
private final JwtRequestFilter jwtRequestFilter;
@Bean
public WebSecurityCustomizer configure() {
return (web) -> web.ignoring().antMatchers("/css/**", "/script/**", "image/**", "/fonts/**", "lib/**");
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.cors();
http.csrf().disable();
http.formLogin().disable();
http.httpBasic().disable();
http.exceptionHandling()
.authenticationEntryPoint(jwtAuthenticationEntryPoint)
.accessDeniedHandler(jwtAccessDeniedHandler);
http.headers().frameOptions().sameOrigin();
http.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers("/signup").permitAll()
.anyRequest().authenticated();
return http.build();
}
}