编程式授权 基于角色的访问控制(RBAC) baseRole.ini
  [users]
  # 等号右边第一个为密码,后面为角色
  lee1=123, role1
  lee2=456, role1, role2
  角色控制
  RoleTest.java
  import com.lee.shiro.util.ShiroUtils;
  import org.apache.shiro.subject.Subject;
  import org.junit.Test;
  public class RoleTest {
  @Test
  public void HasRoleTest() {
  Subject currentUser = ShiroUtils.login("classpath:shiroRole.ini",
  "lee1", "123");
  System.out.println(currentUser.hasRole("role1"));
  }
  }
  Subject 判断当前用户是否具有某角色的方法
  返回值为布尔类型的
  boolean hasRole(String roleIdentifier) boolean[] hasRoles(List<String> roleIdentifiers) boolean hasAllRoles(Collection<String> roleIdentifiers)
  没有返回值但抛异常的
  void checkRole(String roleIdentifier) throws AuthorizationException void checkRoles(Collection<String> roleIdentifiers) throws AuthorizationException void checkRoles(String... roleIdentifiers) throws AuthorizationException 基于权限的访问控制 basePermisssion.ini
  [users]
  # 等号右边第一个为密码,后面为角色
  lee1=123, role1
  lee2=456, role1, role2
  [roles]
  role1=user:select
  role2=user:add, user:update, user:delete
  权限控制
  PermissionTest.java
  import com.lee.shiro.util.ShiroUtils;
  import org.apache.shiro.subject.Subject;
  import org.junit.Test;
  public class PermissionTest {
  @Test
  public void isPermitted() {
  Subject currentUser = ShiroUtils.login("classpath:basePermission.ini",
  "lee1", "123");
  System.out.println(currentUser.isPermitted("user:select"));
  }
  }
  Subject 判断当前用户是否具有某权限的方法
  返回值为布尔类型的
  boolean isPermitted(Permission permission) boolean[] isPermitted(String... permissions) boolean[] isPermitted(List<Permission> permissions) boolean isPermittedAll(String... permissions) boolean isPermittedAll(Collection<Permission> permissions)
  没有返回值但抛异常的
  void checkPermission(String permission) throws AuthorizationException void checkPermission(Permission permission) throws AuthorizationException void checkPermissions(String... permissions) throws AuthorizationException void checkPermissions(Collection<Permission> permissions) throws AuthorizationException 注解式授权
  基于注解授权
  @RequiresAuthentication
  要求当前 Subject 已经在当前的 session 中被验证通过才能被访问或调用。
  @RequiresAuthentication
  public void updateAccount(Account userAccount) {
  //this method will only be invoked by a
  //Subject that is guaranteed authenticated
  ...
  }
  等同于
  public void updateAccount(Account userAccount) {
  if (!SecurityUtils.getSubject().isAuthenticated()) {
  throw new AuthorizationException(...);
  }
  //Subject is guaranteed authenticated here
  ...
  }
  @RequiresGuest
  要求当前的 Subject 是一个“guest”,也是说,他们必须是在之前的 session 中没有被验证或被记住才能被访问或调用。
  @RequiresGuest
  public void signUp(User newUser) {
  //this method will only be invoked by a
  //Subject that is unknown/anonymous
  ...
  }
  等同于
  public void signUp(User newUser) {
  Subject currentUser = SecurityUtils.getSubject();
  PrincipalCollection principals = currentUser.getPrincipals();
  if (principals != null && !principals.isEmpty()) {
  //known identity - not a guest:
  throw new AuthorizationException(...);
  }
  //Subject is guaranteed to be a 'guest' here
  ...
  }
  @RequiresPermissions(“account:create”)
  要求当前的 Subject 被允许一个或多个权限,以便执行注解方法。
  @RequiresPermissions("account:create")
  public void createAccount(Account account) {
  //this method will only be invoked by a Subject
  //that is permitted to create an account
  ...
  }
  等同于
  public void createAccount(Account account) {
  Subject currentUser = SecurityUtils.getSubject();
  if (!subject.isPermitted("account:create")) {
  throw new AuthorizationException(...);
  }
  //Subject is guaranteed to be permitted here
  ...
  }
  @RequiresRoles(“administrator”)
  要求当前的 Subject 拥有所有指定的角色,如果他们没有,则该方法将不会被执行,而且AuthorizationException异常将会被抛出。
  @RequiresRoles("administrator")
  public void deleteUser(User user) {
  //this method will only be invoked by an administrator
  ...
  }
  等同于
  public void deleteUser(User user) {
  Subject currentUser = SecurityUtils.getSubject();
  if (!subject.hasRole("administrator")) {
  throw new AuthorizationException(...);
  }
  //Subject is guaranteed to be an 'administrator' here
  ...
  }
  @RequiresUser
  注解需要当前的 Subject 是一个应用程序用户才能被注解的类/实例方法访问或调用。一个“应用程序用户”被定义为一个拥有已知身份,或在当前 session 中通过验证确认,或者在之前 session 中的“RememberMe”服务被记住。
  @RequiresUser
  public void updateAccount(Account account) {
  //this method will only be invoked by a 'user'
  //i.e. a Subject with a known identity
  ...
  }
  等同于
  public void updateAccount(Account account) {
  Subject currentUser = SecurityUtils.getSubject();
  PrincipalCollection principals = currentUser.getPrincipals();
  if (principals == null || principals.isEmpty()) {
  //no identity - they're anonymous, not allowed:
  throw new AuthorizationException(...);
  }
  //Subject is guaranteed to have a known identity here
  ...
  }
  JSP 标签授权
  JSP 标签授权需要导入shiro-web.jar,并添加标签:
  <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
  The guest tag
  用户没有身份验证时显示相应信息,即游客访问信息。
  <shiro:guest>
  Hi there!  Please <a href="login.jsp">Login</a> or <a href="signup.jsp">Signup</a> today!
  </shiro:guest>
  The user tag
  用户已经身份验证/记住我登录后显示相应的信息。
  <shiro:user>
  Welcome back John!  Not John? Click <a href="login.jsp">here<a> to login.
  </shiro:user>
  The authenticated tag
  用户已经身份验证通过,即 Subject.login 登录成功,不是记住我登录的。
  <shiro:authenticated>
  <a href="updateAccount.jsp">Update your contact information</a>.
  </shiro:authenticated>
  The notAuthenticated tag
  用户没有身份验证通过,即没有调用 Subject.login 进行登录,包括记住我自动登录的也属于未进行身份验证。
  <shiro:notAuthenticated>
  Please <a href="login.jsp">login</a> in order to update your credit card information.
  </shiro:notAuthenticated>
  The principal tag
  显示用户身份信息,默认调用 Subject.getPrincipal() 获取,即 Primary Principal。
  Hello, <shiro:principal/>, how are you today?
  等同于
  Hello, <%= SecurityUtils.getSubject().getPrincipal().toString() %>, how are you today?
  Principal property
  Hello, <shiro:principal property="firstName"/>, how are you today?
  Hello, <%= SecurityUtils.getSubject().getPrincipal().getFirstName().toString() %>, how are you today?
  Hello, <shiro:principal type="com.foo.User" property="firstName"/>, how are you today?
  Hello, <%= SecurityUtils.getSubject().getPrincipals().oneByType(com.foo.User.class).getFirstName().toString() %>, how are you today?
  The hasRole tag
  如果当前 Subject 有此角色将显示 body 内容。
  <shiro:hasRole name="administrator">
  <a href="admin.jsp">Administer the system</a>
  </shiro:hasRole>
  The lacksRole tag
  如果当前 Subject 没有角色将显示 body 内容。
  <shiro:lacksRole name="administrator">
  Sorry, you are not allowed to administer the system.
  </shiro:lacksRole>
  The hasAnyRole tag
  如果当前 Subject 有任意一个角色(或关系),将显示 body 内容。
  <shiro:hasAnyRoles name="developer, project manager, administrator">
  You are either a developer, project manager, or administrator.
  </shiro:lacksRole>
  The hasPermission tag
  如果当前 Subject 有权限将显示 body 内容。
  <shiro:hasPermission name="user:create">
  <a href="createUser.jsp">Create a new User</a>
  </shiro:hasPermission>
  The lacksPermission tag
  如果当前 Subject 没有权限将显示 body 内容。
  <shiro:lacksPermission name="user:delete">
  Sorry, you are not allowed to delete user accounts.
  </shiro:hasPermission>
  集成 Web
  此处只列举关键代码,若要查看详细代码移步到GitHub
  依赖
  <dependency>
  <groupId>org.apache.shiro</groupId>
  <artifactId>shiro-web</artifactId>
  <version>1.3.2</version>
  </dependency>
  <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  </dependency>
  web.xml
  <!-- 若使用 classpath 则需在此定义,否则将此去掉 -->
  <context-param>
  <param-name>shiroConfigLocations</param-name>
  <param-value>classpath:shiroWeb.ini</param-value>
  </context-param>
  <listener>
  <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
  </listener>
  <filter>
  <filter-name>ShiroFilter</filter-name>
  <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
  <!-- 若配置文件在 /WEB-INF/ 下则在此配置
  <init-param>
  <param-name>configPath</param-name>
  <param-value>/WEB-INF/shiroWeb.ini</param-value>
  </init-param>
  -->
  </filter>
  <filter-mapping>
  <filter-name>ShiroFilter</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  shiroWeb.ini
  # authc、roles 等都代表着一个 Filter,具体意义看 Default Filters