|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.ui.digestauth; |
|
17 |
| |
|
18 |
| import org.acegisecurity.AuthenticationException; |
|
19 |
| import org.acegisecurity.intercept.web.AuthenticationEntryPoint; |
|
20 |
| |
|
21 |
| import org.apache.commons.codec.binary.Base64; |
|
22 |
| import org.apache.commons.codec.digest.DigestUtils; |
|
23 |
| import org.apache.commons.logging.Log; |
|
24 |
| import org.apache.commons.logging.LogFactory; |
|
25 |
| |
|
26 |
| import org.springframework.beans.factory.InitializingBean; |
|
27 |
| |
|
28 |
| import java.io.IOException; |
|
29 |
| |
|
30 |
| import javax.servlet.ServletException; |
|
31 |
| import javax.servlet.ServletRequest; |
|
32 |
| import javax.servlet.ServletResponse; |
|
33 |
| import javax.servlet.http.HttpServletResponse; |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| public class DigestProcessingFilterEntryPoint |
|
55 |
| implements AuthenticationEntryPoint, InitializingBean { |
|
56 |
| |
|
57 |
| |
|
58 |
| private static final Log logger = LogFactory.getLog(DigestProcessingFilterEntryPoint.class); |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| private String key; |
|
63 |
| private String realmName; |
|
64 |
| private int nonceValiditySeconds = 300; |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
32
| public void setKey(String key) {
|
|
69 |
32
| this.key = key;
|
|
70 |
| } |
|
71 |
| |
|
72 |
10
| public String getKey() {
|
|
73 |
10
| return key;
|
|
74 |
| } |
|
75 |
| |
|
76 |
42
| public void setNonceValiditySeconds(int nonceValiditySeconds) {
|
|
77 |
42
| this.nonceValiditySeconds = nonceValiditySeconds;
|
|
78 |
| } |
|
79 |
| |
|
80 |
2
| public int getNonceValiditySeconds() {
|
|
81 |
2
| return nonceValiditySeconds;
|
|
82 |
| } |
|
83 |
| |
|
84 |
32
| public void setRealmName(String realmName) {
|
|
85 |
32
| this.realmName = realmName;
|
|
86 |
| } |
|
87 |
| |
|
88 |
15
| public String getRealmName() {
|
|
89 |
15
| return realmName;
|
|
90 |
| } |
|
91 |
| |
|
92 |
32
| public void afterPropertiesSet() throws Exception {
|
|
93 |
32
| if ((realmName == null) || "".equals(realmName)) {
|
|
94 |
1
| throw new IllegalArgumentException("realmName must be specified");
|
|
95 |
| } |
|
96 |
| |
|
97 |
31
| if ((key == null) || "".equals(key)) {
|
|
98 |
1
| throw new IllegalArgumentException("key must be specified");
|
|
99 |
| } |
|
100 |
| } |
|
101 |
| |
|
102 |
26
| public void commence(ServletRequest request, ServletResponse response,
|
|
103 |
| AuthenticationException authException) |
|
104 |
| throws IOException, ServletException { |
|
105 |
26
| HttpServletResponse httpResponse = (HttpServletResponse) response;
|
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
26
| long expiryTime = System.currentTimeMillis()
|
|
111 |
| + (nonceValiditySeconds * 1000); |
|
112 |
26
| String signatureValue = new String(DigestUtils.md5Hex(expiryTime + ":"
|
|
113 |
| + key)); |
|
114 |
26
| String nonceValue = expiryTime + ":" + signatureValue;
|
|
115 |
26
| String nonceValueBase64 = new String(Base64.encodeBase64(
|
|
116 |
| nonceValue.getBytes())); |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
26
| String authenticateHeader = "Digest realm=\"" + realmName + "\", "
|
|
122 |
| + "qop=\"auth\", nonce=\"" + nonceValueBase64 + "\""; |
|
123 |
| |
|
124 |
26
| if (authException instanceof NonceExpiredException) {
|
|
125 |
2
| authenticateHeader = authenticateHeader + ", stale=\"true\"";
|
|
126 |
| } |
|
127 |
| |
|
128 |
26
| if (logger.isDebugEnabled()) {
|
|
129 |
0
| logger.debug("WWW-Authenticate header sent to user agent: "
|
|
130 |
| + authenticateHeader); |
|
131 |
| } |
|
132 |
| |
|
133 |
26
| httpResponse.addHeader("WWW-Authenticate", authenticateHeader);
|
|
134 |
26
| httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED,
|
|
135 |
| authException.getMessage()); |
|
136 |
| } |
|
137 |
| } |