001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.apache.shiro.ee.listeners; 015 016import java.util.Optional; 017import java.util.Set; 018import javax.servlet.ServletContext; 019import javax.servlet.ServletContextEvent; 020import javax.servlet.ServletContextListener; 021 022import static javax.servlet.SessionTrackingMode.COOKIE; 023 024import javax.servlet.annotation.WebListener; 025 026import org.apache.shiro.web.env.EnvironmentLoader; 027import org.apache.shiro.web.env.WebEnvironment; 028 029/** 030 * Automatic, adds ability to disable via system property 031 * Adds ability to have two shiro.ini configuration files that are merged 032 */ 033@WebListener 034public class EnvironmentLoaderListener extends EnvironmentLoader implements ServletContextListener { 035 private static final String SHIRO_EE_DISABLED_PARAM = "org.apache.shiro.ee.disabled"; 036 private static final String SHIRO_EE_REDIRECT_DISABLED_PARAM = "org.apache.shiro.ee.redirect.disabled"; 037 private static final String FORM_RESUBMIT_DISABLED_PARAM = "org.apache.shiro.form-resubmit.disabled"; 038 private static final String FORM_RESUBMIT_SECURE_COOKIES = "org.apache.shiro.form-resubmit.secure-cookies"; 039 private static final String SHIRO_WEB_DISABLE_PRINCIPAL_PARAM = "org.apache.shiro.web.disable-principal"; 040 041 public static boolean isShiroEEDisabled(ServletContext ctx) { 042 return Boolean.TRUE.equals(ctx.getAttribute(SHIRO_EE_DISABLED_PARAM)); 043 } 044 045 public static boolean isShiroEERedirectDisabled(ServletContext ctx) { 046 return Boolean.TRUE.equals(ctx.getAttribute(SHIRO_EE_REDIRECT_DISABLED_PARAM)); 047 } 048 049 public static boolean isFormResubmitDisabled(ServletContext ctx) { 050 return Boolean.TRUE.equals(ctx.getAttribute(FORM_RESUBMIT_DISABLED_PARAM)); 051 } 052 053 public static boolean isFormResubmitSecureCookies(ServletContext ctx) { 054 return Boolean.TRUE.equals(ctx.getAttribute(FORM_RESUBMIT_SECURE_COOKIES)); 055 } 056 057 public static boolean isServletNoPrincipal(ServletContext ctx) { 058 return Boolean.TRUE.equals(ctx.getAttribute(SHIRO_WEB_DISABLE_PRINCIPAL_PARAM)); 059 } 060 061 @Override 062 public void contextInitialized(ServletContextEvent sce) { 063 if (Boolean.parseBoolean(sce.getServletContext().getInitParameter(SHIRO_EE_DISABLED_PARAM))) { 064 sce.getServletContext().setAttribute(SHIRO_EE_DISABLED_PARAM, Boolean.TRUE); 065 } 066 if (Boolean.parseBoolean(sce.getServletContext().getInitParameter(SHIRO_EE_REDIRECT_DISABLED_PARAM))) { 067 sce.getServletContext().setAttribute(SHIRO_EE_REDIRECT_DISABLED_PARAM, Boolean.TRUE); 068 } 069 if (Boolean.parseBoolean(sce.getServletContext().getInitParameter(FORM_RESUBMIT_DISABLED_PARAM))) { 070 sce.getServletContext().setAttribute(FORM_RESUBMIT_DISABLED_PARAM, Boolean.TRUE); 071 } 072 String secureCookiesStr = sce.getServletContext().getInitParameter(FORM_RESUBMIT_SECURE_COOKIES); 073 if (Optional.ofNullable(System.getProperty(FORM_RESUBMIT_SECURE_COOKIES)).map(Boolean::valueOf) 074 .or(() -> Optional.ofNullable(secureCookiesStr).map(Boolean::valueOf)).orElse(true)) { 075 sce.getServletContext().setAttribute(FORM_RESUBMIT_SECURE_COOKIES, Boolean.TRUE); 076 } else { 077 sce.getServletContext().setAttribute(FORM_RESUBMIT_SECURE_COOKIES, Boolean.FALSE); 078 } 079 if (Boolean.parseBoolean(sce.getServletContext().getInitParameter(SHIRO_WEB_DISABLE_PRINCIPAL_PARAM))) { 080 sce.getServletContext().setAttribute(SHIRO_WEB_DISABLE_PRINCIPAL_PARAM, Boolean.TRUE); 081 } 082 if (!isShiroEEDisabled(sce.getServletContext())) { 083 sce.getServletContext().setSessionTrackingModes(Set.of(COOKIE)); 084 initEnvironment(sce.getServletContext()); 085 } 086 } 087 088 @Override 089 public void contextDestroyed(ServletContextEvent sce) { 090 if (!isShiroEEDisabled(sce.getServletContext())) { 091 destroyEnvironment(sce.getServletContext()); 092 } 093 } 094 095 @Override 096 protected Class<? extends WebEnvironment> getDefaultWebEnvironmentClass(ServletContext ctx) { 097 if (isShiroEEDisabled(ctx)) { 098 return super.getDefaultWebEnvironmentClass(ctx); 099 } else { 100 return IniEnvironment.class; 101 } 102 } 103}