1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.shiro.guice.web; |
20 | |
|
21 | |
import java.util.Collection; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.LinkedHashMap; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import javax.servlet.Filter; |
27 | |
import javax.servlet.ServletContext; |
28 | |
|
29 | |
import org.apache.shiro.config.ConfigurationException; |
30 | |
import org.apache.shiro.env.Environment; |
31 | |
import org.apache.shiro.guice.ShiroModule; |
32 | |
import org.apache.shiro.mgt.SecurityManager; |
33 | |
import org.apache.shiro.session.mgt.SessionManager; |
34 | |
import org.apache.shiro.web.env.WebEnvironment; |
35 | |
import org.apache.shiro.web.filter.PathMatchingFilter; |
36 | |
import org.apache.shiro.web.filter.authc.AnonymousFilter; |
37 | |
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter; |
38 | |
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter; |
39 | |
import org.apache.shiro.web.filter.authc.LogoutFilter; |
40 | |
import org.apache.shiro.web.filter.authc.UserFilter; |
41 | |
import org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter; |
42 | |
import org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter; |
43 | |
import org.apache.shiro.web.filter.authz.PortFilter; |
44 | |
import org.apache.shiro.web.filter.authz.RolesAuthorizationFilter; |
45 | |
import org.apache.shiro.web.filter.authz.SslFilter; |
46 | |
import org.apache.shiro.web.filter.mgt.FilterChainResolver; |
47 | |
import org.apache.shiro.web.filter.session.NoSessionCreationFilter; |
48 | |
import org.apache.shiro.web.mgt.DefaultWebSecurityManager; |
49 | |
import org.apache.shiro.web.mgt.WebSecurityManager; |
50 | |
import org.apache.shiro.web.session.mgt.ServletContainerSessionManager; |
51 | |
|
52 | |
import com.google.inject.Binder; |
53 | |
import com.google.inject.Key; |
54 | |
import com.google.inject.TypeLiteral; |
55 | |
import com.google.inject.binder.AnnotatedBindingBuilder; |
56 | |
import com.google.inject.name.Names; |
57 | |
import com.google.inject.servlet.ServletModule; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public abstract class ShiroWebModule extends ShiroModule { |
67 | |
@SuppressWarnings({"UnusedDeclaration"}) |
68 | 2 | public static final Key<AnonymousFilter> ANON = Key.get(AnonymousFilter.class); |
69 | |
@SuppressWarnings({"UnusedDeclaration"}) |
70 | 2 | public static final Key<FormAuthenticationFilter> AUTHC = Key.get(FormAuthenticationFilter.class); |
71 | |
@SuppressWarnings({"UnusedDeclaration"}) |
72 | 2 | public static final Key<BasicHttpAuthenticationFilter> AUTHC_BASIC = Key.get(BasicHttpAuthenticationFilter.class); |
73 | |
@SuppressWarnings({"UnusedDeclaration"}) |
74 | 2 | public static final Key<NoSessionCreationFilter> NO_SESSION_CREATION = Key.get(NoSessionCreationFilter.class); |
75 | |
@SuppressWarnings({"UnusedDeclaration"}) |
76 | 2 | public static final Key<LogoutFilter> LOGOUT = Key.get(LogoutFilter.class); |
77 | |
@SuppressWarnings({"UnusedDeclaration"}) |
78 | 2 | public static final Key<PermissionsAuthorizationFilter> PERMS = Key.get(PermissionsAuthorizationFilter.class); |
79 | |
@SuppressWarnings({"UnusedDeclaration"}) |
80 | 2 | public static final Key<PortFilter> PORT = Key.get(PortFilter.class); |
81 | |
@SuppressWarnings({"UnusedDeclaration"}) |
82 | 2 | public static final Key<HttpMethodPermissionFilter> REST = Key.get(HttpMethodPermissionFilter.class); |
83 | |
@SuppressWarnings({"UnusedDeclaration"}) |
84 | 2 | public static final Key<RolesAuthorizationFilter> ROLES = Key.get(RolesAuthorizationFilter.class); |
85 | |
@SuppressWarnings({"UnusedDeclaration"}) |
86 | 2 | public static final Key<SslFilter> SSL = Key.get(SslFilter.class); |
87 | |
@SuppressWarnings({"UnusedDeclaration"}) |
88 | 2 | public static final Key<UserFilter> USER = Key.get(UserFilter.class); |
89 | |
|
90 | |
|
91 | |
static final String NAME = "SHIRO"; |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | 10 | private final Map<String, Key<? extends Filter>[]> filterChains = new LinkedHashMap<String, Key<? extends Filter>[]>(); |
98 | |
private final ServletContext servletContext; |
99 | |
|
100 | 10 | public ShiroWebModule(ServletContext servletContext) { |
101 | 10 | this.servletContext = servletContext; |
102 | 10 | } |
103 | |
|
104 | |
public static void bindGuiceFilter(Binder binder) { |
105 | 0 | binder.install(guiceFilterModule()); |
106 | 0 | } |
107 | |
|
108 | |
@SuppressWarnings({"UnusedDeclaration"}) |
109 | |
public static void bindGuiceFilter(final String pattern, Binder binder) { |
110 | 0 | binder.install(guiceFilterModule(pattern)); |
111 | 0 | } |
112 | |
|
113 | |
public static ServletModule guiceFilterModule() { |
114 | 0 | return guiceFilterModule("/*"); |
115 | |
} |
116 | |
|
117 | |
public static ServletModule guiceFilterModule(final String pattern) { |
118 | 0 | return new ServletModule() { |
119 | |
@Override |
120 | |
protected void configureServlets() { |
121 | 0 | filter(pattern).through(GuiceShiroFilter.class); |
122 | 0 | } |
123 | |
}; |
124 | |
} |
125 | |
|
126 | |
@Override |
127 | |
protected final void configureShiro() { |
128 | 10 | bindBeanType(TypeLiteral.get(ServletContext.class), Key.get(ServletContext.class, Names.named(NAME))); |
129 | 10 | bind(Key.get(ServletContext.class, Names.named(NAME))).toInstance(this.servletContext); |
130 | 10 | bindWebSecurityManager(bind(WebSecurityManager.class)); |
131 | 10 | bindWebEnvironment(bind(WebEnvironment.class)); |
132 | 10 | bind(GuiceShiroFilter.class).asEagerSingleton(); |
133 | 10 | expose(GuiceShiroFilter.class); |
134 | |
|
135 | 10 | this.configureShiroWeb(); |
136 | |
|
137 | 10 | setupFilterChainConfigs(); |
138 | |
|
139 | 10 | bind(FilterChainResolver.class).toProvider(new FilterChainResolverProvider(filterChains)); |
140 | 10 | } |
141 | |
|
142 | |
private void setupFilterChainConfigs() { |
143 | 10 | Map<Key<? extends PathMatchingFilter>, Map<String, String>> configs = new HashMap<Key<? extends PathMatchingFilter>, Map<String, String>>(); |
144 | |
|
145 | 10 | for (Map.Entry<String, Key<? extends Filter>[]> filterChain : filterChains.entrySet()) { |
146 | 16 | for (int i = 0; i < filterChain.getValue().length; i++) { |
147 | 8 | Key<? extends Filter> key = filterChain.getValue()[i]; |
148 | 8 | if (key instanceof FilterConfigKey) { |
149 | 4 | FilterConfigKey<? extends PathMatchingFilter> configKey = (FilterConfigKey<? extends PathMatchingFilter>) key; |
150 | 4 | key = configKey.getKey(); |
151 | 4 | filterChain.getValue()[i] = key; |
152 | 4 | if (!PathMatchingFilter.class.isAssignableFrom(key.getTypeLiteral().getRawType())) { |
153 | 0 | throw new ConfigurationException("Config information requires a PathMatchingFilter - can't apply to " + key.getTypeLiteral().getRawType()); |
154 | |
} |
155 | 4 | if (configs.get(castToPathMatching(key)) == null) configs.put(castToPathMatching(key), new HashMap<String, String>()); |
156 | 4 | configs.get(castToPathMatching(key)).put(filterChain.getKey(), configKey.getConfigValue()); |
157 | 4 | } else if (PathMatchingFilter.class.isAssignableFrom(key.getTypeLiteral().getRawType())) { |
158 | 4 | if (configs.get(castToPathMatching(key)) == null) configs.put(castToPathMatching(key), new HashMap<String, String>()); |
159 | 4 | configs.get(castToPathMatching(key)).put(filterChain.getKey(), ""); |
160 | |
} |
161 | |
} |
162 | 8 | } |
163 | 10 | for (Key<? extends PathMatchingFilter> filterKey : configs.keySet()) { |
164 | 8 | bindPathMatchingFilter(filterKey, configs.get(filterKey)); |
165 | 8 | } |
166 | 10 | } |
167 | |
|
168 | |
private <T extends PathMatchingFilter> void bindPathMatchingFilter(Key<T> filterKey, Map<String, String> configs) { |
169 | 8 | bind(filterKey).toProvider(new PathMatchingFilterProvider<T>(filterKey, configs)).asEagerSingleton(); |
170 | 8 | } |
171 | |
|
172 | |
@SuppressWarnings({"unchecked"}) |
173 | |
private Key<? extends PathMatchingFilter> castToPathMatching(Key<? extends Filter> key) { |
174 | 24 | return (Key<? extends PathMatchingFilter>) key; |
175 | |
} |
176 | |
|
177 | |
protected abstract void configureShiroWeb(); |
178 | |
|
179 | |
@SuppressWarnings({"unchecked"}) |
180 | |
@Override |
181 | |
protected final void bindSecurityManager(AnnotatedBindingBuilder<? super SecurityManager> bind) { |
182 | 10 | bindWebSecurityManager(bind); |
183 | 10 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
protected void bindWebSecurityManager(AnnotatedBindingBuilder<? super WebSecurityManager> bind) { |
193 | |
try { |
194 | 16 | bind.toConstructor(DefaultWebSecurityManager.class.getConstructor(Collection.class)).asEagerSingleton(); |
195 | 0 | } catch (NoSuchMethodException e) { |
196 | 0 | throw new ConfigurationException("This really shouldn't happen. Either something has changed in Shiro, or there's a bug in ShiroModule.", e); |
197 | 16 | } |
198 | 16 | } |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
@Override |
208 | |
protected void bindSessionManager(AnnotatedBindingBuilder<SessionManager> bind) { |
209 | 10 | bind.to(ServletContainerSessionManager.class).asEagerSingleton(); |
210 | 10 | } |
211 | |
|
212 | |
@Override |
213 | |
protected final void bindEnvironment(AnnotatedBindingBuilder<Environment> bind) { |
214 | 10 | bindWebEnvironment(bind); |
215 | 10 | } |
216 | |
|
217 | |
protected void bindWebEnvironment(AnnotatedBindingBuilder<? super WebEnvironment> bind) { |
218 | 16 | bind.to(WebGuiceEnvironment.class).asEagerSingleton(); |
219 | 16 | } |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
@SuppressWarnings({"UnusedDeclaration"}) |
231 | |
protected final void addFilterChain(String pattern, Key<? extends Filter>... keys) { |
232 | 8 | filterChains.put(pattern, keys); |
233 | 8 | } |
234 | |
|
235 | |
protected static <T extends PathMatchingFilter> Key<T> config(Key<T> baseKey, String configValue) { |
236 | 4 | return new FilterConfigKey<T>(baseKey, configValue); |
237 | |
} |
238 | |
|
239 | |
@SuppressWarnings({"UnusedDeclaration"}) |
240 | |
protected static <T extends PathMatchingFilter> Key<T> config(TypeLiteral<T> typeLiteral, String configValue) { |
241 | 0 | return config(Key.get(typeLiteral), configValue); |
242 | |
} |
243 | |
|
244 | |
@SuppressWarnings({"UnusedDeclaration"}) |
245 | |
protected static <T extends PathMatchingFilter> Key<T> config(Class<T> type, String configValue) { |
246 | 0 | return config(Key.get(type), configValue); |
247 | |
} |
248 | |
|
249 | 4 | private static class FilterConfigKey<T extends PathMatchingFilter> extends Key<T> { |
250 | |
private Key<T> key; |
251 | |
private String configValue; |
252 | |
|
253 | |
private FilterConfigKey(Key<T> key, String configValue) { |
254 | 4 | super(); |
255 | 4 | this.key = key; |
256 | 4 | this.configValue = configValue; |
257 | 4 | } |
258 | |
|
259 | |
public Key<T> getKey() { |
260 | 4 | return key; |
261 | |
} |
262 | |
|
263 | |
public String getConfigValue() { |
264 | 4 | return configValue; |
265 | |
} |
266 | |
} |
267 | |
} |