上一次配置好了shiro,如今来看下源码他是怎么过滤的数据库
这是shiro内置的Filter,在上次的匹配/**中使用了authc,当咱们全部请求都会先进性过滤缓存
看FormAuthenticationFilter的源码,找到他的继承PathMatchingFilter,能够找到session
找到他在FormAuthenticationFilter的实现this
也就是当全部的请求过来咱们都会进行判断是否登陆若是没有登陆加密
保存当前请求,而且跳转到登陆,登陆连接是在FormAuthenticationFilter继承类AccessControlFilter中,默认,咱们在ShiroFilterFactoryBean中setLoginUrl了url
而后会在AccessControlFilter中setLoginUrlspa
若是他是登陆请求经过url来进行匹配,而后判断是否登陆已经在session中,若是没有进行登陆就执行登陆3d
这里就用到官网说的subject.login(token);
他的token是在FormAuthenticationFilter中进行request.getParamter("")来获取用户名密码,code
获得token进行登陆,登陆成功跳转到成功页面,orm
他是怎么来执行login的?找了下源码
主要是securityManager.login(this, token);
一步一步找下去
这里先从缓存里面找,找不到就用到咱们自定义的realm的身份认证了,获取了认证信息若是info不为空执行assertCredentialsMatch进行校验
getCredentialsMatcher就是咱们在配置MyRealm的bean的时候设置的验证规则,
最后比较的都是一个SimpleHash,看下第一个tokenHashedCredentials,
首先他会获取咱们在realm身份验证中的getCredentialsSalt
返回一个SimpleHash
hashAlgorithmName是咱们当时配置的加密规则,credentials是须要加密的字段这里从token中获取的(也就是request.getParamter("password"))
hashIterations加密次数,也是在最初配置myRealm的时候定义的
第二个accountCredentials是获取加密后的密码,
简单来讲就是tokenHashedCredentials和accountCredentials进行对比
tokenHashedCredentials是获取request中的password值和token中的salt来进行加密
accountCredentials是咱们保存在数据库中的密码,两个结果进行对比,
因此添加用户在入库前的password咱们要进行加密操做要和tokenHashedCredentials规则是同样的才能够,
public static Object encodePwd(){ String hashAlgorithmName = "MD5"; String credentials = "123456"; int hashIterations = 2; // ByteSource credentialsSalt = ByteSource.Util.bytes("admin8d78869f470951332959580424d4bf4f"); return new SimpleHash(hashAlgorithmName, credentials, null, hashIterations); }
验证成功跳转到成功页面,失败抛出异常,由咱们进行捕获而后进行操做