目录
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. 获取安全管理器 Factoryfactory = 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("用户不拥有指定的权限"); } }}