Permission framework, java permission framework
As mentioned before, permission authentication is actually used to authenticate the logon identity.
Let's talk about shiro authorization this time.
Shiro can be authorized for roles or resources.
Both of them work, but in today's complex systems, of course you can use the latter, if you are a small system or private, the former can be, or even not, I understand
Okay, go to the Code:
First, create an ini, and configure the login information and permissions.
1 # user 2 [users] 3 # eric user nathan's password is 123456, with two permissions: boss and hr 4 eric = 123456, boss, hr 5 merry = 123456, hr 6 7 # permission 8 [roles] 9 # role has create and update permissions for resource users 10 boss = user: create, user: update11 # The role has the create and delete permissions for the resource user 12 hr = user: create, user: delete13 # The role has the create permission for the resource user 14 tl = user: create
The following code first logs in and then authorizes the user to divide the user into roles and resources:
1 @ Test 2 public void testAuthorization () {3 4 // create a SecurityManager Factory 5 factory <SecurityManager> Factory = new IniSecurityManagerFactory ("classpath: shiro/shiro-permission.ini "); 6 // create SecurityManager 7 SecurityManager securityManager = factory. getInstance (); 8 // set SecurityManager to the system runtime environment, and configure SecurityManager to the spring container after spring. Generally, the singleton instance manages 9 SecurityUtils. setSecurityManager (securityManager); 10 // create subject11 Subject subject = SecurityUtils. getSubject (); 12 // Create token 13 UsernamePasswordToken token = new UsernamePasswordToken ("eric", "123456"); 14 // perform authentication 15 try {16 subject. login (token); 17} catch (AuthenticationException e) {18 e. printStackTrace (); 19} 20 System. out. println ("authentication status:" + subject. isAuthenticated (); 21 // after authentication is passed, perform authorization 22 23 // role-based authorization 24 // hasRole incoming role ID 25 boolean ishasRole = subject. hasRole ("hr"); 26 System. out. println ("single role judgment" + ishasRole); 27 // whether hasAllRoles has multiple roles 28 boolean hasAllRoles = subject. hasAllRoles (Arrays. asList ("boss", "hr", "tl"); 29 System. out. println ("multiple role judgments" + hasAllRoles); 30 31 // use the check Method for authorization. If the authorization fails, an exception 32 // subject is thrown. checkRole ("employee"); 33 34 // resource-based authorization 35 // isPermitted incoming permission identifier 36 boolean isPermitted = subject. isPermitted ("user: create: 1"); 37 System. out. println ("single permission judgment" + isPermitted); 38 39 boolean isPermittedAll = subject. isPermittedAll ("user: create: 1", 40 "user: delete"); 41 System. out. println ("multiple permission judgments" + isPermittedAll); 42 43 // use the check Method for authorization. If the authorization fails, an exception 44 subject is thrown. checkPermission ("items: create: 1"); 45 46}
These are simple configurations.
Later I will talk about how to use spring MVC spring and mybatis for dynamic authentication and authorization.