shiro初识
shiro 可以做认证、授权、加密、会话管理、与web集成、缓存。
在本文中,主要使用认证和授权这两个功能。
在shiro框架中,有些很重要的概念:
Subject 很多人把它理解为当前用户,这只是subject的概念的一部分。官方文档上是这么说的,Security specific user ‘view’ of an application user. It can be a human being, a third-party process, a server connecting to you application application, or even a cron job. Basically, it is anything or anyone communicating with your application.就是想要与你的应用的通信的任何事务或者任何人。
Principals 一个subject 的标识,例如用户名、身份证
Credentials 通常用来验证一个subject的私密的数据,例如密码
Realms shiro 需要从realm中获取安全数据(用户、角色、权限)来验证用户是否合法。
1.搭建环境(web.xml和spring-shiro.xml)
在web.xml中需要配置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>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>shiroFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在spring-shiro.xml中的配置如下:
<!-- 自定义域realm -->
<bean id="custom_Realm" class="com.test.realm.CustomRealm"></bean>
<!-- 安全管理器 ref对象-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="custom_Realm"/>
</bean>
<!-- shiro filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 安全管理器必须的 -->
<property name="securityManager" ref="securityManager"/>
<!-- 身份认证失败 认证提交的地址 -->
<property name="loginUrl" value="/index.jsp"/>
<!-- 权限认证失败 没有权限认证提交的地址 -->
<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
<!-- Shiro连接约束配置,即过滤链的定义 -->
<property name="filterChainDefinitions">
<value>
<!-- 对静态资源设置匿名访问 -->
/login = anon
<!-- /** = authc 所有url都必须认证通过才可以访问 -->
/admin* = authc
</value>
</property>
</bean>
<!-- Shiro生命周期处理器 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean> 2.编写自定义的Realm类
public class CustomRealm extends AuthorizingRealm {
@Resource
private UserService userService;
private static final Logger logger = LoggerFactory.getLogger(CustomRealm.class);
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
logger.info("======用户授权认证======");
String userName = principals.getPrimaryPrincipal().toString();
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.setRoles(userService.queryRolesByName(userName));
simpleAuthorizationInfo.setStringPermissions(userService.queryPermissonByName(userName));
return simpleAuthorizationInfo;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
logger.info("======用户登陆认证======");
String userName = token.getPrincipal().toString();
User user = userService.findUserByUsername(userName);
//System.out.println(user.getUsername());
if (user!=null) {
AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), "test");
return authenticationInfo;
}
return null;
}
}
CustomRealm 类需要继承AuthorizingRealm类,并重写两个方法。
doGetAuthorizationInfo() 设置subject的授权和认可。
doGetAuthenticationInfo() 对subject进行认证。 3.编写登录方法
@RequestMapping("/login")
public String Login(User user , Model model){
Subject subject = SecurityUtils.getSubject();
Session session = subject.getSession();
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(user.getUsername(),user.getPassword());
try{
subject.login(usernamePasswordToken);
if (subject.hasRole("admin")){
return "admin";
}else if(subject.hasRole("普通用户")){
return "user";
}
}catch(Exception e){
e.printStackTrace();
return "index";
}
return "index";
}
4.进行测试
数据库;

在登录页面,输入不同角色的人,就会跳转到不同的页面。
5.shiro对jsp的支持
在jsp页面需要引入标签库
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %> 在jsp页面,就可以使用shiro的标签。例如:
<shiro:hasRole name="普通用户"> 用户角色
</shiro:hasRole>
<shiro:principal></shiro:principal> 当前用户
<shiro:hasPermission name="user:create"> 用户权限
</shiro:hasPermission>
shiro初识的更多相关文章
- Shiro初识与总结
1.1简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序 ...
- Apache Shiro——初识
Shrio是什么? Shrio是一个用Java开发的安全框架,用来保证系统或系统数据安全的.他可以用在大多数程序上,比如移动应用程序.Web程序或者大型的企业应用程序等. Shrio能干什么? 能用来 ...
- ehcache的使用 Shiro与Ehcache的结合(附:EhcacheUtils)
ehcache 缓存的使用 合理的使用缓存会极大的提高程序的运行效率.切记:缓存请勿滥用. 配置ehcache与Shiro shiro初识请查看该文章 https://blog.csdn.net/py ...
- 第一章 初识shiro
shiro学习教程来自开涛大神的博客:http://jinnianshilongnian.iteye.com/blog/2018936 第一章 初识shiro 简单了解shiro主要记住三张图即可. ...
- Apache Shiro系列一,概述 —— 初识
一.什么是Shiro Apache Shiro是一个强大.灵活.开源的安全框架,它支持用户认证.权限控制.企业会话管理以及加密等. Apache Shiro的第一个也是最重要的一个目标就是易于使用和理 ...
- 【shiro】shiro学习笔记1 - 初识shiro
[TOC] 认证流程 st=>start: Start e=>end: End op1=>operation: 构造SecurityManager环境 op2=>operati ...
- 初识Shiro
Shiro是Apache基金会下的一个开源安全框架,提供了身份验证.授权.密码学和会话管理等功能,Shiro框架不仅直观易用,而且也能提供健壮的安全性,另外一点值得说的是Shiro的前身是一个始于20 ...
- shiro学习总结(一)----初识shiro
本系列内容大多总结自官网和张开涛的<跟我学Shiro> 一.shiro简介 1.1.shiro有什么用? shiro是一个功能强大使用简单的java安全框架,主要提供了五大功能: 1.认证 ...
- Apache Shiro(一)-登录认证和权限管理初识
What is Apache Shiro? Apache Shiro是一个功能强大.灵活的,开源的安全框架.它可以干净利落地处理身份验证.授权.企业会话管理和加密. Apache Shiro的首要目标 ...
随机推荐
- vue day5 分页控件 更新 PagedList.mvc 仿
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- python3+qqBot+图灵机器人实现qq聊天机器人
原理: 通过Python3的qqBot开源库,基于腾讯的smartQQ协议登录个人QQ,实现监控.收集QQ消息,进而通过图灵机器人API接入方式实现自动聊天. 零.前期准备: 1.Python3 2. ...
- mySql 数据库中间件 atlas的使用
MySQL 中间件Atlas 实现读写分离 原创 MySQL 作者:神谕丶 时间:2016-08-05 17:07:51 2410 0 〇 Atlas架构介绍 <span "=&q ...
- 详解在Linux下实现(彩色)进度条程序,并通过makefile进行编译.
彩色进度条的实现与makefile编译: 创建一个process文件,在里面编写实现进度条的代码 1.在编写代码的时候我们首先要区分两个转义字符:\n \r \n:表示换行,换到下一行,并位于起 ...
- 【C++】如何接收函数return返回来的数组元素
转自 https://www.cnblogs.com/Wade-James/p/7965775.html 我们知道return语句作为被调用函数的结束,返回给调用者函数值.一般来说,是返回一个函数值, ...
- 转发 Delphi中线程类TThread 实现多线程编程
Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数Delphi书藉都有说到,但基本上都是对TThread类的几个成员作一简单介绍,再说明一下Execute的实现和Synchr ...
- WindowsDenfender
c:\Program Files\Windows Defender>MpCmdRun.exe -scan -scantype 3 -file "D:\手动更新病毒库" -Di ...
- Nginx的编译安装及选项
编译安装Nginx1.安装常见的工具和库(GCC.PCRE.zlib.OpenSSL) Nginx是一个由C语言编写的,所以需要一个编译工具如GNU的GCC[root@www ~]# yum inst ...
- 正则求解@" (?<=^\[length=)(\d+)(?=\])"
举个例子 [length=1548]这个正则 就是匹配 length的值了(1548)(?<=exp)匹配之后的(?=exp)匹配表达式之前的^是边界,在行首例如 aa[length=1548] ...
- SpringCloudConfig配置中心git库以及refresh刷新
基于git库的Spring Cloud Config配置中心代码demo下载地址:https://gitlab.com/mySpringCloud/config-git SpringBoot版本: 1 ...
