일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- AJAX
- IntelliJ
- STS
- Core Java
- error
- Tomcat
- 오픈소스
- Open Source
- Exception
- oracle
- Thymeleaf
- Docker
- git
- PostgreSQL
- spring
- Spring Boot
- JDBC
- 설정
- Source
- Eclipse
- 문서
- MySQL
- JavaScript
- SpringBoot
- ubuntu
- Python
- jpa
- myBatis
- MSSQL
- maven
- Today
- Total
헤르메스 LIFE
[SpringBoot] Interceptor 본문
Interceptor 구현
Interceptor는 HandlerInterceptor 인터페이스를 구현하거나, HandlerInterceptorAdapter를 상속받은 클래스를 만들고 다음의 메소드를 구현합니다.
preHandler - Controller의 메서드가 호출되기 전 호출됩니다. 이 메서드가 false 를 반환하면 코드의 흐름이 중단됩니다.
postHandler - Controller의 메서드의 수행이 완료되고, view 처리를 수행하기 전에 호출됩니다.
afterCompletion - view 처리까지 완료되고 응답결과가 브라우저로 전달되기 전에 호출됩니다.
package com.study.myweb.interceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class AuthInterceptor implements HandlerInterceptor {
public boolean isInstance( Object handler, Class<?> clazz ) {
if( !( handler instanceof HandlerMethod ) ) {
return false;
}
Object object = ( (HandlerMethod)handler ).getBean();
if( clazz.isInstance( object ) ) {
return true;
}
return false;
}
@Override
public boolean preHandle( HttpServletRequest request, HttpServletResponse response, Object handler ) throws Exception {
// 1. handler 종류 확인
// Controller에 있는 메서드이므로 HandlerMethod 타입인지 체크
if( !( handler instanceof HandlerMethod ) ) {
// return true이면 Controller에 있는 메서드가 아니므로, 그대로 컨트롤러로 진행
return true;
}
String targetURI = request.getServletPath();
StringBuilder sb = new StringBuilder();
// request 정보
sb.append( "\n[preHandle] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" );
sb.append( "\n#remoteIp : " ).append( request.getRemoteAddr() );
sb.append( "\n#targetURI : " ).append( targetURI );
sb.append( "\n#reqUrl : " ).append( request.getRequestURL().toString() );
sb.append( "\n#userAgent : " ).append( request.getHeader( "User-Agent" ) );
sb.append( "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" );
System.out.println( sb.toString() );
return true;
}
}
Interceptor 등록
package com.study.myweb.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
// Spring MVC 프로젝트에 관련된 설정을 하는 클래스
@Configuration
//@EnableWebMvc
public class WebMVCConfig implements WebMvcConfigurer {
@Autowired
@Qualifier( value = "authInterceptor" )
private HandlerInterceptor authInterceptor;
/**
*
*/
@Override
public void addInterceptors( InterceptorRegistry registry ) {
registry.addInterceptor( authInterceptor ).addPathPatterns( "/**" );
}
}
오류 : Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'
https://hermeslog.tistory.com/548
Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'
Interceptor를 등록 후 아래와 같은 오류가 발생하였습니다. Could not resolve view with name 'index' in servlet with name 'dispatcherServlet' package com.study.myweb.config; import org.springframework...
hermeslog.tistory.com
출처 : 윤재성 - 만들면서 배우는 Spring MVC5
https://sas-study.tistory.com/274
[번역글] 스프링 vs 스프링 부트 차이 비교하기!
DZone이라는 사이트에서 번역해서 가져온 글입니다. 간단히 읽어보시고 원문을 읽어보고싶으시면 아래 url로 들어가셔서 바로 보시면 될것 같습니다. 참고문 정도로 보시면 좋겠습니다. 원문 : htt
sas-study.tistory.com
'Spring Boot Framework' 카테고리의 다른 글
[SpringBoot] JPA Entity 생성 테스트 (0) | 2021.12.01 |
---|---|
[SpringBoot] Logback 설정 (2) | 2021.07.30 |
[SpringBoot] SpringBoot 기반 Web Project 생성 (1) | 2021.07.27 |
[Spring Boot] Spring Resttemplate 예외 처리 (0) | 2021.01.31 |
[Spring Boot] Spring Resttemplate Sample (0) | 2021.01.31 |