springboot2+shiro 重写filter接口来调用自定义ream的登录校验方式

时间:2024-03-16 10:05:11

本模式采用了springboot2+shiro 前后端分离的模式来做一个简易的用户权限管理系统

1.maven引用pom.xml

         <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.4.0</version>
        </dependency>

2.shiroConfig.java配置

springboot2+shiro 重写filter接口来调用自定义ream的登录校验方式

这里重写filter类,并作为所有路径检验的规则入口

springboot2+shiro 重写filter接口来调用自定义ream的登录校验方式

这里介绍几个常用的方法

springboot2+shiro 重写filter接口来调用自定义ream的登录校验方式

1:protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) throws Exception {}

获取前端传递的token值(从httpServletRequest中获取),并生成自定义的token对象,用于shiro鉴权的令牌token

springboot2+shiro 重写filter接口来调用自定义ream的登录校验方式

 

2:protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {}

检验请求是否允许通过,false  继续执行onAcessDenied()方法继续下一步校验,true 直接授权完成

springboot2+shiro 重写filter接口来调用自定义ream的登录校验方式

3:protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {}

校验请求已经完成授权,通常用于token是否过期,权限操作是否允许通过

springboot2+shiro 重写filter接口来调用自定义ream的登录校验方式

4:protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {}

登录失败后返回的错误信息,此处采用了json格式,异步信息返回

springboot2+shiro 重写filter接口来调用自定义ream的登录校验方式

5:protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {}

登录检验操作之前的token生成,并完成login操作(token校验),如果true,就执行自定义ream的doGetAuthenticationInfo方法做用户账号密码校验。如果false,则调用onLoginFailure返回登录失败消息。

springboot2+shiro 重写filter接口来调用自定义ream的登录校验方式

6: protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {}

登录校验成功后返回的信息,json异步消息提示

 

7: public boolean isPermitted(String permission) {}

isPermitted 系统方法用来验证当前操作是否有某个操作权限

springboot2+shiro 重写filter接口来调用自定义ream的登录校验方式

可配合自定义realm中的重写isPermitted 使用,获取doGetAuthorizationInfo中的角色列表,权限列表的数据做过滤操作

springboot2+shiro 重写filter接口来调用自定义ream的登录校验方式

自定义realm重写isPermitted方法

springboot2+shiro 重写filter接口来调用自定义ream的登录校验方式