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;
/**
* Simple Quickstart application showing how to use Shiro's API.
* @since 0.9 RC2
*/
public class Quickstart {
private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);
public static void main(String[] args) {
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
//设置单例模式
SecurityUtils.setSecurityManager(securityManager);
/*
以下是核心代码
*/
// 获取当前的对象 subject
Subject currentUser = SecurityUtils.getSubject();
// 通过当前对象拿到 session
Session session = currentUser.getSession();
session.setAttribute("someKey", "aValue");
String value = (String) session.getAttribute("someKey");
if (value.equals("aValue")) {
log.info("Retrieved the correct value! [" + value + "]");
}
// 判断当前的用户是否被认证
if (!currentUser.isAuthenticated()) {
//token令牌 没有获取,随机
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
token.setRememberMe(true);//设置记住功能
try {
currentUser.login(token);//执行登录操作~
} catch (UnknownAccountException uae) {//用户名不存在
log.info("There is no user with username of " + token.getPrincipal());
} catch (IncorrectCredentialsException ice) {//密码错误
log.info("Password for account " + token.getPrincipal() + " was incorrect!");
} catch (LockedAccountException lae) {//
log.info("The account for username " + token.getPrincipal() + " is locked. " +
"Please contact your administrator to unlock it.");
}
// ... catch more exceptions here (maybe custom ones specific to your application?
catch (AuthenticationException ae) {
//unexpected condition? error?
}
}
//say who they are:
//print their identifying principal (in this case, a username):
//获取当前用户的认证码——存取信息
log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
//test a role:检测角色
if (currentUser.hasRole("schwartz")) {
log.info("May the Schwartz be with you!");
} else {
log.info("Hello, mere mortal.");
}
//粗粒度
//test a typed permission (not instance-level):检测权限
if (currentUser.isPermitted("lightsaber:wield")) {
log.info("You may use a lightsaber ring. Use it wisely.");
} else {
log.info("Sorry, lightsaber rings are for schwartz masters only.");
}
//细粒度
//a (very powerful) Instance Level permission:是否拥有更高级的权限
if (currentUser.isPermitted("winnebago:drive:eagle5")) {
log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " +
"Here are the keys - have fun!");
} else {
log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
}
//all done - log out!退出
currentUser.logout();
// 结束
System.exit(0);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Spring Boot学习之Shiro源码
共57个文件
xml:14个
java:10个
class:10个
需积分: 18 3 下载量 95 浏览量
2023-01-27
19:57:51
上传
评论
收藏 60KB ZIP 举报
温馨提示
Spring Boot学习之Shiro源码【学习狂神说,自己手动书写,可以实现正常所需的功能】 Spring Boot学习之Shiro源码【学习狂神说,自己手动书写,可以实现正常所需的功能】 Spring Boot学习之Shiro源码【学习狂神说,自己手动书写,可以实现正常所需的功能】 Spring Boot学习之Shiro源码【学习狂神说,自己手动书写,可以实现正常所需的功能】 Spring Boot学习之Shiro源码【学习狂神说,自己手动书写,可以实现正常所需的功能】 Spring Boot学习之Shiro源码【学习狂神说,自己手动书写,可以实现正常所需的功能】 Spring Boot学习之Shiro源码【学习狂神说,自己手动书写,可以实现正常所需的功能】 Spring Boot学习之Shiro源码【学习狂神说,自己手动书写,可以实现正常所需的功能】
资源推荐
资源详情
资源评论
收起资源包目录
Spring Boot学习之Shiro源码.zip (57个子文件)
spring-08-shiro
shiro-springboot
pom.xml 3KB
src
test
java
com
yang
ShiroSpringbootApplicationTests.java 474B
main
resources
mapper
UserMapper.xml 381B
application.properties 90B
templates
user
update.html 148B
add.html 142B
login.html 485B
index.html 678B
static
application.yml 1KB
java
com
yang
mapper
UserMapper.java 317B
controller
MyController.java 2KB
pojo
User.java 402B
service
UserService.java 200B
UserServiceImpl.java 499B
ShiroSpringbootApplication.java 435B
config
ShiroConfig.java 3KB
UserRealm.java 3KB
target
classes
mapper
UserMapper.xml 381B
application.properties 90B
templates
user
update.html 148B
add.html 142B
login.html 485B
index.html 678B
application.yml 1KB
com
yang
mapper
UserMapper.class 354B
controller
MyController.class 3KB
pojo
User.class 3KB
service
UserService.class 226B
UserServiceImpl.class 794B
ShiroSpringbootApplication.class 750B
config
ShiroConfig.class 2KB
UserRealm.class 3KB
test-classes
com
yang
ShiroSpringbootApplicationTests.class 959B
generated-test-sources
test-annotations
generated-sources
annotations
pom.xml 1KB
.idea
jarRepositories.xml 879B
dataSources
cd3dab60-a917-47ea-bae3-010a2526695b
storage_v2
_src_
schema
performance_schema.kIw0nw.meta 76B
atguigudb.FHY3Vg.zip 2KB
atguigudb.FHY3Vg.meta 12B
sys.zb4BAA.meta 61B
information_schema.FNRwLQ.meta 76B
mysql.osA4Bg.meta 63B
cd3dab60-a917-47ea-bae3-010a2526695b.xml 66KB
dataSources.local.xml 1KB
uiDesigner.xml 9KB
workspace.xml 7KB
misc.xml 608B
dataSources.xml 530B
compiler.xml 832B
.gitignore 190B
encodings.xml 639B
hello-shiro
pom.xml 1KB
src
test
java
main
resources
shiro.ini 1KB
log4j.properties 533B
java
Quickstart.java 4KB
target
classes
Quickstart.class 4KB
shiro.ini 1KB
log4j.properties 533B
generated-sources
annotations
共 57 条
- 1
资源评论
缘友一世
- 粉丝: 1758
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功