2014-11-21 10 views
7

Sto costruendo la mia sottoclasse AuthorizingRealm e sto avendo difficoltà a collegarlo al mio SecurityManager.Scrittura personalizzata regno Shiro

L'essenza del mio regno:

public class MyRealm extends AuthorizingRealm { 
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { 
     try { 
      // My custom logic here 

     } catch(Throwable t) { 
      System.out.println(t.getMessage()); 
     } 
     SimpleAuthenticationInfo authn = new SimpleAuthenticationInfo(new MyUser(), "somePassword"); 
     return authn; 
    } 

    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { 
     try { 
      // My custom logic here 
     } catch(Throwable t) { 
      System.out.println(t.getMessage()); 
     } 
     return new SimpleAuthorizationInfo(); 
    } 
} 

Poi nel mio 'shiro.ini':

# ======================= 
# Shiro INI configuration 
# ======================= 
[main] 
myRealm = com.me.myapp.security.MyRealm 

Poi nella mia classe di driver/metodo main (che sto usando per la prova) :

public class Driver { 
    public static void main(String[] args) { 
     Driver d = new Driver(); 
     d.test(); 
    } 

    public void test() { 
     Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); 
     SecurityManager securityManager = factory.getInstance(); 
     SecurityUtils.setSecurityManager(securityManager); 

     UsernamePasswordToken token = new UsernamePasswordToken("", ""); 
     token.setRememberMe(true); 

     System.out.println("Shiro props:"); 
     System.out.println(securityManager.getProperties()); 

     Subject currentUser = SecurityUtils.getSubject() 

     try { 
      currentUser.login(token) 

      println "I think this worked!" 
     } catch (UnknownAccountException uae) { 
      println "Exception: ${uae}" 
     } catch (IncorrectCredentialsException ice) { 
      println "Exception: ${ice}" 
     } catch (LockedAccountException lae) { 
      println "Exception: ${lae}" 
     } catch (ExcessiveAttemptsException eae) { 
      println "Exception: ${eae}" 
     } catch (AuthenticationException ae) { 
      println "Exception: ${ae}" 
     } 
    } 
} 

Quando ho eseguito questo ottengo:

Shiro props: 
[class:class org.apache.shiro.mgt.DefaultSecurityManager, cacheManager:null, subjectFactory:[email protected], authorizer:[email protected], realms:[[email protected]], subjectDAO:[email protected], rememberMeManager:null, authenticator:[email protected], sessionManager:[email protected]] 
Exception: org.apache.shiro.authc.AuthenticationException: Authentication failed for token submission [org.apache.shiro.authc.UsernamePasswordToken - , rememberMe=true]. Possible unexpected error? (Typical or expected login exceptions should extend from AuthenticationException). 

Quindi sembra che stia leggendo il mio shiro.ini perché raccoglie il reame corretto, ma MyRealm non fa nulla se non stub fuori utenti fittizi che dovrebbero essere autenticati a prescindere dal nome utente/password forniti. Qualche idea su dove sto andando storto?

risposta

3

Si può dare un'occhiata al codice sorgente del Stormpath Shiro Plugin in github: Plugin here e Sample App utilizzando il plugin here.

Abbiamo implementato il nostro uno AuthorizingRealm (simile a quello che ti serve). Si può essere interessati a dare un'occhiata a:

  1. https://github.com/stormpath/stormpath-shiro/blob/master/core/src/main/java/com/stormpath/shiro/realm/ApplicationRealm.java
  2. https://github.com/stormpath/stormpath-shiro-web-sample/blob/master/src/main/webapp/WEB-INF/shiro.ini

BTW, nei vostri shiro.ini è necessario aggiungere questo: securityManager.realm = $myRealm

0

aggiungere questo alla tua shiro.ini: securityManager.realms = $myRealm
poi nella classe del driver

UsernamePasswordToken token = new UsernamePasswordToken("", "somePassword"); 

invece di un passowrd vuota.

Penso che questo ha funzionato!

+0

Grazie @Luca Rasconi, tuttavia i vostri suggerimenti non cambiare nulla (stesso comportamento che descrivo sopra). Qualche altra idea/idea? Grazie ancora! – smeeb

0

non ho fatto io stesso, ma qui ci sono un paio di cose che si possono provare:

  1. Se non avete bisogno di logica di autorizzazione, in considerazione sottoclasse AuthenticatingRealm invece di AuthorizingRealm

  2. In metodo doGetAuthenticationInfo, prendere in considerazione l'utilizzo di questo codice:

    SimpleAuthenticationInfo authn = new SimpleAuthenticationInfo (token.getPrincipal(), token.getCredentials(), "myRealm");

Problemi correlati