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' role
root = secret, admin
# user 'guest' with the password 'guest' and the 'guest' role
guest = 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
运行结果: