2014-11-21 13 views
7

Tworzę własną podklasę AuthorizingRealm i mam trudny czas na podłączenie jej do mojego SecurityManager.Pisanie niestandardowej domeny Shiro

Istotą mojego królestwa:

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(); 
    } 
} 

Wtedy w moim 'shiro.ini':

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

Wtedy w mojej klasie/STEROWNIKA metoda main (które używam do testowania) :

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}" 
     } 
    } 
} 

Kiedy uruchamiam to uzyskać:

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). 

Wygląda więc na to, że odczytałem mój plik shiro.ini, ponieważ podniósł on poprawną krainę, ale MyRealm nie robi nic poza odcięciem fikcyjnych użytkowników, którzy powinni zostać uwierzytelnieni, niezależnie od podanej nazwy użytkownika/hasła. Jakieś pomysły co do tego, gdzie idę źle?

Odpowiedz

3

Można przyjrzeć kodu źródłowego Stormpath Shiro Plugin w github: Plugin here i Próbka App za pomocą wtyczki here.

Wdrożyliśmy nasz jeden AuthorizingRealm (podobny do tego, czego potrzebujesz). Możesz być zainteresowany w przyjrzeniu:

  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, w swoim shiro.ini trzeba dodać to: securityManager.realm = $myRealm

0

dodać do swojej shiro.ini: securityManager.realms = $myRealm
następnie w klasie sterowników

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

zamiast pustego passowrd.

Myślę, że to zadziałało!

+0

Thanks @Luca Rasconi, jednakże swoimi sugestiami niczego nie zmieniaj (takie samo zachowanie jak opisuję powyżej). Jakieś inne myśli/pomysły? Dzięki jeszcze raz! – smeeb

0

Nie zrobiłem tego sam, ale tutaj jest kilka rzeczy, które można spróbować:

  1. Jeśli nie potrzebują logiki autoryzacji, należy rozważyć instacji AuthenticatingRealm zamiast AuthorizingRealm

  2. W Metoda doGetAuthenticationInfo, należy rozważyć użycie tego kodu:

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

Powiązane problemy