Shiro安全框架——【快速入门、登录拦截、用户认证、请求授权】

}

//…在此处捕获更多异常

catch (AuthenticationException ae) {

//意外情况 ? 错误 ?

}

}

//打印其标识主体(在这种情况下,为用户名)

log.info(“User [” + currentUser.getPrincipal() + “] logged in successfully.”);

//测试角色是否存在

if (currentUser.hasRole(“schwartz”)) {

log.info(“May the Schwartz be with you!”);

} else {

log.info(“Hello, mere mortal.”);

}

//粗粒度,极限范围小

//测试类型化的极限(不是实例级别)

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.”);

}

//细粒度,极限范围广

//实例级别的权限(非常强大)

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!”);

}

//注销

currentUser.logout();

//退出

System.exit(0);

}

}

三、SpringBoot 集成 Shiro

=====================

1.编写测试环境


1.在刚刚的父项目中新建一个 springboot 模块

2.导入 SpringBoot 和 Shiro 整合包的依赖

org.apache.shiro

shiro-spring

1.4.1

下面是编写配置文件

Shiro 三大要素

Subject        用户      ->    ShiroFilterFactoryBean

SecurityManager        管理所有用户  -> DefaultWebSecurityManager

Realm          连接数据

实际操作中对象创建的顺序 : realm -> securityManager -> subject

3.编写自定义的 realm ,需要继承 AuthorizingRealm

//自定义的 UserRealm extends AuthorizingRealm

public class UserRealm extends AuthorizingRealm {

//授权

@Override

protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

//打印一个提示

System.out.println(“执行了授权方法”);

return null;

}

//认证

@Override

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

//打印一个提示

System.out.println(“执行了认证方法”);

return null;

}

}

4.新建一个ShiroConfig配置文件

@Configuration

public class ShiroConfig {

//ShiroFilterFactoryBean:3

@Bean

public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier(“securityManager”) DefaultWebSecurityManager defaultWebsecurityManager){

ShiroFilterFactoryBean bean = new

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值