关于POST的Spring方面:访问HTTPRequest以查找登录的用户



我有一个用于restful服务的接口,我用一个方面封装了它。

@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调用的某个方面提出一种访问已验证用户的方法吗?

感谢

如果您可以从HttpServletRequest访问您需要的信息,那么您可以使用Spring的RequestContextHolder来访问这些信息。

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

它有帮助吗?

添加到web.xml

<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;

然后一些代码。。。(在这种情况下,它从消息摘要loggon中提取)

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;
  }

最新更新