在jsp中引用Shiro(二)

首先,推荐一套完整的Shiro Demo,免费的。
Shiro Demo:http://www.sojson.com/shiro 

在页面上使用shiro标签的时候,会自动触发MyShiroRealm类中的用户授权方法doGetAuthorizationInfo,即在加载页面的时候,如果发现页面上有使用shiro标签(不是引入标签库的时候),那么,会返回回去先执行doGetAuthorizationInfo方法,执行完,再返回页面。

导入标签库


  1. <%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>  

 

guest标签 


  1. <shiro:guest>  
  2. 欢迎游客访问,<a href="${pageContext.request.contextPath}/login.jsp">登录</a>  
  3. </shiro:guest>   

 

用户没有身份验证时显示相应信息,即游客访问信息。

 

user标签 


  1. <shiro:user>  
  2. 欢迎[<shiro:principal/>]登录,<a href="${pageContext.request.contextPath}/logout">退出</a>  
  3. </shiro:user>   

用户已经身份验证/记住我登录后显示相应的信息。

  

authenticated标签 


  1. <shiro:authenticated>  
  2.     用户[<shiro:principal/>]已身份验证通过  
  3. </shiro:authenticated>   

用户已经身份验证通过,即Subject.login登录成功,不是记住我登录的。    


流程分析:      http://blog.youkuaiyun.com/uk8692/article/details/51098895

<shiro:principal property="username" />这种写法,是要把一个带有username属性的对象转换为Prinipal后保存在session中,才能在页面上正确显示结果的。由于开始学习,所以我用的是ini配置文件作为安全数据源的。在登录的方法中,调用了subject.login(token)后,还要手动利用principal和realmName构造SimpleAuthenticationInfo对象,其实这里的pricipal是一个Object,就是我们的带有username属性的实体对象,然后将SimpleAuthenticationInfo对象存放在session中。 
代码如下:

try {
            subject.login(token);
   //获取realmSecurityManager对象,其包含了很多信息,比如配置文件里面的数据
            RealmSecurityManager realmSecurityManager = (RealmSecurityManager) securityManager;
            Collection<Realm> collection = realmSecurityManager.getRealms();

            if (collection!=null && collection.size()>0){
                Iterator iterator = collection.iterator();
                while (iterator.hasNext()){
                    Realm realm = (Realm)iterator.next();
             //得到默认的数据源名称,虽然默认的为iniRealm,也可以通过程序获得
                    String realmName = realm.getName();
                    //自定义的实体对象
                    User user = new User();
                    user.setUsername(username);
                    user.setPassword(password);
                    //得到SimpleAuthenticationInfo对象
                    SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user,password,realmName);
                    //通过源码分析在调用subject.login(token)后,会通过SubjectContext来保存到session,所以就直接复用了源码(DefaultSecurityManager类中)
                    SubjectContext subjectContext = new DefaultSubjectContext();
                    subjectContext.setAuthenticated(true);
                    subjectContext.setAuthenticationToken(token);
                    subjectContext.setAuthenticationInfo(info);
                    if (subject != null) {
                        subjectContext.setSubject(subject);
                    }
                   //此方法中进行保存
                   realmSecurityManager.createSubject(subjectContext);
                }
            }

        }catch (UnknownAccountException e){
            error = "用户名不存在";
        }catch (IncorrectCredentialsException e){
            error = "用户名或密码错误";
        }catch (AuthenticationException e){
            error = "其他错误"+e.getMessage();
        }

最后结果是在页面上标签<shiro:principal property="username" /> 能正确显示结果,说明此方法可行。


 

notAuthenticated标签

<shiro:notAuthenticated>    未身份验证(包括记住我)</shiro:notAuthenticated> 

用户已经身份验证通过,即没有调用Subject.login进行登录,包括记住我自动登录的也属于未进行身份验证。 

 

principal标签 

<shiro: principal/>

显示用户身份信息,默认调用Subject.getPrincipal()获取,即Primary Principal。

 

  1. <shiro:principal type="java.lang.String"/>  

相当于Subject.getPrincipals().oneByType(String.class)。 

 

  1. <shiro:principal type="java.lang.String"/>  

相当于Subject.getPrincipals().oneByType(String.class)。

 

  1. <shiro:principal property="username"/>  

相当于((User)Subject.getPrincipals()).getUsername()。   

 

hasRole标签 


  1. <shiro:hasRole name="admin">  
  2.     用户[<shiro:principal/>]拥有角色admin<br/>  
  3. </shiro:hasRole>   

如果当前Subject有角色将显示body体内容。

 

hasAnyRoles标签 


  1. <shiro:hasAnyRoles name="admin,user">  
  2.     用户[<shiro:principal/>]拥有角色admin或user<br/>  
  3. </shiro:hasAnyRoles>   

如果当前Subject有任意一个角色(或的关系)将显示body体内容。 

 

lacksRole标签 


  1. <shiro:lacksRole name="abc">  
  2.     用户[<shiro:principal/>]没有角色abc<br/>  
  3. </shiro:lacksRole>   

如果当前Subject没有角色将显示body体内容。 

  

hasPermission标签

 
  1. <shiro:hasPermission name="user:create">  
  2.     用户[<shiro:principal/>]拥有权限user:create<br/>  
  3. </shiro:hasPermission>   

如果当前Subject有权限将显示body体内容。 

  

lacksPermission标签


  1. <shiro:lacksPermission name="org:create">  
  2.     用户[<shiro:principal/>]没有权限org:create<br/>  
  3. </shiro:lacksPermission>   

如果当前Subject没有权限将显示body体内容。

 

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值