【shro】--- Authorization

The core elements of authorization authentication

  • Permissions: the right to manipulate resources
  • Role: a collection of permissions, a role can contain multiple permissions
  • User: In shiro, it represents the user who accesses the system, that is, the Subject

 

Authorize

  • Programmatic authorization

          role-based access control

          Permission based access control

  • Annotated authorization
  • JSP tag authorization

 

According to the explanation of the first two blogs, we can write the authentication process as a tool class for other classes to use.

shiro.util:

public class ShiroUtil {

           // Pass parameters as, configuration file path, username and password

public static Subject login(String configFile,String userName,String password){

// Read the configuration file to initialize the SecurityManager factory

Factory<SecurityManager> factory=new IniSecurityManagerFactory(configFile);

// Get the securityManager instance

SecurityManager securityManager=factory.getInstance();

// Bind the securityManager instance to SecurityUtils

SecurityUtils.setSecurityManager(securityManager);

// get the currently executing user

Subject currentUser=SecurityUtils.getSubject();

// Create token token, username / password

UsernamePasswordToken token=new UsernamePasswordToken(userName, password);

try{

// authentication

currentUser.login(token);

System.out.println(" Authentication succeeded! ");

}catch(AuthenticationException e){

e.printStackTrace ();

System.out.println(" Authentication failed! ");

}

          return currentUser;

}

}

 

Here we take programmatic authorization as an example.

1. Role-based access control:

 

First bind the user and role. For example: there are two roles, role1 and role2.

 

 

Then write a test class for role access:

shiro provides two methods to determine whether a user has the role.

One starts with has and the other starts with check:

 

 

Let's start with Has.

// Assign roles to users, role-based access control

public class RoleTest {

 

// Test 3 character methods

@Test

public void testHasRole(){

// Whether the user has the role

Subject cuSubject=ShiroUtil.login("classpath:shiro_role.ini", "java1234", "123456");

System.out.println(cuSubject.hasRole("role1")?"role1这个角色":"没有role1这个角色");

//该用户是否拥有这些角色

boolean []results=cuSubject.hasRoles(Arrays.asList("role1","role2","role3"));

System.out.println(results[0]?"role1这个角色":"没有role1这个角色");

System.out.println(results[1]?"role2这个角色":"没有role2这个角色");

System.out.println(results[2]?"role3这个角色":"没有role3这个角色");

 

//该用户是否全部拥有这些角色,全部拥有则返回true

System.out.println(cuSubject.hasAllRoles(Arrays.asList("role1","role2"))?"role1,role2这个角色":"没有role1,role2这个角色");

cuSubject.logout();

}

}

 

 

2、基于权限的访问控制

 

先给角色赋予各种权限,然后用户拥有角色,就代表拥有了这些权限。

 

 

判断这个用户是否有某个权限的方法,shiro同样也提供了两种,一个是is开头的,一个是check开头的



 

/**

 * 基于权限的访问控制,给角色赋权限

 * @author ALWZ

 *

 */

public class PermissionTest {

 

@Test

public void testIsPermitted(){

//用户认证

Subject cuSubject=ShiroUtil.login("classpath:shiro_permission.ini", "java1234", "123456");

System.out.println(cuSubject.isPermitted("user:select")?"user:select这个权限":"没有user:select这个权限");

cuSubject.logout();

 

}

}

 

 

对于注解式授权,和JSP标签授权,咱们只讲讲原理,不举例子了。

注解式权限:

@RequiresAuthentication 要求当前 Subject 已经在当前的 session 中被验证通过才能被访问或调用。

@RequiresGuest 要求当前的 Subject 是一个"guest",也就是说,他们必须是在之前的 session 中没有被验证或被记住才

能被访问或调用。

@RequiresPermissions("account:create") 要求当前的 Subject 被允许一个或多个权限,以便执行注解的方法。

@RequiresRoles("administrator") 要求当前的 Subject 拥有所有指定的角色。如果他们没有,则该方法将不会被执行,而

AuthorizationException 异常将会被抛出。

@RequiresUser RequiresUser 注解需要当前的 Subject 是一个应用程序用户才能被注解的类/实例/方法访问或调用。一个

用程序用户被定义为一个拥有已知身份,或在当前 session 中由于通过验证被确认,或者在之前 session 中的'RememberMe'

服务被记住。

 

 

 

JSP标签权限:

先在页面引入:

<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

然后:

Guest 标签:用户没有身份验证时显示相应信息,即游客访问信息;

User 标签:用户已经身份验证/记住我登录后显示相应的信息;

Authenticated 标签:用户已经身份验证通过,即 Subject.login 登录成功,不是记住我登录的。

notAuthenticated 标签:用户没有身份验证通过,即没有调用 Subject.login 进行登录,包括记住我自动登录

的也属于未进行身份验证。

principal 标签 显示用户身份信息,默认调用 Subject.getPrincipal()获取,即 Primary Principal

hasRole 标签 如果当前 Subject 有角色将显示 body 体内容。

lacksRole 标签 如果当前 Subject 没有角色将显示 body 体内容。

hasAnyRoles 标签 如果当前 Subject 有任意一个角色(或的关系)将显示 body 体内容。

hasPermission 标签 如果当前 Subject 有权限将显示 body 体内容。

lacksPermission 标签 如果当前 Subject 没有权限将显示 body 体内容。

比如:

guest标签 

 

〈shiro:guest>  

欢迎游客访问,〈a href="${pageContext.request.contextPath}/login.jsp">登录〈/a>  

〈/shiro:guest>   

 

用户没有身份验证时显示相应信息,即游客访问信息。

 

user标签 

 

〈shiro:user>  

欢迎[〈shiro:principal/>]登录,〈a href="${pageContext.request.contextPath}/logout">退出〈/a>  

〈/shiro:user>   

用户已经身份验证/记住我登录后显示相应的信息。

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325635546&siteId=291194637