Skip to content

Commit

Permalink
SHIRO-892 Ensure ShiroWebModules works with explicit bindings in with…
Browse files Browse the repository at this point in the history
… Guice

GuiceShiroFilter uses com.google.inject.Inject to conform with other usages
  • Loading branch information
bdemers committed Oct 28, 2022
1 parent 2c1254e commit d6f6959
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.shiro.web.mgt.WebSecurityManager;
import org.apache.shiro.web.servlet.AbstractShiroFilter;

import javax.inject.Inject;
import com.google.inject.Inject;

/**
* Shiro filter that is managed by and receives its filter chain configurations from Guice. The convenience method to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.util.StringUtils;
import org.apache.shiro.web.config.ShiroFilterConfiguration;
import org.apache.shiro.web.env.WebEnvironment;
import org.apache.shiro.web.filter.InvalidRequestFilter;
import org.apache.shiro.web.filter.PathMatchingFilter;
Expand Down Expand Up @@ -135,6 +136,7 @@ protected final void configureShiro() {
bindBeanType(TypeLiteral.get(ServletContext.class), Key.get(ServletContext.class, Names.named(NAME)));
bind(Key.get(ServletContext.class, Names.named(NAME))).toInstance(this.servletContext);
bindWebSecurityManager(bind(WebSecurityManager.class));
bindShiroFilterConfiguration(bind(ShiroFilterConfiguration.class));
bindWebEnvironment(bind(WebEnvironment.class));
bind(GuiceShiroFilter.class).asEagerSingleton();
expose(GuiceShiroFilter.class);
Expand Down Expand Up @@ -246,6 +248,17 @@ protected void bindWebSecurityManager(AnnotatedBindingBuilder<? super WebSecurit
}
}

/**
* Binds the Shiro Filter Configuration. Override this method in order to provide your own Shiro Filter Configuration binding.
* <p/>
* By default, a {@link ShiroFilterConfiguration} is bound as an eager singleton.
*
* @param bind
*/
protected void bindShiroFilterConfiguration(AnnotatedBindingBuilder<? super ShiroFilterConfiguration> bind) {
bind.asEagerSingleton();
}

/**
* Binds the session manager. Override this method in order to provide your own session manager binding.
* <p/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.apache.shiro.web.filter.mgt.FilterChainResolver;
import org.apache.shiro.web.mgt.WebSecurityManager;

import javax.inject.Named;
import com.google.inject.name.Named;
import javax.servlet.ServletContext;

@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
*/
package org.apache.shiro.guice.web;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.name.Named;
import com.google.inject.Provides;
import com.google.inject.binder.AnnotatedBindingBuilder;
import com.google.inject.name.Names;
Expand All @@ -47,7 +49,6 @@
import org.junit.Assume;
import org.junit.Test;

import javax.inject.Named;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
Expand Down Expand Up @@ -432,7 +433,7 @@ public void testChangeInvalidFilterConfig() {
expect(request.getAttribute("javax.servlet.include.servlet_path")).andReturn("/test/foobar");
replay(servletContext, request);

Injector injector = Guice.createInjector(new ShiroWebModule(servletContext) {
Injector injector = Guice.createInjector(Binder::requireExplicitBindings, new ShiroWebModule(servletContext) {
@Override
protected void configureShiroWeb() {

Expand Down

0 comments on commit d6f6959

Please sign in to comment.