Control usernameParameter for UserDetailsService
This commit is contained in:
@@ -4,6 +4,7 @@ import demo.api.jwt.dtos.TokenDto;
|
||||
import demo.api.user.domain.User;
|
||||
import demo.api.user.dtos.UserSignInRequest;
|
||||
import demo.api.user.dtos.UserSignUpRequest;
|
||||
import java.util.Objects;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -29,7 +30,7 @@ public class AuthController {
|
||||
public String signUp(@Validated UserSignUpRequest signUpReq) throws Exception {
|
||||
User user = authService.signUp(signUpReq);
|
||||
|
||||
if(user.getEmail() != "") {
|
||||
if(!Objects.isNull(user)) {
|
||||
return "redirect:/user/signIn";
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ public class SecurityConfig {
|
||||
.csrf().disable()
|
||||
.formLogin()
|
||||
.loginPage("/auth/signIn")
|
||||
.usernameParameter("email")
|
||||
.defaultSuccessUrl("/")
|
||||
.failureUrl("/auth/signIn?fail=true");
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String email) throws UserNotFoundException {
|
||||
|
||||
System.out.println("email in loadUserByUsername = " + email);
|
||||
User user = userRepository.findByEmail(email)
|
||||
.orElseThrow(UserNotFoundException::new);
|
||||
Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
|
||||
|
||||
@@ -31,7 +31,6 @@ public class JwtTokenFilter extends OncePerRequestFilter {
|
||||
SecurityContextHolder.getContext().setAuthentication(auth); // 정상 토큰이면 SecurityContext에 저장
|
||||
}
|
||||
} catch (CustomException ex) {
|
||||
//this is very important, since it guarantees the user is not authenticated at all
|
||||
SecurityContextHolder.clearContext();
|
||||
response.sendError(ex.getHttpStatus().value(), ex.getMessage());
|
||||
return;
|
||||
|
||||
@@ -26,6 +26,7 @@ public class UserController {
|
||||
|
||||
@GetMapping("/profile")
|
||||
public String profile(Model model, @AuthenticationPrincipal UserDetails userDetails) {
|
||||
System.out.println("userDetails = " + userDetails);
|
||||
if (userDetails != null) {
|
||||
User userDetail = userService.findByEmail(userDetails.getUsername())
|
||||
.orElseThrow(() -> new UserNotFoundException());
|
||||
|
||||
Reference in New Issue
Block a user