shiro

来源:互联网 发布:非农数据对美元影响 编辑:程序博客网 时间:2024/06/16 03:01

shiro

shiro教程


shiro注解

  • 注解使用
  • shiro注解可在 controller 跟 service 中使用

  • @RequiresAuthentication

    • 判断是否经过认证或者登陆,若没有的话会抛出异常(UnauthenticatedException)
    • 要求当前Subject 已经在当前的session 中被验证通过才能被访问或调用。例如:

      @RequiresAuthenticationpublic void updateAccount(Account userAccount) {...}
    • 上面的例子相当于:↓

      public void updateAccount(Account userAccount) {    if (!SecurityUtils.getSubject().isAuthenticated()) {        throw new AuthorizationException(...);    }    ...}
  • @RequiresGuest

    • 未认证或者叫未登陆,可能在remember me状态下,否则抛出异常UnauthenticatedException
    • 要求当前的Subject 是一个”guest”,也就是说,他们必须是在之前的session中没有被验证或被记住才能被访问或调用。例如:

      @RequiresGuestpublic void signUp(User newUser) {...}
    • 上面的例子相当于:↓

      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(“权限名称”)

    • 格式 @RequiresPermissions(value={“user:create”, “user:delete”}, logical= Logical.OR)
      • 表示当前 Subject 需要权限 user:create 或 user:delete
    • 检查是否有该权限,没有抛出异常AuthorizationException
    • 要求当前的Subject 被允许一个或多个权限,以便执行注解的方法。例如:

      @RequiresPermissions("account:create")public void createAccount(Account account) {...}
    • 上面的例子相当于:↓

      public void createAccount(Account account) {Subject currentUser = SecurityUtils.getSubject();if (!subject.isPermitted("account:create"/*权限名称*/)) {    throw new AuthorizationException(...);}...}
  • @RequiresRoles

    • 检查是否有该角色,没有抛出异常AuthorizationException
    • 要求当前的Subject 拥有所有指定的角色。如果他们没有,则该方法将不会被执行,而且AuthorizationException 异常将会被抛出。例如:

      @RequiresRoles("administrator"/*角色名称*/)public void deleteUser(User user) {...}
    • 上面的例子相当于:↓

      public void deleteUser(User user) {Subject currentUser = SecurityUtils.getSubject();if (!subject.hasRole("administrator")) {    throw new AuthorizationException(...);}...}
  • @RequiresUser

    • 这个刚好跟@RequiresGuest相反,这个必须经过认证,或者从rememberme进行登陆,这个没有RequiresAuthentication严格但类似,否则抛出异常AuthorizationException
    • RequiresUser 注解需要当前的Subject 是一个应用程序用户才能被注解的类/实例/方法访问或调用。一个“应用程序用户”被定义为一个拥有已知身份,或在当前session 中由于通过验证被确认,或者在之前session 中的’RememberMe’服务被记住。例如:

      @RequiresUserpublic void updateAccount(Account account) {...}
    • 上面的例子相当于:↓

      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(...);} ...}

shiro标签

  • shiro标签使用
  • 引入shiro标签   <%@ taglib prefix=”shiro” uri=”http://shiro.apache.org/tags” %>

  • authenticated(用户已经经过身份验证,但不是记住我登录的)

    <shiro:authenticated>    <shiro:principal />已经经过身份验证<br><br></shiro:authenticated>
  • notAuthenticated(用户没有进行身份验证,记住我自动登录的属于没有进行身份验证)

    <shiro:notAuthenticated>
    用户没有进行身份验证,记住我自动登录的属于没有进行身份验证<br><br>
    </shiro:notAuthenticated>
  • guest(用户没有验证时显示相应信息 ,如登录等相关信息)

    <shiro:guest>
    <a href="login.jsp">登录</a><br><br>
    </shiro:guest>
  • hasAnyRoles(当前用户有任意一个角色将会显示body体中的内容)

    <shiro:hasAnyRoles name="admin,user,manager">
    <shiro:principal></shiro:principal>拥有admin/user/manager中的角色<br><br>
    </shiro:hasAnyRoles>
  • hasPermission(当前用户有相应的权限,将显示body体中的信息)

    <shiro:hasPermission name="customer:delete">
    <shiro:principal />拥有customer:delete权限<br><br>
    </shiro:hasPermission>
  • lacksPermission(当前用户没有相应的权限,将显示body体中的信息)

    <shiro:lacksPermission name="customer:delete">
    没有权限customer:delete<br><br>
    </shiro:lacksPermission>
  • lacksRole(当前用户没有相应的角色,将显示body中的信息)

    <shiro:lacksRole name="manager">
    <shiro:principal></shiro:principal>没有manager角色<br><br>
    </shiro:lacksRole>
  • user(用户已经经过认证/记住我登录后 显示相应的信息)

    <shiro:user>
    <a href="logout">登出</a><br><br>
    </shiro:user>
  • hasRole(当前用户是否拥有该角色,有就显示相关信息)

    <shiro:hasRole name="admin">
    <a href="admin.jsp">Admin Page</a><br><br>
    </shiro:hasRole>

shiro内置过滤器

  • anon(匿名) org.apache.shiro.web.filter.authc.AnonymousFilter
  • authc(身份验证) org.apache.shiro.web.filter.authc.FormAuthenticationFilter
  • authcBasic(http基本验证) org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
  • logout(退出) org.apache.shiro.web.filter.authc.LogoutFilter
  • noSessionCreation(不创建session) org.apache.shiro.web.filter.session.NoSessionCreationFilter
  • perms(许可验证) org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
  • port(端口验证) org.apache.shiro.web.filter.authz.PortFilter
  • rest (rest方面) org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
  • roles(权限验证) org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
  • ssl (ssl方面) org.apache.shiro.web.filter.authz.SslFilter
  • user (用户方面) org.apache.shiro.web.filter.authc.UserFilter