首页 > 编程知识 正文

vue shiro,shiro执行流程

时间:2023-05-03 12:15:44 阅读:175414 作者:3453

摘要这里使用Shiro实现用户的登录和注销功能。

前提:现在可以Spring集成到Shiro中。 即使没有底部也会提供源代码。 以下只说明Shiro部分的核心代码。 例如,Mapper、Service类的代码基本上从数据库中读取数据。 此外,提供了源代码,但不做说明。

实现的第一步:登录页面和注销页面。

login.html

! doctypehtmlhtmllang=' en ' headmetacharset=' utf-8 ' title log in/title/headbodyformaction='/user/log in ' method br/密码:输入类型=' password ' name=' password '请记住。 input type=' checkbox ' name=' remember me ' input type=' submit ' value='登录' p注:成功登录的帐户是root和123456/p/foot

%@ page contentType='text/html; charset=utf-8 ' language=' Java ' % htmlheadtitletitle/title/headbodyh 1恭喜$ { session scope.get (' user ' ).uu

步骤3 :实现退出逻辑

步骤四:自定义类继承授权结果,实现其中两种方法,登录只需实现认证方法doGetAuthenticationInfo ()方法即可。

@ componentpublicclassmyrealmextendsauthorizingrealm { @ autowiredprivateuserserviceuserservice; //protectedauthorizationinfodogetauthorizationinfo (principalcollectionprincipalcollection ) {返回空值; //经过认证的protectedauthenticationinfodogetauthenticationinfo (authenticationtoken ) throwsauthenticationexcen ) throwsauthenticationexcen nticationToken是主体传递的凭据,如果使用的是UsernamePasswordToken,则为此类型的usernamepasswordtokentoken=(usernamepasswordtoken ) authen //token.getPrincipal (; 获取相同的用户名//2。 用用户名从数据库中查询用户信息useruser=userservice.selectuserbyusername (username )。 //3 .确定查询的用户信息是否为有效的if(user==null ) (thrownewunknownaccountexception ); }if(0==user.getstate () ) /用户状态为0表示该用户已禁用thrownewdisabledaccountexception )。 //4 .用户凭据simpleauthenticationinfoinfo=newsimpleauthenticationinfo (username,//即使是用户名,也是用户信息user.getPassword ) )

bytes(username),// 为密码加的盐 getName());// 固定写法,是一个名字 return info; }}

注意:这里的用户信息是从数据库查到的,用了Service、Mapper接口等,这属于SSM的知识,这里不做说明。

第五步:在spring的配置文件applicationContext.xml中设置自定义的Realm。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置Shiro的认证和授权的过滤器--> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="login.html"/> <property name="unauthorizedUrl" value="403.html"/> <property name="filterChainDefinitions"> <value> <!--对静态资源不拦截--> <!--anon指匿名访问的过滤器,所有匿名用户都可以访问静态资源,如css等--> /css/*=anon <!--登录页面可以访问--> /login.html = anon <!--登录请求也可以通过--> /user/login = anon <!--表示/testRole请求必须是admin角色才能访问,roles["角色名"]是标准格式--> /testRole = roles["admin"] <!--表示/testRole2请求必须同时具备admin和user角色才能访问--> /testRole2 = roles ["admin","user"] <!--表示/testPerms请求必须具有user:delete权限才能访问,perms["权限名"]是标准格式--> /testPerms = perms["user:delete"] <!--表示/testPerms2请求必须同时具有user:delete权限和user:update权限才能访问--> /testPerms2 = perms["user:delete","user:update"] <!--authc指必须经过认证(登录过之后)才能放弃的请求,/*指的是所有有一个斜杠的请求都要经过认证--> <!--/**表示所有资源和请求都需要认证,/** = authc是标准格式--> <!--/** = authc--> <!--/**=user指的是系统中所有资源和请求需要验证通过或者RememberMe登录的都可以,/**=user是标准格式--> /**=user </value> </property> </bean> <!--创建安全管理器SecurityManager对象--> <bean class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" id="securityManager"> <!--声明域,在域中读取认证和授权的数据--> <property name="realm" ref="myRealm"/> <!--声明rememberMe管理器--> <property name="rememberMeManager" ref="rememberMeManager"/> </bean> <!--配置自定义的Realm--> <bean class="com.demo.shiro.realm.MyRealm" id="myRealm"> <property name="credentialsMatcher" ref="credentialsMatcher"/> </bean> <!--加密管理器对象--> <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher" id="credentialsMatcher"> <!--设置加密的算法:MD5--> <property name="hashAlgorithmName" value="md5"/> <!--设置加密的次数--> <property name="hashIterations" value="1"/> </bean> <!--配置记住我--> <bean class="org.apache.shiro.web.mgt.CookieRememberMeManager" id="rememberMeManager"> <property name="cookie" ref="rememberMeCookie"/> </bean> <bean class="org.apache.shiro.web.servlet.SimpleCookie" id="rememberMeCookie"> <!--声明Cookie对象--> <constructor-arg value="rememberMe"/> <!--设置cookie的失效时间,单位是秒--> <property name="maxAge" value="3600"/> </bean></beans>

注意,数据库里面的密码是使用了Md5Hash加密了的,使用的盐的用户名。可以通过下面的代码来获取加密后的密文。

下面是关于配置的说明:

简化掉Shiro过滤器的配置:

第六步:在web.xml中配置Shiro的过滤器

其实上面这些步骤也是Spring集成Shiro的配置,都属于Shiro的配置。

源码

最好的办法还是看源码直观感受Shiro的登录。

源码地址:GitHub的Demo

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。