• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Spring 게시판 로그인시 권한 정보 저장이 안되는 것 같아요~

로그인시 권한 정보 저장이 안되는 것 같아요~

  • 이 주제에는 7개 답변, 3명 참여가 있으며 도준엽11 월 전에 전에 마지막으로 업데이트했습니다.
8 글 보임 - 1 에서 8 까지 (총 8 중에서)
  • 글쓴이
  • #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);
    }
    }
    
    
    로그인할 때 여기를 안들어오고 있어서 그런건가 싶은데
    여기는 왜 안들어오는지도 모르겠어요;;
    
    
    
    
    #124595

    codingapple
    키 마스터
    SecurityConfig 클래스나 로그인폼이 뭔가 이상한게 아닐까요
    #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  코드예요. 
    'ㅁ';;
    
    
    #124617

    codingapple
    키 마스터
    폼의 action을 /login으로 바꿔봅시다
    #124630

    lovejinb
    참가자
    폼의 action을 login으로 바꿨더니 loadUserByUsername 함수 들어왔어요!!
    
    그런데 로그인해도 auth는 여전히 null 이예요..
    
    다시 차근차근.. 검토해보고 질문할께요 ㅜㅜ
    #124661

    codingapple
    키 마스터
    .findByUsername 으로 찾은게 비어있어서 그럴수도요
    #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]]
    스프링 시큐리티 로직을 태워야 하는데
    제가 따로 만든 컨트롤러를 다녀와서 권한저장이 안되었던 것 같아요.
    
    감사합니다 :)
    
    #128971

    도준엽
    참가자
    같은 문제가 있는데 어떻게 해결하신 건가요.
8 글 보임 - 1 에서 8 까지 (총 8 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠