zoukankan      html  css  js  c++  java
  • 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)退出

  • 相关阅读:
    UVa 1349 (二分图最小权完美匹配) Optimal Bus Route Design
    UVa 1658 (拆点法 最小费用流) Admiral
    UVa 11082 (网络流建模) Matrix Decompressing
    UVa 753 (二分图最大匹配) A Plug for UNIX
    UVa 1451 (数形结合 单调栈) Average
    UVa 1471 (LIS变形) Defense Lines
    UVa 11572 (滑动窗口) Unique Snowflakes
    UVa 1606 (极角排序) Amphiphilic Carbon Molecules
    UVa 11054 Wine trading in Gergovia
    UVa 140 (枚举排列) Bandwidth
  • 原文地址:https://www.cnblogs.com/lyh421/p/6378793.html
Copyright © 2011-2022 走看看