1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.web.env;
20
21 import org.apache.shiro.env.DefaultEnvironment;
22 import org.apache.shiro.mgt.SecurityManager;
23 import org.apache.shiro.web.config.ShiroFilterConfiguration;
24 import org.apache.shiro.web.filter.mgt.FilterChainResolver;
25 import org.apache.shiro.web.mgt.WebSecurityManager;
26
27 import javax.servlet.ServletContext;
28 import java.util.Map;
29
30
31
32
33
34
35 public class DefaultWebEnvironment extends DefaultEnvironment implements MutableWebEnvironment {
36
37 private static final String DEFAULT_FILTER_CHAIN_RESOLVER_NAME = "filterChainResolver";
38 private static final String SHIRO_FILTER_CONFIG_NAME = "shiroFilter";
39
40 private ServletContext servletContext;
41
42 private ShiroFilterConfiguration filterConfiguration;
43
44 public DefaultWebEnvironment() {
45 super();
46 }
47
48 public FilterChainResolver getFilterChainResolver() {
49 return getObject(DEFAULT_FILTER_CHAIN_RESOLVER_NAME, FilterChainResolver.class);
50 }
51
52 public void setFilterChainResolver(FilterChainResolver filterChainResolver) {
53 setObject(DEFAULT_FILTER_CHAIN_RESOLVER_NAME, filterChainResolver);
54 }
55
56 @Override
57 public SecurityManager getSecurityManager() throws IllegalStateException {
58 return getWebSecurityManager();
59 }
60
61 @Override
62 public void setSecurityManager(SecurityManager securityManager) {
63 assertWebSecurityManager(securityManager);
64 super.setSecurityManager(securityManager);
65 }
66
67 public WebSecurityManager getWebSecurityManager() {
68 SecurityManager sm = super.getSecurityManager();
69 assertWebSecurityManager(sm);
70 return (WebSecurityManager) sm;
71 }
72
73 public void setWebSecurityManager(WebSecurityManager wsm) {
74 super.setSecurityManager(wsm);
75 }
76
77 private void assertWebSecurityManager(SecurityManager sm) {
78 if (!(sm instanceof WebSecurityManager)) {
79 String msg = "SecurityManager instance must be a " + WebSecurityManager.class.getName() + " instance.";
80 throw new IllegalStateException(msg);
81 }
82 }
83
84 public ServletContext getServletContext() {
85 return this.servletContext;
86 }
87
88 public void setServletContext(ServletContext servletContext) {
89 this.servletContext = servletContext;
90 }
91
92
93 @Override
94 public void setShiroFilterConfiguration(ShiroFilterConfiguration filterConfiguration) {
95 setObject(SHIRO_FILTER_CONFIG_NAME, filterConfiguration);
96 }
97
98 @Override
99 public ShiroFilterConfiguration getShiroFilterConfiguration() {
100 ShiroFilterConfiguration config = getObject(SHIRO_FILTER_CONFIG_NAME, ShiroFilterConfiguration.class);
101
102 if (config == null) {
103 config = MutableWebEnvironment.super.getShiroFilterConfiguration();
104 setShiroFilterConfiguration(config);
105 }
106 return config;
107 }
108 }