在做shiro+sqlserver时,碰到的不知道什么问题,百度说是密码不对,可是我也试过md5加密,也没能解决
java方法
package skt;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.util.Factory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
public class ShiroSqlTest {
public static void main(String[] args) {
Factory<SecurityManager> factory=
new IniSecurityManagerFactory("classpath:shiro-sql.ini");
SecurityManager securityManager=factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject subject=SecurityUtils.getSubject();
UsernamePasswordToken token=new UsernamePasswordToken("admin@shiro.com","admin");
try{
subject.login(token);
System.out.println("登录成功");
if(subject.hasRole("admin")){
System.out.println("有admin角色");
}
else{
System.out.println("没有admin角色");
}
if(subject.isPermitted("search")){
System.out.println("有search权限");
}
else{
System.out.println("没有search权限");
}
if(subject.isPermittedAll("del","update")){
System.out.println("有del,update权限");
}
else{
System.out.println("没有del,update权限");
}
}catch(AuthenticationException e){
e.printStackTrace();
System.out.println("登录失败");
}
}
}
ini文件配置
[main]
dataSource=org.springframework.jdbc.datasource.DriverManagerDataSource
dataSource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
dataSource.url=jdbc:sqlserver://localhost:1433;DatabaseName=Rol
dataSource.username=sa
dataSource.password=************
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.permissionsLookupEnabled=true
jdbcRealm.dataSource=$dataSource
jdbcRealm.authenticationQuery=select Password from Users where Username=?
jdbcRealm.userRolesQuery=select Userrole from Role where Username=?
jdbcRealm.permissionsQuery=select Permname from Permission where Rolename=?
securityManager.realms=$jdbcRealm
报错信息
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
六月 15, 2019 11:15:06 上午 org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
信息: Loaded JDBC driver: com.microsoft.sqlserver.jdbc.SQLServerDriver
org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - admin@shiro.com, rememberMe=false] did not match the expected credentials.
at org.apache.shiro.realm.AuthenticatingRealm.assertCredentialsMatch(AuthenticatingRealm.java:600)
at org.apache.shiro.realm.AuthenticatingRealm.getAuthenticationInfo(AuthenticatingRealm.java:578)
at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doSingleRealmAuthentication(ModularRealmAuthenticator.java:180)
at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:267)
at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)
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 skt.ShiroSqlTest.main(ShiroSqlTest.java:22)
登录失败