shiro框架的学习

1shiro框架是什么:是一个权限控制的框架
2shiro框架有什么作用:权限管理,管理那些资源是否需要登录才能访问、控制某些资源需要那些权限才能访问
3shiro框架怎样使用:

          1在web.xml配置shiro的Filter,拦截指定的URL(注意只有被shiroFilter拦截到的URL才能被shiro管理)

  <!-- Shiro filter-->
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>
            org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
            <init-param>
      <param-name>targetFilterLifecycle</param-name>
      <param-value>true</param-value>
    </init-param>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 

       2在shiro的配置文件里配置shiroFilter:

<?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:cache="http://www.springframework.org/schema/cache"
    xmlns:task="http://www.springframework.org/schema/task"
    default-lazy-init="true"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <!-- shiro配置begin -->
    <!-- Shiro Filter -->
    <bean id="shiroFilter"
        class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager" />
        <property name="loginUrl" value="/admin/login.jsp" />
        <property name="successUrl" value="/index.jsp" />
        <property name="unauthorizedUrl" value="/error.jsp" />
        <property name="filterChainDefinitions">
            <value>
            /admin/login.jsp = authc
            /admin/* = authc
            /validateCode = anon
            /* =anon
            </value>
        </property>
    </bean>
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="shiroDbRealm" />
    </bean>
    <!-- 項目自定义的Realm -->
    <bean id="shiroDbRealm" class="com.framework.authority.realm.MyRealm" >
        <property name="authorizationCacheName" value="authorization" />
    </bean>

    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager" />
        <property name="arguments" ref="securityManager" />
    </bean>

</beans>

  3自定义Realm:
  

package com.framework.authority.realm;

import javax.security.auth.Subject;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class MyRealm extends AuthorizingRealm  {
    public MyRealm() {
        super();    //To change body of overridden methods use File | Settings | File Templates.
    }
    //验证用户的准确性--验证登录
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
       //获取登录信息
        System.out.println("-------------------验证用户的准确性-----------------------");
        UsernamePasswordToken userToken = (UsernamePasswordToken) authcToken;
        String userName=String.valueOf(userToken.getUsername());
        String password=String.copyValueOf(userToken.getPassword());
        System.out.println("用户名:---->"+userName);
        System.out.println("密码:-------------->"+password);
        userToken.setRememberMe(true);
        if(userName.equals("jeremy")&&password.equals("123")){
            //这个是什么来的???--验证登录信息对象
            SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(userName,password,getName());
            System.out.println("getName:-------------->"+getName());
            return info;
        }
        return null;
    }
    //为用户添加角色和权限---验证权限,
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("----------------验证用户的角色与权限--------------------");
        String userName=principals.asList().get(0).toString();
        if(userName.equals("jeremy")){
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        info.addRole("youke");
        return info;
        }
        return null;

    }

}

4登录测试(登录提交的页面不用交给任何控制器处理,让shiroFilter来调用Realm来处理)

<%@ page language="java" contentType="text/html; charset=utf-8"
    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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="login.jsp"  method="POST">
    userName:<input id="username" name="username"><br>
    password:<input id="password" name="password"><br>
    <input type="submit" id="submit" value="submit">
    </form>
</body>
</html>

shiro框架的运行流程:
request(url)---->shiroFilter是否是shiroURL--是-->FormAuthenticationFilter判断是当前URL的权限----没有权限-->longinURL--登录-->FormAuthenticationFilter(调用executeLogin()方法)---ModularRealmAuthenticator.doAuthenticate()---调用自定义的Realm---->doAuthenticationInfo()---->doAuthorizationInfo()--->????
以上流程纯属个人猜测---》》

时间: 2024-07-30 20:32:32

shiro框架的学习的相关文章

shiro Apache 框架的学习之authentication 和authorization

shiro  作为Apache的开源项目.该框架下的主要模块包含如下: 1,Subject 2,SecurityManager 3,Authenticator 4,Authorizer 5,Realm 6,SessionManager 7,SessionDao 8,CacheManager 9,Cryptography 源码下载链接:http://shiro.apache.org/download.html  详见官网. 如下以版本1.4学习为主. shiro 作为java security 

shiro框架总结

一.概念 shiro是一个安全框架,主要可以帮助我们解决程序开发中认证和授权的问题.基于拦截器做的权限系统,权限控制的粒度有限,为了方便各种各样的常用的权限管理需求的实现,,我们有必要使用比较好的安全框架,早期spring  security 作为一个比较完善的安全框架比较火,但是spring security学习成本比较高,于是就出现了shiro安全框架,学习成本降低了很多,而且基本的功能也比较完善. 二.shiro提供的功能 1.Authentication:身份认证/登陆,验证用户是不是拥

一个入门rpc框架的学习

一个入门rpc框架的学习 参考 huangyong-rpc 轻量级分布式RPC框架 该程序是一个短连接的rpc实现 简介 RPC,即 Remote Procedure Call(远程过程调用),说得通俗一点就是:调用远程计算机上的服务,就像调用本地服务一样. RPC 可基于 HTTP 或 TCP 协议,Web Service 就是基于 HTTP 协议的 RPC, 它具有良好的跨平台性,但其性能却不如基于 TCP 协议的 RPC.会两方面会直接影响 RPC 的性能,一是传输方式,二是序列化. 众所

JAVA框架之Hibernate框架的学习步骤

首先介绍一下Java三大框架的关系 以CRM项目即客户关系管理项目示例 hibernate框架的学习路线: 1.学习框架入门,自己搭建框架,完成增删改查的操作 2.学习一级缓存,事物管理和基本查询 3.学习一对多和多对多的操作等 4.学习基本查询和查询的优化 本人正在学习hibernate框架,今天是第一天,愿意把自己的学习过程分享给大家,希望大家提出宝贵意见,留言给我,共同进步需要什么软件.资源.例程jar包等的,可以留下邮箱,我晚上会给大家发过去的,谢谢

node.js框架StrongLoop学习笔记(一)

node.js框架StrongLoop学习笔记(一) 本人在用node.js做手机后台,查找框架发现StrongLoop挺适合,可是却发现没有中文教程,于是在自己学习时,做一下笔记,以方便其他像我一样的人参考(本人的英语水平非常差,只能一点点试着做,并记录下来,如果大家发现问题,请通知我好更正,谢谢了!).所有操作都是在CentOS7-x64,Node.js 0.12.2下完成的. nodejs框架StrongLoop学习笔记一 安装StrongLoop 创建项目 安装数据库驱动 配置数据库连接

《PHP框架Yii学习》系列技术文章整理收藏

<PHP框架Yii学习>系列技术文章整理收藏 1Yii Framework框架获取分类下面的所有子类方法 2YII模块实现绑定二级域名的方法 3Yii框架官方指南系列43——专题:URL(创建.路由.美化及自定义) 4Yii入门教程之目录结构.入口文件及路由设置 5Yii核心组件AssetManager原理分析 6Yii使用ajax验证显示错误messagebox的解决方法 7Yii框架中memcache用法实例 8Yii中CGridView关联表搜索排序方法实例详解 9yii实现CheckB

Mina框架的学习笔记——Android客户端的实现

Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应用程序提供了非常便利的框架.当前发行的 MINA 版本支持基于 Java NIO 技术的 TCP/UDP 应用程序开发.串口通讯程序(只在最新的预览版中提供),MINA 所支持的功能也在进一步的扩展中.目前正在使用 MINA 的软件包括有:Apache Directory Project.Asyn

关于CSS基础框架的学习

什么是CSS基础框架? CSS 框架是一系列 CSS 文件的集合体,包含了基本的元素重置,页面排版.网格布局.表单样式.通用规则等代码块,用于简化web前端开发的工作,提高工作效率. 为什么需要研究和使用它? 在当前浏览器很多 而且各有差异的情况下 如何统一样式 兼容大部分浏览器是很有必要的 从具象的表现中抽出抽象的模块来重复使用,是减少用户下载.方便团队及个人开发最重要的手段.所以CSS框架就很有必要了 现在有哪些比较著名的框架? 960gs 960 像素的页面宽度似乎成为了一种设计标准,在当

TP框架代码学习 学习记录 3.2.3

文件:think.class.php PHP提供register_shutdown_function()这个函数,能够在脚本终止前回调注册的函数,也就是当 PHP 程序执行完成后执行的函数.register_shutdown_function 执行机制是:PHP把要调用的函数调入内存.当页面所有PHP语句都执行完成时,再调用此 函数.注意,在这个时候从内存中调用,不是从PHP页面中调用,所以上面的例子不能使用相对路径,因为PHP已经当原来的页面不存在了.就没有什么相对路 径可言.注意:regis