001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.shiro.web.env; 020 021import org.apache.shiro.env.DefaultEnvironment; 022import org.apache.shiro.mgt.SecurityManager; 023import org.apache.shiro.web.config.ShiroFilterConfiguration; 024import org.apache.shiro.web.filter.mgt.FilterChainResolver; 025import org.apache.shiro.web.mgt.WebSecurityManager; 026 027import javax.servlet.ServletContext; 028import java.util.Map; 029 030/** 031 * Default {@link WebEnvironment} implementation based on a backing {@link Map} instance. 032 * 033 * @since 1.2 034 */ 035public class DefaultWebEnvironment extends DefaultEnvironment implements MutableWebEnvironment { 036 037 private static final String DEFAULT_FILTER_CHAIN_RESOLVER_NAME = "filterChainResolver"; 038 private static final String SHIRO_FILTER_CONFIG_NAME = "shiroFilter"; 039 040 private ServletContext servletContext; 041 042 private ShiroFilterConfiguration filterConfiguration; 043 044 public DefaultWebEnvironment() { 045 super(); 046 } 047 048 public FilterChainResolver getFilterChainResolver() { 049 return getObject(DEFAULT_FILTER_CHAIN_RESOLVER_NAME, FilterChainResolver.class); 050 } 051 052 public void setFilterChainResolver(FilterChainResolver filterChainResolver) { 053 setObject(DEFAULT_FILTER_CHAIN_RESOLVER_NAME, filterChainResolver); 054 } 055 056 @Override 057 public SecurityManager getSecurityManager() throws IllegalStateException { 058 return getWebSecurityManager(); 059 } 060 061 @Override 062 public void setSecurityManager(SecurityManager securityManager) { 063 assertWebSecurityManager(securityManager); 064 super.setSecurityManager(securityManager); 065 } 066 067 public WebSecurityManager getWebSecurityManager() { 068 SecurityManager sm = super.getSecurityManager(); 069 assertWebSecurityManager(sm); 070 return (WebSecurityManager) sm; 071 } 072 073 public void setWebSecurityManager(WebSecurityManager wsm) { 074 super.setSecurityManager(wsm); 075 } 076 077 private void assertWebSecurityManager(SecurityManager sm) { 078 if (!(sm instanceof WebSecurityManager)) { 079 String msg = "SecurityManager instance must be a " + WebSecurityManager.class.getName() + " instance."; 080 throw new IllegalStateException(msg); 081 } 082 } 083 084 public ServletContext getServletContext() { 085 return this.servletContext; 086 } 087 088 public void setServletContext(ServletContext servletContext) { 089 this.servletContext = servletContext; 090 } 091 092 093 @Override 094 public void setShiroFilterConfiguration(ShiroFilterConfiguration filterConfiguration) { 095 setObject(SHIRO_FILTER_CONFIG_NAME, filterConfiguration); 096 } 097 098 @Override 099 public ShiroFilterConfiguration getShiroFilterConfiguration() { 100 ShiroFilterConfiguration config = getObject(SHIRO_FILTER_CONFIG_NAME, ShiroFilterConfiguration.class); 101 // Use the default configuration if config is null 102 if (config == null) { 103 config = MutableWebEnvironment.super.getShiroFilterConfiguration(); 104 setShiroFilterConfiguration(config); 105 } 106 return config; 107 } 108}