jeesite的shiro流程

xiaoxiao2021-02-28  98

      由于项目内部cms的快速开发需要,选型使用jeesite进行实现,于是也好好的对shiro的流程进行了学习,其中由于用户在登录之前就需要选择产品分类,登录后也需要能进行切换。考虑过使用自定义的filter将该值进行注入到httpsession中,但是发现当用户登录成功却换了一个session(logger.info了sessionId发现不是同一个东西),于是使用过下面的方式直接将其与当前登录用户的Subject进行绑定。流程大致总结如下:

1、用户在浏览器端访问应用

2、跳转至LoginController的login方法,forward至sysLogin.jsp(登录页面) 3、用户填写:用户名、密码、验证码、产品(主题切换不包含在表单中),并提交表单。 4、所有请求进入shiro框架的DelegatingFilterProxy代理对象ShiroFilterFactoryBean。       一般流程为配置ShiroFilter,继承自父类AbstractShiroFilter(属性WebSecurityManager 继承自shiro的核心,安全管理器SecurityManager)。 5. ShiroFilterFactoryBean内部主要包含属性(属性值从配置文件中进行获取)如下:     1、securityManager(shiro核心安全管理器)      2、loginUrl(登录地址)      3、successUrl(登录成功地址)     4、Map<String, Filter> filters          1、Shiro的CasFilter,然而该项目不存在sso的情况,所以不进行追踪。          2.1、FormAuthenticationFilter 继承自Filter的FormAuthenticationFilter类型并重写和添加的属性如下:                       validateCode:验证码                       mobileLogin:是否手机登录表示(现在没有进行使用,后续可能会使用)                       projectId:用户在登陆前选择的产品ID          2.2   第一、重写父类的Shiro的FormAuthenticationFilter父类的createToken()方法,                  第二、并返回自定义的 UsernamePasswordToken对象(里面包含产品ID属性)继承自shiro的UsernamePasswordToken对象                  第三、Shiro的FormAuthenticationFilter extends AuthenticatingFilter                                而自定义的FormAuthenticationFilter extends shiro的FormAuthenticationFilter extends AuthenticatingFilter     5、Map<String, String> filterChainDefinitionMap                   说明:对于以下filter的拦截URL配置如下(都为Shiro内部的Filter):

         1) 都可以进行访问的静态资源文件:

                     /static/** = anon

                     /userfiles/** = anon

          2)CasFilter拦截的url :

                     ${adminPath}/cas = cas

          3)authcFilter:

                     ${adminPath}/login = authc

          4)logoutFilter:

                     ${adminPath}/logout = logout

          5)userFilter :

                     ${adminPath}/** = user

                      /act/editor/** = user                      /ReportServer/** = user     6、SecurityManager的实现DefaultWebSecurityManager,包含以下对象,从配置中进行获取              1)SystemAuthorizingRealm extends AuthorizingRealm并从写父类的方法将数据源realm进行注入,              2)经filterChain的关键filter FormAuthenticationFilter 将包含产品ID的UsernamePasswordToken对象进行传入              3)SystemAuthorizingRealm使用使用重写父类的doGetAuthenticationInfo方法,传递的参数SimpleAuthenticationInfo中包含其内部类Principal,                      Principal包含用户登录前选择的产品信息,以进行用户名密码验证(若登录过则从UserUtils缓存中进行获取返回对象)                      并将projectId注入SecurityUtils的当前用户Subject中

             4)用户登录成功后的切换产品ID也直接拿到当前的用户Subject并进行替换即可。

转载请注明原文地址: https://www.6miu.com/read-50950.html

最新回复(0)