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()--->????
以上流程纯属个人猜测---》》

shiro框架的学习的更多相关文章

  1. Apache Shiro安全(权限框架)学习笔记二

    课程目标 通过学习本课程掌握权限管理的设计思想及方法,使用Shiro框架完成权限管理功能开发. 1.  理解基于资源的权限管理方法. 2.  掌握权限管理的数据模型. 3.  掌握不使用shiro开发 ...

  2. 学习了解Shiro框架

    有关Shiro安全框架 实现权限的几种方式 1)通过表来实现 2)shiro框架 3)Spring Security框架 shiro有哪些主要功能 1.授权 访问控制的过程,即确定谁有权访问 2.身份 ...

  3. shiro框架学习-5-自定义Realm

    1. 自定义Realm基础 步骤: 创建一个类 ,继承AuthorizingRealm->AuthenticatingRealm->CachingRealm->Realm 重写授权方 ...

  4. JAVAEE——BOS物流项目10:权限概述、常见的权限控制方式、apache shiro框架简介、基于shiro框架进行认证操作

    1 学习计划 1.演示权限demo 2.权限概述 n 认证 n 授权 3.常见的权限控制方式 n url拦截权限控制 n 方法注解权限控制 4.创建权限数据模型 n 权限表 n 角色表 n 用户表 n ...

  5. 《shiro框架》

    20170929 shiro授权流程学习 shiro-filter执行流程 CacheManager(shiro缓存管理) JEESITE登录流程简单梳理 shiro与springMVC整合 shir ...

  6. Shiro框架 (原理分析与简单实现)

    Shiro框架(原理分析与简单实现) 有兴趣的同学也可以阅读我之前分享的:Java权限管理(授权与认证)CRM权限管理   (PS : 这篇博客里面的实现方式没有使用框架,完全是手写的授权与认证,可以 ...

  7. Shiro learning - 入门学习 Shiro中的基础知识(1)

    Shiro入门学习 一 .什么是Shiro? 看一下官网对于 what is Shiro ? 的解释 Apache Shiro (pronounced “shee-roh”, the Japanese ...

  8. shiro框架的组成和内部结构(下一篇为spring整合shiro)

    1.shiro简介 Apache Shiro是Java的一个安全框架.功能强大,使用简单的Java安全框架,它为开发人员提供一个直观而全面的认证,授权,加密及会话管理的解决方案. ​ 实际上,Shir ...

  9. shiro框架总结

    一.概念 shiro是一个安全框架,主要可以帮助我们解决程序开发中认证和授权的问题.基于拦截器做的权限系统,权限控制的粒度有限,为了方便各种各样的常用的权限管理需求的实现,,我们有必要使用比较好的安全 ...

随机推荐

  1. 第三百零一节,python操作redis缓存-管道、发布订阅

    python操作redis缓存-管道.发布订阅 一.管道 redis-py默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,如果想要在一次请求中指定多个命令,则可以使用pi ...

  2. VMware下Ubuntu与宿主Windows共享文件夹 (转至 http://blog.csdn.net/zz962/article/details/7706755)

    概述 1.安装VMware Tool 2.设置共享 步骤 开始安装VMware Tool 显示如下画面(如果宿主无法访问外网,可能会出现一个更新失败,可以无视之) 通过下列命令解压.执行,分别是下面的 ...

  3. 未能在当前目标框架中解析主引用“System.Net.Http”,它是一个框架程序集。“.NETFramework,Version=v4.0”。若要解决此问题,请移除引用“System.Net.Http”,或将应用程序的目标重新指向包含“System.Net.Http”的框架版本。 Zephyr.Web

    解决方法:升级项目到.net framework 4.5

  4. Unity5.5+easytouch5双摇杆控制角色移动

    第一步:新建两个Joystick,分别改名LeftJoyStick和RightJoyStick 在LeftJoyStick的ETC Joystick-Axes properties中的Horizont ...

  5. Git 的BUG小结

    Git 的BUG小结 Git 在push的时候出现了: fatal: The remote end hung up unexpectedly 在网上找了非常多  发现出现了下面错误提示也可能是同样的问 ...

  6. mybatis由浅入深day01_ 7输入映射(7.1传递pojo的包装对象_7.2#{}与${}_7.3传递简单类型_7.4传递pojo对象_7.5传递hashmap)

    7 输入映射 通过parameterType指定输入参数的类型,类型可以是简单类型.hashmap.pojo的包装类型. 7.1 传递pojo的包装对象 7.1.1 需求 完成用户信息的综合查询,需要 ...

  7. RMQ LAC 入门

    RMQ RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大) ...

  8. python常用内置模块,执行系统命令的模块

    Subprocess模块 python3.5将使用Subprocess模块跟操作系统进行交互,比如系统命令,他将替换 os.system os.spawn* subprocess.run()方法封装的 ...

  9. PHP创建文件以及移动文件

    创建文件,这里用到的是fopen,即是打开,又是创建 <?php $counter_file = 'aa.txt ';//文件名及路径,在当前目录下新建aa.txt文件 $fopen = fop ...

  10. 定义了一个UIImageView如何使加载的图片不会失真 UIImageView的Frame值是固定的

    定义了一个UIImageView如何使加载的图片不会失真  UIImageView的Frame值是固定的 UIViewContentModeScaleToFill, 缩放内容到合适比例大小 UIVie ...