Skip to content

Commit

Permalink
#82 fix : createDelegatingPasswordEncoder() 메서드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
rivkode committed Apr 25, 2024
1 parent 922fe4b commit 7c476b0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.seoultech.synergybe.domain.common;

public interface CustomPasswordEncoder{
public interface CustomPasswordEncoder {
String encodePassword(String rawPassword);

boolean matchesPassword(String rawPassword, String encodedPassword);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,10 @@ public class SecurityConfig {
private final AuthenticationConfiguration authenticationConfiguration;

@Bean
public static CustomPasswordEncoder customPasswordEncoder() {
return new BCryptCustomPasswordEncoder(new BCryptPasswordEncoder());
public CustomPasswordEncoder customPasswordEncoder() {
return new BCryptCustomPasswordEncoder();
}

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

@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception {
return configuration.getAuthenticationManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.seoultech.synergybe.domain.common.CustomPasswordEncoder;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

@RequiredArgsConstructor
public class BCryptCustomPasswordEncoder implements CustomPasswordEncoder {

private final BCryptPasswordEncoder passwordEncoder;
private final PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();

@Override
public String encodePassword(String rawPassword) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Slf4j(topic = "로그인, JWT 생성")
Expand All @@ -29,19 +33,28 @@ public Authentication attemptAuthentication(HttpServletRequest request,
try {
LoginRequest loginRequest = new ObjectMapper().readValue(request.getInputStream(),
LoginRequest.class);
List<GrantedAuthority> authorities = getAuthorities(List.of("ROLE_USER"));

return getAuthenticationManager().authenticate(
new UsernamePasswordAuthenticationToken(
loginRequest.getEmail(),
loginRequest.getPassword(),
null
authorities
// null
)
);
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}

private List<GrantedAuthority> getAuthorities(List<String> authorities) {
return authorities.stream()
.map(SimpleGrantedAuthority::new)
.map(GrantedAuthority.class::cast)
.toList();
}

@Override
public void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
FilterChain chain, Authentication authentication) throws IOException {
Expand Down

0 comments on commit 7c476b0

Please sign in to comment.