• 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(四)


    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398

    根据下载的pdf学习。

    第六章 Realm及相关对象(四)

    1.Subject的代码结构

    Subject是shiro的核心,基本所有身份验证、授权都是通过Subject完成的。

     1 public interface Subject {
     2     //身份信息获取
     3     Object getPrincipal(); //Primary Principal
     4     PrincipalCollection getPrincipals(); // PrincipalCollection
     5 
     6     //身份验证
     7     void login(AuthenticationToken token) throws AuthenticationException;
     8     boolean isAuthenticated();
     9     boolean isRemembered();
    10 
    11     //角色授权认证
    12     boolean hasRole(String roleIdentifier);
    13     boolean[] hasRoles(List<String> roleIdentifiers);
    14     boolean hasAllRoles(Collection<String> roleIdentifiers);
    15     void checkRole(String roleIdentifier) throws AuthorizationException;
    16     void checkRoles(Collection<String> roleIdentifiers) throws           AuthorizationException;
    17     void checkRoles(String... roleIdentifiers) throws AuthorizationException;
    18     
    19     //权限授权验证
    20     boolean isPermitted(String permission);
    21     boolean isPermitted(Permission permission);
    22     boolean[] isPermitted(String... permissions);
    23     boolean[] isPermitted(List<Permission> permissions);
    24     boolean isPermittedAll(String... permissions);
    25     boolean isPermittedAll(Collection<Permission> permissions);
    26     void checkPermission(String permission) throws AuthorizationException;
    27     void checkPermission(Permission permission) throws AuthorizationException;
    28     void checkPermissions(String... permissions) throws AuthorizationException;
    29     void checkPermissions(Collection<Permission> permissions) throws AuthorizationException;
    30 
    31     //会话
    32     Session getSession(); //相当于 getSession(true)
    33     Session getSession(boolean create);
    34 
    35     //退出
    36     void logout();
    37 
    38     ....
    39     //其他略
    40 } 
    View Code

    还有两个是:RunAs和多线程。代码里省掉了,因为当时我不知道是干嘛的。就打了...

    2.Subject的使用

    (1)Subject的创建

    一般不用自己创建,直接通过SecurityUtils.getSubject() 获取即可。

    public static Subject getSubject() {
        Subject subject = ThreadContext.getSubject();// 先判定当前线程是否绑定了Subject
        if (subject == null) {
            subject = (new Subject.Builder()).buildSubject(); //如果没有绑定就通过Builder绑定,并且返回
            ThreadContext.bind(subject);
        }
        return subject;
    }    

    (2)自定义创建Subject

    创建完成后,再自己绑定到当前线程即可。

    new Subject.Builder().principals(身份).authenticated(true/false).buildSubject();

    3.Subject的基本使用

    (1)身份验证(login)
    (2)授权(hasRole*/isPermitted*或 checkRole*/checkPermission*)
    (3)将相应的数据存储到会话(Session)
    (4)切换身份(RunAs)/多线程身份传播
    (5)退出

  • 相关阅读:
    2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 (B,F,L,M)
    Coderfroces 862 C. Mahmoud and Ehab and the xor
    [CQOI 2015] 任务查询系统
    [POI 2014] Couriers
    [POJ 2104] K-th Number
    [模板] 可持久化数组
    [AHOI 2006] 上学路线
    [SCOI2009] 生日礼物
    [BZOJ 3436] 小K的农场
    [USACO2007 Demo] Cow Acrobats
  • 原文地址:https://www.cnblogs.com/lyh421/p/6378793.html
Copyright © 2020-2023  润新知