Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentResolver에서 request에 지정한 attribute 꺼내오기 #236

Open
Jhvictor4 opened this issue Oct 5, 2022 · 0 comments
Open

Comments

@Jhvictor4
Copy link
Contributor

Jhvictor4 commented Oct 5, 2022

안녕하세요, 2차시 세션에서 설명이 제대로 이루어지지 않은 부분이 있었는데요!

저희가 package javax.servlet.http 안에 있는 HttpServletRequest를 사용해서 setAttribute 해주기 때문에, 어트리뷰트를 가져올 때도 이 객체로부터 정보를 얻어와야 합니다.

request.getAttribute() 메소드를 사용할 때, 스프링에 내장된 NativeWebRequest 타입의 getAttribute 메소드는 시그니처로 name뿐만 아니라 scope를 받습니다. 스코프는 세션 레벨에서의 다양한 정보 저장을 위한 기능인데요,

아래처럼 NativeWebRequest 타입의 요청을 ServletWebRequest라는 객체로 감싸주면 (어댑터 클래스입니다),
그 객체 내부에 있는 request 프로퍼티를 통해 인터셉터에서 가로챘던 HttpServletRequest 에 접근할 수 있습니다.

    override fun resolveArgument(
        parameter: MethodParameter,
        mavContainer: ModelAndViewContainer?,
        webRequest: NativeWebRequest,
        binderFactory: WebDataBinderFactory?
    ): Any? {
        parameter.hasMethodAnnotation(UserContext::class.java)
        val password = (webRequest as ServletWebRequest).request.getAttribute("pwd")
        return password
    }

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant