로그인 올바르게 해도 login?error로 갑니다
1406
5 asked
SecurityConfig.java
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final SecurityDetailsService securityDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// .csrf().disable()
.authorizeRequests()
.antMatchers("/user/**").authenticated()
.antMatchers("/admin/**").access("hasRole('ADMIN')")
.anyRequest().permitAll()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/home")
.usernameParameter("userEmail")
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login")
;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
SecurityDetails.java
@RequiredArgsConstructor
public class SecurityDetails implements UserDetails {
private final UserEntity userEntity;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new GrantedAuthority() {
@Override
public String getAuthority() {
return userEntity.getAuthority().toString();
}
});
return authorities;
}
@Override
public String getPassword() {
return userEntity.getUserPassword();
}
@Override
public String getUsername() {
return userEntity.getUserEmail();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
SecurityDetailsService.java
@Service
@RequiredArgsConstructor
public class SecurityDetailsService implements UserDetailsService {
private final UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String userEmail) throws UsernameNotFoundException {
UserEntity userEntity = userRepository.findByUserEmail(userEmail);
if(userEntity != null) return new SecurityDetails(userEntity);
return null;
}
}
SecurityDetails에서 return값이 boolean인 override 받는 메소드들 다 true로 해줬고
login페이지나 home페이지에는 이미지도 없어서 문제될 게 없다고 생각되는데
도통 이유를 모르겠습니다ㅠㅠㅠ
Answer 1
0
알아냈습니다...
loadUserByUsername
위 메소드는 UserDetailsService에 정의된 메소드에요
그래서 아이디를 username, 비밀번호는 password가 아닌 다른 이름, 다른 걸 받을 경우
config에 이를 명시해줘야 합니다.
명시 방법은 다음과 같아요.
.usernameParameter("userEmail")
.passwordParameter("userPassword")저는 username이 아닌 userEmail
password가 아닌 userPassword라는 이름으로 값을 받습니다.
password라는 이름에 해당하는 값이 없으니 로그인이 안됐던 거에요..하하하하
JWT를 구현한 다음 이 API를 호출해서 사용하는 것은 프론트엔드 쪽에서 하는 역할인가요?
0
94
1
Jwt쓰면 스프링시큐리티는 필수적으로 사용해야하나요?
0
401
1
13:23 system.out 출력문이 다르게 나옵니다.
0
129
1
수료증 문의
0
226
2
9분대에 질문이 있습니다 !
0
114
1
password 비교를 하지 않았는데 어떻게 인증이 통과된 건가요?
0
320
1
이전 강의 참고하라는 말씀
0
253
1
강의 실습하다가 막히는 분들 참고(2024년8월 기준)
2
1116
2
구글 소셜 로그인 302
0
200
1
오류 문의 _ org.springframework.orm.jpa.JpaSystemException: could not deserialize
1
584
1
[자바] 시큐리티 Config 참고
13
953
1
이론강의
0
280
1
SpringSecurity JWT 로그인 URL 2개 설정하는 방법
0
486
1
2024.06기준) 최근 SecurityConfig 설정 문의
0
921
3
구글 로그인시 authentication이 null 값이라고 에러가 발생합니다.
0
677
2
특정 url필터 거는 방법 이슈
0
422
1
강사님께서 말씀하시는 시큐리티세션이 SecurityContext인가요?
0
277
1
25강 마지막 테스트에서 오류
1
1044
2
jwt를 저장하는 위치에 궁금한 점이 있습니다.
0
298
1
mustache를 사용하지 않고 thymeleaf를 사용하려고 하는데
0
694
1
세션 인증방식이 REST 원칙에 위배되는 건가요?
0
337
1
jwt와 실제데이터의 관계
1
241
1
jwt 와 세션ID의 관계
1
310
1
SecurityConfig에서 세션 설정, 인가 설정
0
415
1

