2014-11-21 14 views
7

Tôi đang xây dựng lớp con AuthorizingRealm của riêng mình và đang có thời gian khó khăn để kết nối với số điện thoại SecurityManager của mình.Viết lĩnh vực tùy chỉnh Shiro

Bản chất của vương quốc của tôi:

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

Sau đó, trong 'shiro.ini' của tôi:

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

Sau đó, trong lớp điều khiển của tôi/phương pháp chính (mà tôi đang sử dụng để thử nghiệm) :

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

Khi tôi chạy này, tôi nhận được:

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

Vì vậy, có vẻ như nó đọc shiro.ini của tôi vì nó chọn đúng lĩnh vực, nhưng MyRealm không làm bất cứ điều gì ngoại trừ khai báo người dùng giả nên được xác thực bất kể tên người dùng/mật khẩu được cung cấp. Bất kỳ ý tưởng nào về việc tôi sẽ đi đâu?

Trả lời

3

Bạn có thể có một cái nhìn vào mã nguồn của Stormpath Shiro Plugin trong github: Plugin here và App mẫu bằng cách sử dụng Plugin here.

Chúng tôi đã triển khai một số AuthorizingRealm (tương tự như những gì bạn cần). Bạn có thể quan tâm đến việc xem xét tại địa chỉ:

  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, trong bạn shiro.ini bạn cần phải thêm này: securityManager.realm = $myRealm

0

thêm video này vào shiro.ini của bạn: securityManager.realms = $myRealm
sau đó trong lớp điều khiển của bạn

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

thay vì một passowrd trống.

Tôi nghĩ điều này đã hiệu quả!

+0

Cảm ơn @Luca Rasconi, tuy nhiên đề xuất của bạn không thay đổi bất cứ điều gì (hành vi tương tự như tôi mô tả ở trên). Bất kỳ suy nghĩ/ý tưởng nào khác? Cảm ơn một lần nữa! – smeeb

0

tôi đã không làm điều này bản thân mình, nhưng đây là một vài điều bạn có thể thử:

  1. Nếu bạn không cần logic ủy quyền, xem xét subclassing AuthenticatingRealm thay vì AuthorizingRealm

  2. Trong phương thức doGetAuthenticationInfo, hãy xem xét sử dụng mã này:

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

Các vấn đề liên quan