|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.acegisecurity.ui.webapp; |
|
16 |
| |
|
17 |
| import org.acegisecurity.AuthenticationException; |
|
18 |
| import org.acegisecurity.intercept.web.AuthenticationEntryPoint; |
|
19 |
| import org.acegisecurity.util.PortMapper; |
|
20 |
| import org.acegisecurity.util.PortMapperImpl; |
|
21 |
| import org.acegisecurity.util.PortResolver; |
|
22 |
| import org.acegisecurity.util.PortResolverImpl; |
|
23 |
| |
|
24 |
| import org.apache.commons.logging.Log; |
|
25 |
| import org.apache.commons.logging.LogFactory; |
|
26 |
| |
|
27 |
| import org.springframework.beans.factory.InitializingBean; |
|
28 |
| |
|
29 |
| import org.springframework.util.Assert; |
|
30 |
| |
|
31 |
| import java.io.IOException; |
|
32 |
| |
|
33 |
| import javax.servlet.ServletException; |
|
34 |
| import javax.servlet.ServletRequest; |
|
35 |
| import javax.servlet.ServletResponse; |
|
36 |
| import javax.servlet.http.HttpServletRequest; |
|
37 |
| import javax.servlet.http.HttpServletResponse; |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| public class AuthenticationProcessingFilterEntryPoint |
|
64 |
| implements AuthenticationEntryPoint, InitializingBean { |
|
65 |
| private static final Log logger = LogFactory.getLog(AuthenticationProcessingFilterEntryPoint.class); |
|
66 |
| private PortMapper portMapper = new PortMapperImpl(); |
|
67 |
| private PortResolver portResolver = new PortResolverImpl(); |
|
68 |
| private String loginFormUrl; |
|
69 |
| private boolean forceHttps = false; |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
6
| public void setForceHttps(boolean forceHttps) {
|
|
80 |
6
| this.forceHttps = forceHttps;
|
|
81 |
| } |
|
82 |
| |
|
83 |
2
| public boolean getForceHttps() {
|
|
84 |
2
| return forceHttps;
|
|
85 |
| } |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
8
| public void setLoginFormUrl(String loginFormUrl) {
|
|
95 |
8
| this.loginFormUrl = loginFormUrl;
|
|
96 |
| } |
|
97 |
| |
|
98 |
1
| public String getLoginFormUrl() {
|
|
99 |
1
| return loginFormUrl;
|
|
100 |
| } |
|
101 |
| |
|
102 |
11
| public void setPortMapper(PortMapper portMapper) {
|
|
103 |
11
| this.portMapper = portMapper;
|
|
104 |
| } |
|
105 |
| |
|
106 |
1
| public PortMapper getPortMapper() {
|
|
107 |
1
| return portMapper;
|
|
108 |
| } |
|
109 |
| |
|
110 |
10
| public void setPortResolver(PortResolver portResolver) {
|
|
111 |
10
| this.portResolver = portResolver;
|
|
112 |
| } |
|
113 |
| |
|
114 |
1
| public PortResolver getPortResolver() {
|
|
115 |
1
| return portResolver;
|
|
116 |
| } |
|
117 |
| |
|
118 |
10
| public void afterPropertiesSet() throws Exception {
|
|
119 |
10
| Assert.hasLength(loginFormUrl, "loginFormUrl must be specified");
|
|
120 |
9
| Assert.notNull(portMapper, "portMapper must be specified");
|
|
121 |
8
| Assert.notNull(portResolver, "portResolver must be specified");
|
|
122 |
| } |
|
123 |
| |
|
124 |
8
| public void commence(ServletRequest request, ServletResponse response,
|
|
125 |
| AuthenticationException authException) |
|
126 |
| throws IOException, ServletException { |
|
127 |
8
| HttpServletRequest req = (HttpServletRequest) request;
|
|
128 |
8
| String scheme = request.getScheme();
|
|
129 |
8
| String serverName = request.getServerName();
|
|
130 |
8
| int serverPort = portResolver.getServerPort(request);
|
|
131 |
8
| String contextPath = req.getContextPath();
|
|
132 |
| |
|
133 |
8
| boolean inHttp = "http".equals(scheme.toLowerCase());
|
|
134 |
8
| boolean inHttps = "https".equals(scheme.toLowerCase());
|
|
135 |
| |
|
136 |
8
| boolean includePort = ((inHttp && (serverPort == 80)) ||
|
|
137 |
| (inHttps && (serverPort == 443))); |
|
138 |
| |
|
139 |
8
| if ("http".equals(scheme.toLowerCase()) && (serverPort == 80)) {
|
|
140 |
2
| includePort = false;
|
|
141 |
| } |
|
142 |
| |
|
143 |
8
| if ("https".equals(scheme.toLowerCase()) && (serverPort == 443)) {
|
|
144 |
1
| includePort = false;
|
|
145 |
| } |
|
146 |
| |
|
147 |
8
| String redirectUrl = contextPath + loginFormUrl;
|
|
148 |
| |
|
149 |
8
| if (forceHttps && inHttp) {
|
|
150 |
5
| Integer httpPort = new Integer(portResolver.getServerPort(request));
|
|
151 |
5
| Integer httpsPort = (Integer) portMapper.lookupHttpsPort(httpPort);
|
|
152 |
| |
|
153 |
5
| if (httpsPort != null) {
|
|
154 |
4
| if (httpsPort.intValue() == 443) {
|
|
155 |
1
| includePort = false;
|
|
156 |
| } else { |
|
157 |
3
| includePort = true;
|
|
158 |
| } |
|
159 |
| |
|
160 |
4
| redirectUrl = "https://" + serverName +
|
|
161 |
4
| ((includePort) ? (":" + httpsPort) : "") + contextPath +
|
|
162 |
| loginFormUrl; |
|
163 |
| } |
|
164 |
| } |
|
165 |
| |
|
166 |
8
| if (logger.isDebugEnabled()) {
|
|
167 |
0
| logger.debug("Redirecting to: " + redirectUrl);
|
|
168 |
| } |
|
169 |
| |
|
170 |
8
| ((HttpServletResponse) response).sendRedirect(((HttpServletResponse) response).encodeRedirectURL(
|
|
171 |
| redirectUrl)); |
|
172 |
| } |
|
173 |
| } |