8 글 보임 - 1 에서 8 까지 (총 8 중에서)
-
글쓴이글
-
2024년 5월 31일 09:11 #124592
lovejinb참가자@GetMapping("/my-page") public String myPage(Authentication auth) { System.out.println(auth); return "mypage.html"; } 선생님~ auth 값에 계속 null 이 떠요... -----------------------
@Service @RequiredArgsConstructor public class MyUserDetailsService implements UserDetailsService {
private final MemberRepository memberRepository;
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
var result = memberRepository.findByUsername(username); if(result.isEmpty()) { throw new UsernameNotFoundException("그런 아이디 없음"); } var user = result.get();
List<GrantedAuthority> authorities = new ArrayList<>(); authorities.add(new SimpleGrantedAuthority("user"));
return new User(user.getUsername(), user.getPassword(), authorities); } } 로그인할 때 여기를 안들어오고 있어서 그런건가 싶은데 여기는 왜 안들어오는지도 모르겠어요;;
2024년 5월 31일 10:39 #124601
lovejinb참가자@Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.csrf((csrf) -> csrf.disable());
http.authorizeHttpRequests((authorize) -> authorize.requestMatchers("/**").permitAll() ); http.formLogin((formLogin) -> formLogin.loginPage("/login") .defaultSuccessUrl("/") //.failureUrl("/fail") ); http.logout(logout -> logout.logoutUrl("/logout"));
return http.build(); }
<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link href="/common.css" rel="stylesheet"> </head> <body> <div th:replace="~{nav.html :: navbar}"></div>
<h4>로그인</h4>
<div th:if="${param.error}"> <h4>아이디나 비번 틀림</h4> </div>
<form action="/doLogin" method="POST"> <input type="text" name="username" placeholder="아이디"> <input type="password" name="password" placeholder="비밀번호"> <button type="submit">전송</button> </form>
</body> </html> SecurityConfig 클래스랑 로그인 html 코드예요. 'ㅁ';;
2024년 5월 31일 17:56 #124630
lovejinb참가자폼의 action을 login으로 바꿨더니 loadUserByUsername 함수 들어왔어요!! 그런데 로그인해도 auth는 여전히 null 이예요.. 다시 차근차근.. 검토해보고 질문할께요 ㅜㅜ
2024년 6월 3일 10:18 #124803
lovejinb참가자이제 잘되요!! UsernamePasswordAuthenticationToken [Principal=com.zalzal.jdr.member.CustomUser [Username=qweqwe, Password=[PROTECTED], Enabled=true, AccountNonExpired=true, CredentialsNonExpired=true, AccountNonLocked=true, Granted Authorities=[user]], Credentials=[PROTECTED], Authenticated=true, Details=WebAuthenticationDetails [RemoteIpAddress=0:0:0:0:0:0:0:1, SessionId=34D2C10F762FAF844BAFAD70132A870C], Granted Authorities=[user]]
스프링 시큐리티 로직을 태워야 하는데 제가 따로 만든 컨트롤러를 다녀와서 권한저장이 안되었던 것 같아요. 감사합니다 :)
-
글쓴이글
8 글 보임 - 1 에서 8 까지 (총 8 중에서)
- 답변은 로그인 후 가능합니다.