博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Shiro 入门
阅读量:6860 次
发布时间:2019-06-26

本文共 5296 字,大约阅读时间需要 17 分钟。

目录

1. 基础

1.1 创建 Maven 项目

Mavenpom.xml文件

4.0.0
com.nwgdk
shiro-test-javase
1.0-SNAPSHOT
org.apache.shiro
shiro-all
1.3.2
log4j
log4j
1.2.17
org.slf4j
slf4j-api
1.6.1
org.slf4j
slf4j-log4j12
1.6.1

1.2 shiro 认证流程

log4j.properties配置文件

log4j.rootLogger=INFO, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m %n# General Apache librarieslog4j.logger.org.apache=WARN# Springlog4j.logger.org.springframework=WARN# Default Shiro logginglog4j.logger.org.apache.shiro=INFO# Disable verbose logginglog4j.logger.org.apache.shiro.util.ThreadContext=WARNlog4j.logger.org.apache.shiro.cache.ehcache.EhCache=WARN

shiro.ini配置文件

# -----------------------------------------------------------------------------# 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
package com.shiro.bean;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.*;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;public class HelloWorld {    private static final Logger log = LoggerFactory.getLogger(HelloWorld.class);    public static void main(String[] args) {        log.info("正在测试输出Log4j...");        // 1. 获取安全管理器        Factory
factory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance(); // 2. 设置安全管理器 SecurityUtils.setSecurityManager(securityManager); // 3. 获取 Subject 对象 Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); session.setAttribute("name", "nwgdk"); String value = (String) session.getAttribute("name"); if (value != null) { log.info("Shiro 已经帮我们获得 session 中的指定值:" + value); } /* 认证登录流程 * false : 代表没有登录 */ if (currentUser.isAuthenticated() == false) { // UsernamePasswordToken : 提供认证信息 UsernamePasswordToken token = new UsernamePasswordToken("root", "secret"); token.setRememberMe(true); try { // 开始登陆 currentUser.login(token); log.info("用户名和密码正确,登录成功!"); } catch (UnknownAccountException e) { log.info("账户不存在!"); } catch (IncorrectCredentialsException e) { log.info("密码错误!"); } catch (LockedAccountException e) { log.info("用户已锁定!"); } catch (AuthenticationException e) { log.info("认证异常!"); } } // 判断当前用户是否拥有指定的角色 if (currentUser.hasRole("admin") == true) { log.info("拥有指定的角色"); } else { log.info("不拥有指定的角色"); } // 判断当前用户是否拥有指定的权限 if (currentUser.isPermitted("winnebago:drive:eagle5") == true) { log.info("用户拥有指定的权限"); } else { log.info("用户不拥有指定的权限"); } }}

转载于:https://www.cnblogs.com/nwgdk/p/9764164.html

你可能感兴趣的文章
图片轮换
查看>>
PHP数据结构练习笔记--栈
查看>>
JSON对象配合jquery.tmpl.min.js插件,手动攒出一个table
查看>>
编译安装QEMU 及卸载
查看>>
关于php-fpm与nginx进程重载的坑
查看>>
P2S、P2P、P2SP之对比
查看>>
笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用...
查看>>
替代变量
查看>>
73. Spring Boot注解(annotation)列表【从零开始学Spring Boot】
查看>>
UNIX环境高级编程——pthread_create的问题
查看>>
接口继承中一个常见问题的思考
查看>>
C#获取软件图标
查看>>
提高代码质量的三要素
查看>>
Android避免进入页面自动弹出软键盘(真正好用)
查看>>
网络编程
查看>>
cocos2d JS-(JavaScript) 函数类型相互转换(字符串、整形、浮点形、布尔值)
查看>>
手把手教你使用腾讯的热修复框架-Tinker
查看>>
《当程序员的那些狗日日子》(三十一)特殊任务
查看>>
9.10---堆箱子问题(CC150)
查看>>
Spark技术内幕:究竟什么是RDD
查看>>