2013-06-27 5 views
2

나는 Aspect로 감싸는 편안한 서비스를위한 인터페이스를 가지고있다.POST 주변의 스프링 aspect : 로그온 한 사용자를 찾기 위해 HTTPRequest에 접근하기

@Path("/rs-service/") 
public interface ServiceRSI 
{ 
    @Override 
    @POST 
    @Path("/lookupParam/") 
    @Produces(MediaType.APPLICATION_XML) 
    ParamValue getParamValue(GetParamValue request); 
} 

그럼 내 XML 측면에 .. 할

<aop:config> 
    <aop:aspect id="auditLoggingAspect" ref="auditLogging"> 
     <aop:pointcut id="aspectA" expression="execution(* com.ServiceRSI.*(..))" /> 
       <aop:around pointcut-ref="aspectA" method="logRequest" /> 
      /> 
    </aop:aspect> 
</aop:config> 

내가 원하는/필요는이 요청을하려면 인증 된 사용자이었다 측면에서 로그입니다. 인증 과정에서 MessageDigest를 사용하고 있습니다.

일반적으로 HTTPRequest에 액세스하여 전화를 걸 때 인증 된 사용자를 찾을 수 있지만이 경우에는 메서드에 전달되지 않으므로이를 가로 채지 못합니다.

누구나 restufull 통화와 관련된 측면에서 인증 된 사용자에게 액세스 할 수있는 방법을 제안 할 수 있습니까?

감사

답변

1

은 추가 당신이 그것을에 액세스해야하는 클래스에서

<listener> 
    <listener-class> 
    org.springframework.web.context.request.RequestContextListener 
    </listener-class> 
</listener> 
<listener> 
    <listener-class> 
    org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 

...

@Autowired 
    private HttpServletRequest context; 

그런 다음 몇 가지 코드가 ... (이 경우에는이 메시지 다이제스트에서 추출을 web.xml에 할 로그 온)

private String getAuthenticationUser() 
    { 
    String authorization = context.getHeader("authorization"); 
    if (authorization.startsWith("Digest response")) { 
     String[] splits = authorization.split(","); 
     for (String split : splits) 
     { 
     String[] splitKeyValue = split.trim().split("="); 
     if(splitKeyValue[0].equalsIgnoreCase("username")) { 
      authorization = splitKeyValue[1]; 
      authorization = authorization.replace("\"", ""); 
      break; 
     } 
     } 
    } 
    return authorization; 
    } 
1

당신이 HttpServletRequest에서 필요한 정보를 액세스 할 수있는 경우에, 당신은이 정보에 액세스하려면 봄의 RequestContextHolder를 사용할 수 있습니다.

ServletRequestAttributes t = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); 
HttpServletRequest req = t.getRequest(); 

도움이 되셨습니까?