Example usage for org.springframework.security.web.authentication.www DigestAuthUtils md5Hex

List of usage examples for org.springframework.security.web.authentication.www DigestAuthUtils md5Hex

Introduction

In this page you can find the example usage for org.springframework.security.web.authentication.www DigestAuthUtils md5Hex.

Prototype

static String md5Hex(String data) 

Source Link

Usage

From source file:org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint.java

public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    HttpServletResponse httpResponse = (HttpServletResponse) response;

    // compute a nonce (do not use remote IP address due to proxy farms)
    // format of nonce is:
    // base64(expirationTime + ":" + md5Hex(expirationTime + ":" + key))
    long expiryTime = System.currentTimeMillis() + (nonceValiditySeconds * 1000);
    String signatureValue = DigestAuthUtils.md5Hex(expiryTime + ":" + key);
    String nonceValue = expiryTime + ":" + signatureValue;
    String nonceValueBase64 = new String(Base64.getEncoder().encode(nonceValue.getBytes()));

    // qop is quality of protection, as defined by RFC 2617.
    // we do not use opaque due to IE violation of RFC 2617 in not
    // representing opaque on subsequent requests in same session.
    String authenticateHeader = "Digest realm=\"" + realmName + "\", " + "qop=\"auth\", nonce=\""
            + nonceValueBase64 + "\"";

    if (authException instanceof NonceExpiredException) {
        authenticateHeader = authenticateHeader + ", stale=\"true\"";
    }//from  www  . j  a v a  2s .c  o m

    if (logger.isDebugEnabled()) {
        logger.debug("WWW-Authenticate header sent to user agent: " + authenticateHeader);
    }

    httpResponse.addHeader("WWW-Authenticate", authenticateHeader);
    httpResponse.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
}