AI교육과정 - Spring project.1

단비·2023년 1월 3일
0

AI교육과정

목록 보기
51/69
  • MockMvc
    • MVC(Model View Controller)

    • 웹 어플리케이션을 어플리케이션 서버에 배포하지 않고 테스트용 MVC 환경을 만들어 요청 및 전송, 응답을 제공하는 유틸리티 클래스

    • mvc.perform(get("/hello"))

      • MockMvc를 통해 /hello주소로 get요청 가능
    • .andExcept
      - mvc.perform의 결과(status)를 검증

      status().isOk
      
      .andExcept(status().isOk)
      
      - perform의 결과가 성공적인지(200,404,500 등)
      
      content().string(~)
      
      .andExcept(content().string(hello))
      
      - mvc.perform의 결과를 검증하는 것으로 응답의 본문의 내용을 검증
      - perform 내용중 ~ 값이 있는지 검증
      
      model().attributeExists("article")
      
      .andExpect(model().attributeExists("article"))
      
      - model 의 프로퍼티 중 articles라는 프로퍼티가 있는지 검증
      mvc.perform(get("/api/articles"))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.valueOf("application/hal+json")));
      				.andExpect(model().attributeExists("article"));
  • Query DSL
    • JPA를 좀 더 효율적으로 사용할 수 있는 라이브러리
    • 오픈소스 프로젝트로 JPQL을 Java 코드로 작성할 수 있도록 함
    • 정적 타입을 이용해서 SQL과 같은 쿼리를 생성해줌

  • html 에 타임리프 입히기
    • Spring Configuration Processor
      • 디펜던시에 하기 코드 추가

        annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    • th.xml 파일에 하기 코드 추가
      <?xml version="1.0" ?>
      <thlogic>
          <attr sel="#header" th:replace="header :: header"></attr>
          <attr sel="#footer" th:replace="footer :: footer"></attr>
      </thlogic>
    • @ConfigurationPropertiesScan
      • @EnableConfigurationProperties를 이용해 설정 프로퍼티 클래스를 사용하는 경우 클래스가 많아지면 코드가 무거워지고 복잡해짐
      • @Configuration 으로 선언된 프로퍼티스를 자동으로 모두 찾아줌 (사용자가 선언한 configuration 사용 시에만 선언해도됨. JPA, Security등 spring기본 configuration 사용 시에는 선언하지 않아도됨)
  • Spring Security
    • HttpSecurity 설정
      • 리소스(URL) 접근 권한 설정
      • 인증 전체 흐름에 필요한 Login, Logout 페이지 인증완료 후 페이지 인증 실패 시 이동페이지 등등 설정
      • 인증 로직을 커스텀하기위한 커스텀 필터 설정
    • 리소스(URL)의 권한한 설정
      • antMatchers 특정 리소스에 대한 권한을 설정
        • anyRequest

          모든 리소스를 의미

        • permitAll

          antMatchers 설정한 리소스의 접근을 인증절차 없이 허용한다는 의미
          .authorizeRequests().antMatchers("/login**", "/web-resources/**", "/actuator/**").permitAll()
          .authorizeRequests(auth -> auth.anyRequest().permitAll())
    • 로그인처리 설정
      • formLogin 로그인 페이지와 기타 로그인 처리 및 성공 실패 처리를 사용하겠다는 의미 http.formLogin() 를 호출하지 않으면 완전히 로그인처리 커스텀필터를 만들고 설정하지 않는 이상 로그인 페이지 및 기타 처리를 할 수 없음
      • loginPage 사용자가 만든 로그인 페이지를 사용할 때
      • loginProcessingUrl 로그인 즉 인증 처리를 하는 URL을 설정, 인증처리를 수행하는 필터가 호출
      • defaultSuccessUrl 정상적으로 인증 성공 했을 경우 이동하는 페이지
      • failureUrl 인증 실패 했을 경우 이동하는 페이지
      • successHandler 정상적인증 성공 후 별도의 처리가 필요한 경우 커스텀 핸들러를 생성하여 등록
      • failureHandler 인증 실패 했을 때 별도의 처리가 필요한 경우 커스텀 핸들러를 생성하여 등록
    • 커스텀 필터 등록
      • addFilterBefore 지정된 필터 앞에 커스텀 필터 추가
      • addFilterAfter 지정된 필터 뒤에 커스텀 필터 추가
  • Model(인터페이스), ModelAndMap(클래스)
    • addAttribute를 통해 데이터 저장 addAttribute(”key”, value)
    • 데이터 저장
  • ModelAndView
    • addObject를 통해 데이터를 저장

      addObject(”key”, value), setViewName(” … “)

    • setViewName을 통해 이동하고자 하는 View페이지를 저장

    • Model & View 동시에 리턴 가능

      ModelAndView mv = new ModelAndView();
      return type ModelAndView
  • @RepositoryRestResource
    • controller를 만들지 않아도 자동으로 REST API 가 생성됨
profile
tistory로 이전! https://sweet-rain-kim.tistory.com/

0개의 댓글