shiro初步学习
来源:互联网 发布:淘宝卖家评价举报功能 编辑:程序博客网 时间:2024/06/07 00:37
1、用户需要提供用户的
principals (身份)和credentials(证明)给shiro,以用来验证身份。
2、Realm
Shiro从从Realm获取安全数据(如用户、角色、权限),就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用户身份是否合法;也需要从Realm得到用户相应的角色/权限进行验证用户是否能进行操作;可以把Realm看成DataSource,即安全数据源。
3、流程
SecurityManager负责真正的身份验证逻辑;它会委托给Authenticator进行身份验证;
Authenticator才是真正的身份验证者,Shiro API中核心的身份认证入口点,此处可以自定义插入自己的实现;
Authenticator可能会委托给相应的AuthenticationStrategy进行多Realm身份验证,默认ModularRealmAuthenticator会调用AuthenticationStrategy进行多Realm身份验证;
Authenticator会把相应的token传入Realm,从Realm获取身份验证信息,如果没有返回/抛出异常表示身份验证失败了。此处可以配置多个Realm,将按照相应的顺序及策略进行访问。
4、代码
4.1
定义Realm
public class ESStoreRealm extends AuthorizingRealm { private final Map<String, SimpleAccount> accounts = new HashMap<String, SimpleAccount>(); @Override public boolean supports(AuthenticationToken token) { <span style="color: rgb(0, 130, 0); font-family: Monaco, "DejaVu Sans Mono", "Bitstream Vera Sans Mono", Consolas, "Courier New", monospace; background-color: rgb(250, 250, 250);">//判断此Realm是否支持此Token</span> return token instanceof UsernamePasswordToken; } @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { String username = getAvailablePrincipal(principals).toString(); SimpleAccount account = this.accounts.get(username); return account; }<span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px; font-family: Monaco, "DejaVu Sans Mono", "Bitstream Vera Sans Mono", Consolas, "Courier New", monospace; background-color: rgb(250, 250, 250);">//根据Token获取认证信息</span><span style="font-family: Monaco, "DejaVu Sans Mono", "Bitstream Vera Sans Mono", Consolas, "Courier New", monospace; background-color: rgb(250, 250, 250);"> </span> @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken t) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) t; String username = token.getUsername(); Object password = token.getCredentials(); SimpleAccount account = this.doGetAuthenticationInfo(username, password); this.accounts.put(username, account); return account; }<span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px; font-family: Monaco, "DejaVu Sans Mono", "Bitstream Vera Sans Mono", Consolas, "Courier New", monospace; background-color: rgb(250, 250, 250);">//根据Token获取认证信息</span><span style="font-family: Monaco, "DejaVu Sans Mono", "Bitstream Vera Sans Mono", Consolas, "Courier New", monospace; background-color: rgb(250, 250, 250);"> </span>}
4.2
定义SecurityManager 和设置Realm
ESStoreRealm realm = new ESStoreRealm(client); List<Realm> list = new ArrayList<Realm>(); list.add(realm); SecurityManager securityManager = new DefaultSecurityManager(list);
获取Token使用Authenticator验证
UsernamePasswordToken token = SecurityUtil.parseUsernamePasswordToken(request);AuthenticationInfo info = ThreadContext.getSecurityManager().authenticate(token);
5.授权
授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等)。在授权中需了解的几个关键对象:主体(Subject)、资源(Resource)、权限(Permission)、角色(Role)。
Permission
规则:“资源标识符:操作:对象实例ID” 即对哪个资源的哪个实例可以进行什么操作。其默认支持通配符权限字符串,“:”表示资源/操作/实例的分割;“,”表示操作的分割;“*”表示任意资源/操作/实例。
SimpleAccount account = new UserAccount(username, password, this.getName()); //获取subject
account.addRole((String)role);//添加角色
//添加权限
Map<String, Object> map = privilege.sourceAsMap();String permission = (String) map.get(AUTH_TABLE_PRIVILEGE_CONTENT);account.addObjectPermission(new WildcardPermission(permission, true));
6.验证权限
boolean permit = subject.isPermitted(new WildcardPermission(index+":"+type+":search", true));
- shiro初步学习
- shiro框架初步学习
- shiro初步 shiro认证
- shiro初步 shiro授权
- Shiro 学习笔记(2)—— 授权初步
- 初步认识Shiro
- Shiro初步_1
- Shiro初步_2
- Shiro 初步使用
- Shiro入门-授权初步
- 2016.12.9 初步了解shiro
- 二. Shiro 环境初步搭建
- shiro学习
- Shiro学习
- shiro学习
- shiro学习
- shiro学习
- shiro 学习
- Xcode 8如何继续使用插件
- php中使用python生成excel报表
- Android——RecyclerView——使用ItemDragHelper来实现酷炫拖拽效果
- Android Material Design之Toolbar与Palette实践
- 彻底卸载ORACLE数据库
- shiro初步学习
- 建行E商贸通支付开发系列之三(2、密钥生成与交换)
- 【css3】鼠标悬停图片旋转效果
- 关于C51编译器的声明标识符
- OpenCV Tutorial: 影像分割(grabCut)
- 一张关于docker版本的图
- Oracle-视图
- poj 2407Relatives 欧拉函数
- 一些不错的网址