@Bean public FilterRegistrationBean someFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new ShiroFilter()); registration.addUrlPatterns("/admin/*","static/*"); registration.setName("shiro-admin-filter"); registration.setOrder(1); return registration; }
private void initializeShiro(final ShiroConfiguration config, Environment environment) { if (config.isEnabled()) { LOG.debug("Shiro is enabled"); if (config.isDropwizardSessionHandler() && environment.getApplicationContext().getSessionHandler() == null) { LOG.debug("Adding DropWizard SessionHandler to environment."); environment.getApplicationContext().setSessionHandler(new SessionHandler()); } // This line ensure Shiro is configured and its .ini file found in the designated location. // e.g., via the shiroConfigLocations ContextParameter with fall-backs to default locations if that parameter isn't specified. environment.servlets().addServletListeners( new EnvironmentLoaderListener() ); final String filterUrlPattern = config.getSecuredUrlPattern(); LOG.debug("ShiroFilter will check URLs matching '{}'.", filterUrlPattern); environment.servlets().addFilter("shiro-filter", new ShiroFilter()).addMappingForUrlPatterns( EnumSet.allOf(DispatcherType.class), true, filterUrlPattern ); } else { LOG.debug("Shiro is not enabled"); } }
/** * Sets up Shiro on the servlet context lifecycle, and establishes a URL pattern to protect. * * @param config The Shiro configuration. * @param environment The DropWizard environment. */ private void initializeShiro(ShiroConfiguration config, Environment environment) { if (config.isEnabled()) { if (config.isDropwizardSessionHandler()) { environment.servlets().setSessionHandler(new SessionHandler()); } environment.servlets().addServletListeners(new EnvironmentLoaderListener()); String filterUrlPattern = config.getSecuredUrlPattern(); environment.servlets() .addFilter("shiro-filter", new ShiroFilter()) .addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, filterUrlPattern); } } }