Thursday, June 19, 2014

Shiro with google juice

package ShiroGuice;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
public @interface Aop {

}


package ShiroGuice;

import org.apache.shiro.SecurityUtils;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;







public class GuiceServletConfig extends GuiceServletContextListener {

@Override
protected Injector getInjector() {
return Guice.createInjector(new ServletModule() {
@Override
protected void configureServlets() {
Injector injector = Guice.createInjector(new MyShiroModule());
   SecurityManager securityManager = injector.getInstance(SecurityManager.class);
   SecurityUtils.setSecurityManager(securityManager);

}
});
}
}

package ShiroGuice;




import org.apache.shiro.config.Ini;
import org.apache.shiro.guice.ShiroModule;
import org.apache.shiro.realm.text.IniRealm;

import com.google.inject.Provides;

 class MyShiroModule extends ShiroModule {
        protected void configureShiro() {
            try {
                bindRealm().toConstructor(IniRealm.class.getConstructor(Ini.class));
            } catch (NoSuchMethodException e) {
                addError(e);
            }
        }

        @Provides
        Ini loadShiroIni() {
            return Ini.fromResourcePath("classpath:shiro.ini");
        }
    }

package ShiroGuice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class SecurityGuard implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("I am intercepted ");
return invocation.proceed();

}
}

No comments:

Post a Comment