实现步骤
查考官网实现步骤http://shiro.apache.org/spring-boot.html
1)创立一个类去继承AuthorizingRealm,目标在于间接实现Realm(实现类)
实现其中的办法
doGetAuthenticationInfo 获取封装认证信息
doGetAuthorizationInfo 获取封装受权信息
2)创Realm 的Bean对象,交给Spring治理
@Bean
public Realm realm() {
return new ShiroRealm();
3)定义过滤规定
@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
//定义过滤链对象
DefaultShiroFilterChainDefinition chainDefinition =
new DefaultShiroFilterChainDefinition();
//设置登录操作不须要认证(其中,anon示意匿名拜访解决)
chainDefinition.addPathDefinition("/user/login/**", "anon");
//设置登出操作,登出当前要跳转到登录页面(其中,logout示意零碎登出解决)
chainDefinition.addPathDefinition("/user/logout", "logout");
//设置哪些资源须要认证才可拜访
//这里示意对这个url(例如/user/**),要调用过滤链中的哪个过滤器(例如authc)进行解决
//chainDefinition.addPathDefinition("/user/**", "authc");
//chainDefinition.addPathDefinition("/**", "authc"); chainDefinition.addPathDefinition("/**", "user");
return chainDefinition;
}
4)spring的配置
shiro:loginUrl:/login.html
###登录认证流程

1) 零碎调用 subject 的 login 办法将用户信息提交给 SecurityManager
2) SecurityManager 将认证操作委托给认证器对象 Authenticator
3) Authenticator 将用户输出的身份信息传递给 Realm。
4) Realm 拜访数据库获取用户信息而后对信息进行封装并返回。
5) Authenticator 对 realm 返回的信息进行身份认证
1.获取以后用户
Subject currentUser = SecurityUtils.getSubject();
2.判断用户是否登录
currentUser.isAuthenticate();
3.将用户名明码封装成tonken
UsernamePasswordToken tonken = new UsernamePasswordToken(“username”,“password”);
4.通过捕捉执行currentUser.login(tonken)的异样来验证登录
ConcurrentAccessException:并发拜访异样(多个用户同时登录时抛出)
UnknownAccountException uae:用户名不存在
IncorrectCredentialsException ice:明码谬误
LockedAccountException lae:用户被锁住
ExpiredCredentialsException:凭证过期
ExcessiveAttemptsException:认证次数超过限度
UnsupportedTokenException:应用了不反对的Token
5.currentUser.login(tonken)是调用Realm中的getAuthenticationInfo办法拿到一个AuthenticationInfo对象info,再调用CredentialsMatcher的doCredentialsMatch办法将token(代表了用户输出的那一块)和info(代表了数据库中取出的那一块)进行比对
发表回复