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()--->????
以上流程纯属个人猜测---》》
shiro框架的学习的更多相关文章
- Apache Shiro安全(权限框架)学习笔记二
课程目标 通过学习本课程掌握权限管理的设计思想及方法,使用Shiro框架完成权限管理功能开发. 1. 理解基于资源的权限管理方法. 2. 掌握权限管理的数据模型. 3. 掌握不使用shiro开发 ...
- 学习了解Shiro框架
有关Shiro安全框架 实现权限的几种方式 1)通过表来实现 2)shiro框架 3)Spring Security框架 shiro有哪些主要功能 1.授权 访问控制的过程,即确定谁有权访问 2.身份 ...
- shiro框架学习-5-自定义Realm
1. 自定义Realm基础 步骤: 创建一个类 ,继承AuthorizingRealm->AuthenticatingRealm->CachingRealm->Realm 重写授权方 ...
- JAVAEE——BOS物流项目10:权限概述、常见的权限控制方式、apache shiro框架简介、基于shiro框架进行认证操作
1 学习计划 1.演示权限demo 2.权限概述 n 认证 n 授权 3.常见的权限控制方式 n url拦截权限控制 n 方法注解权限控制 4.创建权限数据模型 n 权限表 n 角色表 n 用户表 n ...
- 《shiro框架》
20170929 shiro授权流程学习 shiro-filter执行流程 CacheManager(shiro缓存管理) JEESITE登录流程简单梳理 shiro与springMVC整合 shir ...
- Shiro框架 (原理分析与简单实现)
Shiro框架(原理分析与简单实现) 有兴趣的同学也可以阅读我之前分享的:Java权限管理(授权与认证)CRM权限管理 (PS : 这篇博客里面的实现方式没有使用框架,完全是手写的授权与认证,可以 ...
- Shiro learning - 入门学习 Shiro中的基础知识(1)
Shiro入门学习 一 .什么是Shiro? 看一下官网对于 what is Shiro ? 的解释 Apache Shiro (pronounced “shee-roh”, the Japanese ...
- shiro框架的组成和内部结构(下一篇为spring整合shiro)
1.shiro简介 Apache Shiro是Java的一个安全框架.功能强大,使用简单的Java安全框架,它为开发人员提供一个直观而全面的认证,授权,加密及会话管理的解决方案. 实际上,Shir ...
- shiro框架总结
一.概念 shiro是一个安全框架,主要可以帮助我们解决程序开发中认证和授权的问题.基于拦截器做的权限系统,权限控制的粒度有限,为了方便各种各样的常用的权限管理需求的实现,,我们有必要使用比较好的安全 ...
随机推荐
- C#NetRemoting双向通信
闲来无事想玩玩双向通信,实现类似QQ的互发消息的功能.于是乎开始学习.Net Remoting. .Net Remoting 是由客户端通过Remoting,访问通道以获得服务端对象,再通过代理解析为 ...
- python读写word文档
读: from docx import Document dir_docx = 'F:\Eclipse\workspace\Spider\cnblogs_doc\mytest - 副本.docx' d ...
- Google Analytics10条有用教程(转)
几乎每个网站都会统计自身的浏览状况:日IP.PV.跳出率.转换率.浏览者属性等等.了解这些数据有助于更好地了解浏览者的属性.知道网站在什么地方存在缺陷,为更好地提供服务.提高网站收入都有所帮助. 对于 ...
- 对sssp项目搭建的补充,总错误处理。
总错误处理,是为了在程序运行时代码出错能及时在控制台看出错误信息. 1. springMVC配置文件中: -------- 2.controller包中: 新建类FrameControllerAdvi ...
- error MSB8031
http://go.microsoft.com/fwlink/p/?LinkId=286820 下载
- html5引擎开发 -- 引擎消息中心和有限状态机 - 初步整理 一
一 什么是有限状态机 FSM (finite-state machine),又称有限状态自动机,简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型.他对于逻辑以及 ...
- SQLServer------插入数据时出现IDENTITY_INSERT错误
详细错误信息: 当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'Student' 中的标识列插入显式值. 原因: 表中存在某个字段是自动增长的标识符 解决方法: set IDENT ...
- Java精选笔记_Java编程基础
Java的基本语法 Java代码的基本格式 修饰符 class 类名 { 程序代码 } 一个Java源文件只定义一个类,不同的类使用不同的源文件定义:将每个源文件中单独定义的类都定义成public ...
- Linux buffer/cache异同
buffers与cached 1).异同点 在Linux 操作系统中,当应用程序需要读取文件中的数据时,操作系统先分配一些内存,将数据从磁盘读入到这些内存中,然后再将数据分发给应用程序:当需要往文件中 ...
- VMware 12安装虚拟机Mac OS X 10.10(VMware12安装/共享文件夹)
推荐电脑配置 1:Inter I5及以上 (A卡请自行百度大神解决方案) 必须开启CPU虚拟化:开机进入BIOS--->Intel Virtualization Technology---> ...