security - 编写自定义Shiro领域

标签 security authentication authorization shiro

我正在构建自己的 AuthorizingRealm 子类,但很难将其连接到我的 SecurityManager

我的境界的本质:

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

然后在我的“shiro.ini”中:

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

然后在我的驱动程序类/主方法(我用于测试)中:

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

当我运行这个时,我得到:

Shiro props: 
[class:class org.apache.shiro.mgt.DefaultSecurityManager, cacheManager:null, subjectFactory:org.apache.shiro.mgt.DefaultSubjectFactory@6a2b8b42, authorizer:org.apache.shiro.authz.ModularRealmAuthorizer@50c3d082, realms:[com.me.myapp.security.MyRealm@67ae303a], subjectDAO:org.apache.shiro.mgt.DefaultSubjectDAO@5ce06503, rememberMeManager:null, authenticator:org.apache.shiro.authc.pam.ModularRealmAuthenticator@1007d798, sessionManager:org.apache.shiro.session.mgt.DefaultSessionManager@72db4460] 
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). 

所以它看起来像是在读取我的 shiro.ini,因为它选择了正确的领域,但是 MyRealm 除了删除应该进行身份验证的虚拟用户之外不执行任何操作,无论提供的用户名/密码如何。关于我哪里出了问题有什么想法吗?

最佳答案

将其添加到您的 shiro.ini:securityManager.realms = $myRealm
然后在您的驱动程序类中

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

而不是空密码。

我认为这有效!

关于security - 编写自定义Shiro领域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27063599/

相关文章:

php - symfony2 调用 is_granted in voter : how to avoid an infinite loop?

database - postgresql super 用户没有密码提示

python - 使用 Django webapp 登录 Azure

Android - Paypal 支付 "sendAuthorizationToServer"

iphone - iPhone App数据安全

php - 如何保护用户注册?

java - java中的RSA签名和验证

node.js - req.user 在 Sean.js 中未定义

ASP.NET 自定义角色提供程序 - 附加字段

authorization - 在 Varnish 中取消设置授权 header ,但也将 header 发送到后端