You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by jasmine <sp...@gmail.com> on 2016/03/03 12:03:04 UTC
Re: Shiro + Guice
Hi Jared, sorry for posting so late, I've been busy.
Thanks for your reply. I got it working, but I don't know if this is a good
way to :) Can you have a look at my code to check if I'm wrong someway?
Thanks a lot!!
web.xml:
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>mypackage.shiro.GuiceServletInjector</listener-class>
</listener>
GuiceServletInjector.java
public class GuiceServletInjector extends GuiceServletContextListener {
private ServletContext servletContext;
@Override
public void contextInitialized(ServletContextEvent servletContextEvent)
{
servletContext = servletContextEvent.getServletContext();
super.contextInitialized(servletContextEvent);
}
@Override
protected Injector getInjector() {
Injector injector = Guice.createInjector(
new MyShiroModule(servletContext),
new ShiroAopModule(),
new GuiceServletModule(),
MyShiroModule.guiceFilterModule()
);
return injector;
}
}
MyShiroModule.java
public class MyShiroModule extends ShiroWebModule {
private static Logger log =
LoggerFactory.getLogger(MyShiroModule.class);
@Inject
public MyShiroModule(ServletContext servletContext) {
super(servletContext);
}
@Provides
@Singleton
Ini loadShiroIni() {
return Ini.fromResourcePath("classpath:shiro.ini");
}
@Override
protected void configureShiroWeb() {
try {
bindRealm().to(MYREALM.class);
} catch (Exception e) {
log.error("Errore configurazione MyShiroModule");
throw e;
}
}
}
GuiceServletModule.java
public class GuiceServletModule extends ServletModule {
@Override
protected void configureServlets() {
super.configureServlets();
serve("/*").with(MyUi.MyUIServlet.class);
bind(MyUi.MyUIServlet.class);
bind(org.apache.shiro.realm.ldap.JndiLdapContextFactory.class).toProvider(JndiLdapContextGuiceProvider.class).in(Singleton.class);
bind(MyReaknììlm.class).toProvider(MyRealmGuiceProvider.class).in(Singleton.class);
Properties properties = new Properties();
try {
InputStream stream
=
getClass().getClassLoader().getResourceAsStream("shiro.ini");
properties.load(stream);
Names.bindProperties(binder(), properties);
} catch (IOException e) {
// log.error("File di propertier non trovato");
}
}
@Provides
private Class<? extends UI> provideUIClass() {
return MyUI.class;
}
}
PRGuiceProvider.java
public class PRGuiceProvider implements Provider<MyRealm> {
@Inject
private Provider<JndiLdapContextFactory> ldapContextFactoryProvider;
@Inject
private Provider<IniRealmRolePermissionResolver>
iniRealmRolePermissionResolverProvider;
@Inject
@Named("searchBase")
private String searchBase;
@Inject
@Named("groupRolesMap")
private String groupRolesMapString;
@Override
public MyRealm get() {
MyRealm realm = new MyRealm ();
JndiLdapContextFactory factory = ldapContextFactoryProvider.get();
realm.setLdapContextFactory(factory);
realm.setSearchBase(searchBase);
Map<String, String> groupRolesMap = new HashMap<String, String>();
String[] maps = groupRolesMapString.split("\\|");
for (String mappingString : maps) {
String[] mapping = mappingString.split(":");
groupRolesMap.put(mapping[0].replaceAll("\\\"", ""),
mapping[1].replaceAll("\\\"", ""));
}
realm.setGroupRolesMap(groupRolesMap);
IniRealmRolePermissionResolver permissionResolver =
iniRealmRolePermissionResolverProvider.get();
permissionResolver.setIni(new IniRealm("classpath:shiro.ini"));
realm.setRolePermissionResolver(permissionResolver);
return realm;
}
}
JndiLdapContextGuiceProvider .class
public class JndiLdapContextGuiceProvider implements
Provider<org.apache.shiro.realm.ldap.JndiLdapContextFactory> {
@Inject @Named("realmUrl") String url;
@Inject @Named("systemUserName") String username;
@Inject @Named("systemPassword") String password;
@Override
public JndiLdapContextFactory get() {
JndiLdapContextFactory factory = new JndiLdapContextFactory();
factory.setUrl(url);
factory.setSystemUsername(username);
factory.setSystemPassword(password);
return factory;
}
}
--
View this message in context: http://shiro-user.582556.n2.nabble.com/Shiro-Guice-tp7580965p7580973.html
Sent from the Shiro User mailing list archive at Nabble.com.