自学-Shiro的权限管理-12

Shiro的权限管理:

接下来几节是详细对授权来进行说明。

首先我们先来写一个简单的例子,看下这个例子能不能从中受到一点启发好来学习Shiro的授权。

现在我们在来创建一个页面。

即:admin.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
    admin jsp
    <a href="shiro/logout">登出</a>
    <br/><br/>
  </body>
</html>

然后在index.jsp页面在加几个链接,可以跳转到user.jsp 以及admin.jsp页面。

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
    index jsp
    <br/><br/>
    <a href="admin.jsp">admin</a>
    <br/><br/>
    <a href="user.jsp">user</a>
    <br/><br/>
    <a href="shiro/logout">登出</a>
  </body>
</html>

然后呢?我们来进行测试,发现不管你是用哪个用户登录,都是可以进行登录成功,并且可以进行访问user.jsp 以及admin.jsp.



这时,不管你点击admin 或者user链接都可以正确进入到页面。

如果这时候我们在applicationContext.xml中配置一个roles拦截器呢?

Roles:角色过滤器,判断当前用户是否指定角色。

Eg:

 <!-- shiroFilter:web.xml中的name一致。否则报错 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="login.jsp"/>
        <property name="successUrl" value="index.jsp"/>
        <property name="unauthorizedUrl" value="unauthorized.jsp"/>
        <!-- 
        anon:(anonymous) 拦截器表示匿名访问(即不需要录即可访问)
        authc:(authentication)拦截器表示需要身份认证通过后才能访问
         -->
        <property name="filterChainDefinitions">
            <value>
                /login.jsp = anon
                /shiro/login = anon
                /shiro/logout =logout
                
               /admin.jsp=roles[admin]
                /user.jsp=roles[user]
                
                # everything else requires authentication:
                /** = authc
            </value>
        </property>
    </bean>

这时就发现不管你用哪个用户登录,user.jsp 以及admin.jsp页面都是不能访问的,会直接重定向到未授权的页面。



这些就说明:未给admin user指定角色。

下一节我们就来详细学习授权吧。看怎么样才能重定向到正确的页面。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值