|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.securechannel; |
|
17 |
| |
|
18 |
| import org.acegisecurity.util.PortMapper; |
|
19 |
| import org.acegisecurity.util.PortMapperImpl; |
|
20 |
| import org.acegisecurity.util.PortResolver; |
|
21 |
| import org.acegisecurity.util.PortResolverImpl; |
|
22 |
| |
|
23 |
| import org.apache.commons.logging.Log; |
|
24 |
| import org.apache.commons.logging.LogFactory; |
|
25 |
| |
|
26 |
| import org.springframework.beans.factory.InitializingBean; |
|
27 |
| import org.springframework.util.Assert; |
|
28 |
| |
|
29 |
| import java.io.IOException; |
|
30 |
| |
|
31 |
| import javax.servlet.ServletException; |
|
32 |
| import javax.servlet.ServletRequest; |
|
33 |
| import javax.servlet.ServletResponse; |
|
34 |
| import javax.servlet.http.HttpServletRequest; |
|
35 |
| import javax.servlet.http.HttpServletResponse; |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| public class RetryWithHttpsEntryPoint implements InitializingBean, |
|
51 |
| ChannelEntryPoint { |
|
52 |
| |
|
53 |
| |
|
54 |
| private static final Log logger = LogFactory.getLog(RetryWithHttpsEntryPoint.class); |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| private PortMapper portMapper = new PortMapperImpl(); |
|
59 |
| private PortResolver portResolver = new PortResolverImpl(); |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
6
| public void setPortMapper(PortMapper portMapper) {
|
|
64 |
6
| this.portMapper = portMapper;
|
|
65 |
| } |
|
66 |
| |
|
67 |
1
| public PortMapper getPortMapper() {
|
|
68 |
1
| return portMapper;
|
|
69 |
| } |
|
70 |
| |
|
71 |
6
| public void setPortResolver(PortResolver portResolver) {
|
|
72 |
6
| this.portResolver = portResolver;
|
|
73 |
| } |
|
74 |
| |
|
75 |
1
| public PortResolver getPortResolver() {
|
|
76 |
1
| return portResolver;
|
|
77 |
| } |
|
78 |
| |
|
79 |
6
| public void afterPropertiesSet() throws Exception {
|
|
80 |
6
| Assert.notNull(portMapper, "portMapper is required");
|
|
81 |
5
| Assert.notNull(portResolver, "portResolver is required");
|
|
82 |
| } |
|
83 |
| |
|
84 |
5
| public void commence(ServletRequest request, ServletResponse response)
|
|
85 |
| throws IOException, ServletException { |
|
86 |
5
| HttpServletRequest req = (HttpServletRequest) request;
|
|
87 |
| |
|
88 |
5
| String pathInfo = req.getPathInfo();
|
|
89 |
5
| String queryString = req.getQueryString();
|
|
90 |
5
| String contextPath = req.getContextPath();
|
|
91 |
5
| String destination = req.getServletPath()
|
|
92 |
5
| + ((pathInfo == null) ? "" : pathInfo)
|
|
93 |
5
| + ((queryString == null) ? "" : ("?" + queryString));
|
|
94 |
| |
|
95 |
5
| String redirectUrl = contextPath;
|
|
96 |
| |
|
97 |
5
| Integer httpPort = new Integer(portResolver.getServerPort(req));
|
|
98 |
5
| Integer httpsPort = portMapper.lookupHttpsPort(httpPort);
|
|
99 |
| |
|
100 |
5
| if (httpsPort != null) {
|
|
101 |
4
| boolean includePort = true;
|
|
102 |
| |
|
103 |
4
| if (httpsPort.intValue() == 443) {
|
|
104 |
2
| includePort = false;
|
|
105 |
| } |
|
106 |
| |
|
107 |
4
| redirectUrl = "https://" + req.getServerName()
|
|
108 |
4
| + ((includePort) ? (":" + httpsPort) : "") + contextPath
|
|
109 |
| + destination; |
|
110 |
| } |
|
111 |
| |
|
112 |
5
| if (logger.isDebugEnabled()) {
|
|
113 |
0
| logger.debug("Redirecting to: " + redirectUrl);
|
|
114 |
| } |
|
115 |
| |
|
116 |
5
| ((HttpServletResponse) response).sendRedirect(((HttpServletResponse) response)
|
|
117 |
| .encodeRedirectURL(redirectUrl)); |
|
118 |
| } |
|
119 |
| } |