Skip to content

Commit

Permalink
Merge pull request #148 from KUIT-Space/feat/#147/passwordencode
Browse files Browse the repository at this point in the history
Feat/#147/ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™”
  • Loading branch information
hyunn522 authored Sep 11, 2024
2 parents 1f51533 + 0537c63 commit 6b3bea9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/main/java/space/space_spring/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
Expand All @@ -18,6 +25,14 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.csrf((csrfConfig) ->
csrfConfig.disable()
)
// HTTP ์ธ์ฆ ์š”๊ตฌ ๋น„ํ™œ์„ฑํ™”
// .authorizeHttpRequests(auth -> auth
// .anyRequest().authenticated()
// )
// Session ์‚ฌ์šฉ ์„ค์ • ํ•ด์ œ
.sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
)
// h2 console ํ™”๋ฉด์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•ด๋‹น ์˜ต์…˜๋“ค disable
.headers((headerConfig) ->
headerConfig.frameOptions(frameOptionsConfig ->
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/space/space_spring/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public void saveUser(String email, String password, String userName, UserSignupT
initializeBaseEntityFields();
}

public boolean passwordMatch(String password) {
return this.password.equals(password);
}


}
8 changes: 6 additions & 2 deletions src/main/java/space/space_spring/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import space.space_spring.dao.UserSpaceDao;
Expand Down Expand Up @@ -35,6 +36,7 @@ public class UserService {
private final JwtLoginProvider jwtLoginProvider;
private final UserSpaceDao userSpaceDao;
private final UserUtils userUtils;
private final PasswordEncoder passwordEncoder;

@Transactional
public Long signup(PostUserSignupRequest postUserSignupRequest) {
Expand All @@ -45,7 +47,7 @@ public Long signup(PostUserSignupRequest postUserSignupRequest) {

// TODO 2. ํšŒ์›์ •๋ณด db insert
String email = postUserSignupRequest.getEmail();
String password = postUserSignupRequest.getPassword();
String password = passwordEncoder.encode(postUserSignupRequest.getPassword());
String userName = postUserSignupRequest.getUserName();

User saveUser = userDao.saveUser(email, password, userName, LOCAL);
Expand Down Expand Up @@ -79,9 +81,11 @@ public PostLoginDto login(PostLoginDto.Request request) {
}

private void validatePassword(User userByEmail, String password) {
if (!userByEmail.passwordMatch(password)) {
String encodePassword = userByEmail.getPassword();
if(!passwordEncoder.matches(password,encodePassword)){
throw new CustomException(PASSWORD_NO_MATCH);
}

}

@Transactional
Expand Down

0 comments on commit 6b3bea9

Please sign in to comment.