异常显示好像是realms的问题,我调试的时候securityManager的realms为空,求大神解决​

大大的微笑 发布于 2015/12/04 14:37
阅读 2K+
收藏 0
package com.demo.shiro;


import java.util.HashSet;
import java.util.Set;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.apache.shiro.mgt.SecurityManager;


import tools.ToolConfig;




public class TestShiro {
public static void main(String[] args) {
//获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager
Factory<SecurityManager> factory =new IniSecurityManagerFactory("classpath:shiro.ini");
//得到SecurityManager实例 并绑定给SecurityUtils  
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);

//得到subject及创建用户名/密码身份验证Token(用户身份/凭证)
Subject subject = SecurityUtils.getSubject();
// Md5Hash md5 = new Md5Hash("123");
UsernamePasswordToken token = new UsernamePasswordToken("zhang","123");
//身份验证
try {
subject.login(token);
} catch (AuthenticationException e) {
e.printStackTrace() ;
}
}

}



//下面是自定义的userRealm

package com.demo.shiro;


import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;




 public class userRealm extends AuthorizingRealm{
@Override
public String getName() {
return "userRealm";
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection arg0) {
/* 这里编写授权代码 */
return null;
}


@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken arg0) throws AuthenticationException {
/* 这里编写认证代码 */
String userName="zhang";
Md5Hash md5 = new Md5Hash("123");
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userName,md5.toString(),getName());
return info;
}

}


//ini配置文件

zhang=123

wang=123

#声明一个realm  
userRealm=com.demo.shiro.userRealm
#指定securityManager的realms实现  
securityManager.realms=$userRealm


//这里是问题

log4j:WARN No appenders could be found for logger (org.apache.shiro.io.ResourceUtils).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
org.apache.shiro.authc.AuthenticationException: Authentication failed for token submission [org.apache.shiro.authc.UsernamePasswordToken - zhang, rememberMe=false].  Possible unexpected error? (Typical or expected login exceptions should extend from AuthenticationException).
at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:214)
at org.apache.shiro.mgt.AuthenticatingSecurityManager.authenticate(AuthenticatingSecurityManager.java:106)
at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:270)
at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:256)
at com.demo.shiro.TestShiro.main(TestShiro.java:45)
Caused by: java.lang.IllegalStateException: Configuration error:  No realms have been configured!  One or more realms must be present to execute an authentication attempt.
at org.apache.shiro.authc.pam.ModularRealmAuthenticator.assertRealmsConfigured(ModularRealmAuthenticator.java:161)
at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:264)
at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)
... 4 more


//第二个问题好像是realms的问题,我调试的时候securityManager的realms为空,求大神解决




加载中
返回顶部
顶部