Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags more
Archives
Today
Total
관리 메뉴

요리사에서 IT개발자로

스파르타 부트캠프 Spring Master 3강 Spring Security 로그인 본문

Spring

스파르타 부트캠프 Spring Master 3강 Spring Security 로그인

H.S-Backend 2024. 5. 27. 17:22

 Spring Security 사용 전


Spring Security 사용 후

 

Client의 요청은 모두 Spring Security를 거친다.

 

Spring Security의 역할

인증 / 인가

 

성공시 

Controller로 Client 요청 을 전달한다.

Client 요청 + 사용자 정보(UserDetails)

 

실패

Controller로 Client 요청을 전달하지않는다

(Error Response)


 

로그인 처리 과정

 

Client가 로그인을 시도한다

로그인 시도할 username과 password정보를 HTTP body로 전달한다(POST)

 

인증 관리자(Authentication Manager)

UserDetailsService에게 username을 전달하고

회원상세 정보를 요청한다.

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    // CSRF 설정
    http.csrf((csrf) -> csrf.disable());

    http.authorizeHttpRequests((authorizeHttpRequests) ->
            authorizeHttpRequests
                    .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() // resources 접근 허용 설정
                    .anyRequest().authenticated() // 그 외 모든 요청 인증처리
    );

    // 로그인 사용
    http.formLogin((formLogin) ->
            formLogin
                    // 로그인 처리 (POST /api/user/login)
                    .loginProcessingUrl("/api/user/login").permitAll()
    );

    return http.build();
}

 

UserDetatilsService는

 

회원 DB에서 회원을 조회한다.

회원 정보가 없으면 Error를 발생한다.

 

User user = userRepository.findByUsername(username)
        .orElseThrow(() -> new UsernameNotFoundException("Not Found " + username));

 

조회된 회원 정보(user)를 UserDetails로 변환한다.

UserDetails userDetails = new UserDetailsImpl(user);

 

UserDetails

인증관리자 (Authentication Manager) 에게 전달한다

 

인증관리자가 인증처리를 할때

Client가 로그인 시도username, password 일치 여부확인한다.

userDetailsService가 전달해준 UserDetails의 username, password 일치 여부를 확인한다.

 

password를 비교할 때

Client가 보낸 password는 평문, UserDetailspassword는 암호문이다.

Client가 보낸 password를 암호화해서 비교한다.

 

인증을 성공하면 

세션에 로그인 정보를 저장하고

실패하면 Error를 발생한다.


로그인 구현

로그인 처리URL 설정

 

로그인 처리 URL을 설정한다.(Spring Security를 사용하면 인가처리가 편해진다)


 

.requestMatchers("/api/user/**").permitAll() // '/api/user/'로 시작하는 요청 모두 접근 허가

이 요청들은

로그인, 회원가입 관련 요청이기에 비회원, 회원 누구나 접근이 가능하다.

 


 

.anyRequest().authenticated() // 그 외 모든 요청 인증처리

인증이 필요한 URL들도 간편하게 처리할 수 있다.

 


DB회원 정보 조회도
Spring Security 인증 관리자에게 전달한다.

@AuthenticationPrincipal

@Controller
@RequestMapping("/api")
public class ProductController {

    @GetMapping("/products")
    public String getProducts(@AuthenticationPrincipal UserDetailsImpl userDetails) {

        User user = userDetails.getUser();
        System.out.println("user.getUsername() = " + user.getUsername());
        System.out.println("user.getEmail() = " + user.getEmail());
        return "redirect:/";
    }
}

 

Authentication의 Pricipal에 저장된

UserDetailsImpl을 가져올 수 있다.

 

UserDetailsImpl에 저장된 인증된 사용자인 User객체를 사용할 수 있다.

 


 

@AuthenticationPrincipal을 사용하여 메인페이지 사용자 이름 반영

@Controller
public class HomeController {

    @GetMapping("/")
    public String home(Model model, @AuthenticationPrincipal UserDetailsImpl userDetails) {
        model.addAttribute("username", userDetails.getUsername());
        return "index";
    }
}

반응형