Mr_sswen 2019-06-15 11:25 采纳率: 50%
浏览 1661
已采纳

Submitted credentials for token did not match the expected credentials.

在做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)
登录失败
  • 写回答

1条回答 默认 最新

  • Mr_sswen 2019-06-15 23:55
    关注

    原来是数据库问题,设置的时候设置username为nchar(20),结果输入的用户名没够20个字符长度,在后面补够20个字节长度就好了,话说有这么不智能吗,还是我没找到方法

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 AttributeError: 'EasyDict' object has no attribute 'BACKUP_DB_INFO'
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。
  • ¥15 支付宝h5参数如何实现跳转
  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 CF1927D 求Hack