shiro快速学习(一)
来源:互联网 发布:云计算iaas paas saas 编辑:程序博客网 时间:2024/05/22 20:27
shiro基本功能:
工作流程图
package scu.zhc.bean;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.IncorrectCredentialsException;import org.apache.shiro.authc.UnknownAccountException;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.config.IniSecurityManagerFactory;import org.apache.shiro.mgt.SecurityManager;import org.apache.shiro.session.Session;import org.apache.shiro.subject.Subject;import org.apache.shiro.util.Factory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * Created by zhc on 2017/10/24 */public class HelloWorld { private static final Logger log = LoggerFactory.getLogger(HelloWorld.class); public static void main(String[] args) { log.info("log 调试"); //1、获取安全管理器 Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); //2、设置安全管理器 SecurityUtils.setSecurityManager(securityManager); //3、设置即将登陆的用户 Subject currentUser = SecurityUtils.getSubject(); //获得shiro中的session Session session = currentUser.getSession(); session.setAttribute("name","朱汉成"); String value =(String) session.getAttribute("name"); if (value !=null){ log.info("shiro已经从session获得value的值 :"+value); }else { log.info("获取失败"); } //验证登陆 if (!currentUser.isAuthenticated()){ UsernamePasswordToken token = new UsernamePasswordToken("root","secret"); token.isRememberMe(); try { currentUser.login(token); log.info("验证成功"); }catch (UnknownAccountException ukae){ log.info("账号问题"); } catch (IncorrectCredentialsException e){ log.info("密码问题"); }catch (AuthenticationException e){ log.info("认真问题"); } } //判断是否有给指定角色 if (currentUser.hasRole("admin")){ log.info("具有admin角色"); }else { log.info("不具有这样的角色"); } //判断是否有权限操作 if (currentUser.isPermitted("eagle5")){ log.info("有此权限操作"); }else { log.info("无此权限"); } }}
相应shiro的配置文件主要是shiro.ini,从shiro官方实例中直接复制而来
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.## =============================================================================# Quickstart INI Realm configuration## For those that might not understand the references in this file, the# definitions are all based on the classic Mel Brooks' film "Spaceballs". ;)# =============================================================================# -----------------------------------------------------------------------------# Users and their assigned roles## Each line conforms to the format defined in the# org.apache.shiro.realm.text.TextConfigurationRealm#setUserDefinitions JavaDoc# -----------------------------------------------------------------------------[users]# user 'root' with password 'secret' and the 'admin' roleroot = secret, admin# user 'guest' with the password 'guest' and the 'guest' roleguest = guest, guest# user 'presidentskroob' with password '12345' ("That's the same combination on# my luggage!!!" ;)), and role 'president'presidentskroob = 12345, president# user 'darkhelmet' with password 'ludicrousspeed' and roles 'darklord' and 'schwartz'darkhelmet = ludicrousspeed, darklord, schwartz# user 'lonestarr' with password 'vespa' and roles 'goodguy' and 'schwartz'lonestarr = vespa, goodguy, schwartz# -----------------------------------------------------------------------------# Roles with assigned permissions# # Each line conforms to the format defined in the# org.apache.shiro.realm.text.TextConfigurationRealm#setRoleDefinitions JavaDoc# -----------------------------------------------------------------------------[roles]# 'admin' role has all permissions, indicated by the wildcard '*'admin = *# The 'schwartz' role can do anything (*) with any lightsaber:schwartz = lightsaber:*# The 'goodguy' role is allowed to 'drive' (action) the winnebago (type) with# license plate 'eagle5' (instance specific id)goodguy = winnebago:drive:eagle5
其中**=**,**,** 分别是账号,密码,角色1,角色2
运行结果:
阅读全文
0 0
- shiro快速学习(一)
- shiro学习(一)
- Shiro学习(一)
- shiro学习(一)
- shiro学习(一)
- shiro学习(一)
- Shiro入门学习一
- Shiro入门学习一
- Shiro学习总结(一)--关于Shiro
- shiro学习摘录(一)
- shiro学习总结(一)
- Shiro学习随笔【一】基础学习篇
- Apache shiro学习笔记+ spring整合shiro (一)
- shiro学习笔记一 shiro基础的了解
- 权限验证框架Shiro学习(一)
- Apache Shiro学习(一)架构介绍
- Shiro个人学习笔记(一)
- Shiro基础学习(一) 权限管理
- 前端模块化,AMD与CMD的区别
- 简单封装okhttp的get和post方法
- Android:前端与原生混合开发尝试
- 克罗内克积
- STM32 IO口的8中配置方式解读
- shiro快速学习(一)
- 《深入理解java虚拟机》学习笔记7——Java虚拟机类生命周期
- struts2之类型转换器的简单使用
- C++信息学奥赛一本通题库1030
- springMVC项目Controller中Invalid mime type错误
- MySQL使用总结
- 【微信小程序】初识地图组件(一)
- c# wpf 无参有参构造函数
- “Hello Word“ 图形用户界面