You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shiro.apache.org by "James Moger (JIRA)" <ji...@apache.org> on 2015/01/21 20:39:36 UTC
[jira] [Commented] (SHIRO-493) shiro-guice not working with the
last guice 4 beta
[ https://issues.apache.org/jira/browse/SHIRO-493?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14286139#comment-14286139 ]
James Moger commented on SHIRO-493:
-----------------------------------
I've run into this too. It seems to me that Shiro-Guice mostly works with 4.0-beta5 but it fails on configuring path matching filters.
This line in the error message is the big clue (the other 4 errors cascade from this failure):
bq. 5) T cannot be used as a key; It is not fully specified.
That is triggered by use of the {{static Key<T> config(Key<T>, String)}} method.
Drilling down into Guice 4 and comparing to Guice 3, Google has clamped down on type-mapping in the _Key_ constructor.
{code:title=Key.java|borderStyle=solid}
protected Key() {
this.annotationStrategy = NullAnnotationStrategy.INSTANCE;
this.typeLiteral = MoreTypes.canonicalizeForKey(
(TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()));
this.hashCode = computeHashCode();
this.toStringSupplier = createToStringSupplier();
}
{code}
*MoreTypes.canonicalizeForKey()* is new for Guice 4.0. That leads to this...
{code:title=MoreTypes.java|borderStyle=solid}
public static <T> TypeLiteral<T> canonicalizeForKey(TypeLiteral<T> typeLiteral) {
Type type = typeLiteral.getType();
if (!isFullySpecified(type)) {
Errors errors = new Errors().keyNotFullySpecified(typeLiteral);
throw new ConfigurationException(errors.getMessages());
}
...
}
{code}
Which then leads to this...
{code:title=MoreTypes.java|borderStyle=solid}
private static boolean isFullySpecified(Type type) {
if (type instanceof Class) {
return true;
} else if (type instanceof CompositeType) {
return ((CompositeType) type).isFullySpecified();
} else if (type instanceof TypeVariable){
// CONFIG SHIRO FILTER FAILS HERE
return false;
} else {
return ((CompositeType) canonicalize(type)).isFullySpecified();
}
}
{code}
*Difference:*
In Guice 4 you are no longer able to subclass _Key_ with a generic type +and+ have that be the final class in the graph.
*Solution:*
I ended up forking _ShiroWebModule_ and relevant classes to implement the following strategy: subclass _FilterConfigKey_ for each configurable default filter and implement a lookup in the _config()_ method. (Change inlined below.)
This breaks the ability to configure custom filters, but there are alternatives to that. If there is a better way, I'm happy to hear about it.
{code:title=ShiroWebModule.java|borderStyle=solid}
@SuppressWarnings("unchecked")
protected static <T extends PathMatchingFilter> Key<T> config(Key<T> baseKey, String configValue) {
if (ROLES == baseKey) {
return (Key<T>) new RolesAuthorizationConfigKey(configValue);
} else if (PERMS == baseKey) {
return (Key<T>) new PermissionsAuthorizationConfigKey(configValue);
} else if (REST == baseKey) {
return (Key<T>) new HttpMethodPermissionConfigKey(configValue);
} else if (PORT == baseKey) {
return (Key<T>) new PortConfigKey(configValue);
} else if (SSL == baseKey) {
return (Key<T>) new SslConfigKey(configValue);
}
throw new IllegalArgumentException("You may not configure a custom filter!");
}
private static class RolesAuthorizationConfigKey extends FilterConfigKey<RolesAuthorizationFilter> {
private RolesAuthorizationConfigKey(String configValue) {
super(ROLES, configValue);
}
}
private static class PermissionsAuthorizationConfigKey extends FilterConfigKey<PermissionsAuthorizationFilter> {
private PermissionsAuthorizationConfigKey(String configValue) {
super(PERMS, configValue);
}
}
private static class HttpMethodPermissionConfigKey extends FilterConfigKey<HttpMethodPermissionFilter> {
private HttpMethodPermissionConfigKey(String configValue) {
super(REST, configValue);
}
}
private static class PortConfigKey extends FilterConfigKey<PortFilter> {
private PortConfigKey(String configValue) {
super(PORT, configValue);
}
}
private static class SslConfigKey extends FilterConfigKey<SslFilter> {
private SslConfigKey(String configValue) {
super(SSL, configValue);
}
}
{code}
> shiro-guice not working with the last guice 4 beta
> --------------------------------------------------
>
> Key: SHIRO-493
> URL: https://issues.apache.org/jira/browse/SHIRO-493
> Project: Shiro
> Issue Type: Bug
> Components: Integration: Guice
> Affects Versions: 1.2.3
> Environment: java version "1.7.0_51"
> Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
> Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
> Reporter: Filipe Sousa
> Assignee: Jared Bunting
>
> Switching from guice 3.0 to guice 4 beta is causing some problems.
> public class SecurityModule extends ShiroWebModule {
> public SecurityModule(ServletContext servletContext) {
> super(servletContext);
> }
> @Override
> protected void configureShiroWeb() {
> bind(Realm.class).to(JdbcRealm.class);
> }
> }
> com.google.inject.CreationException: Guice creation errors:
> 1) No implementation for java.util.Set<org.apache.shiro.realm.Realm> was bound.
> at org.apache.shiro.guice.ShiroModule.configure(ShiroModule.java:74) (via modules: pt.egoi.megan.guice.MeganModule -> pt.egoi.megan.security.SecurityModule)
> 1 error
> at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:449) ~[guice-4.0-SNAPSHOT.jar:na]
> at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:155) ~[guice-4.0-SNAPSHOT.jar:na]
> at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:107) ~[guice-4.0-SNAPSHOT.jar:na]
> at com.google.inject.Guice.createInjector(Guice.java:96) ~[guice-4.0-SNAPSHOT.jar:na]
> at com.google.inject.Guice.createInjector(Guice.java:84) ~[guice-4.0-SNAPSHOT.jar:na]
> at pt.egoi.megan.guice.GuiceListener.getInjector(GuiceListener.java:20) ~[classes/:na]
> at com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:47) ~[guice-servlet-4.0-SNAPSHOT.jar:na]
> at pt.egoi.megan.guice.GuiceListener.contextInitialized(GuiceListener.java:31) ~[classes/:na]
> at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:746) ~[jetty-server-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:238) ~[jetty-servlet-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1240) ~[jetty-webapp-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:689) ~[jetty-server-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:482) ~[jetty-webapp-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) [jetty-util-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:39) [jetty-deploy-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:186) [jetty-deploy-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:494) [jetty-deploy-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:141) [jetty-deploy-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:145) [jetty-deploy-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:56) [jetty-deploy-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:615) [jetty-util-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:540) [jetty-util-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.util.Scanner.scan(Scanner.java:403) [jetty-util-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.util.Scanner.doStart(Scanner.java:337) [jetty-util-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) [jetty-util-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.deploy.providers.ScanningAppProvider.doStart(ScanningAppProvider.java:121) [jetty-deploy-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) [jetty-util-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.deploy.DeploymentManager.startAppProvider(DeploymentManager.java:555) [jetty-deploy-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.deploy.DeploymentManager.doStart(DeploymentManager.java:230) [jetty-deploy-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) [jetty-util-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.util.component.AggregateLifeCycle.doStart(AggregateLifeCycle.java:81) [jetty-util-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:58) [jetty-server-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:96) [jetty-server-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.server.Server.doStart(Server.java:281) [jetty-server-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) [jetty-util-7.6.14.v20131031.jar:7.6.14.v20131031]
> at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1274) [jetty-xml-7.6.14.v20131031.jar:7.6.14.v20131031]
> at java.security.AccessController.doPrivileged(Native Method) [na:1.7.0_51]
> at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1197) [jetty-xml-7.6.14.v20131031.jar:7.6.14.v20131031]
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)