1, the new maven project, dependent on the introduction of the core shiro
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.5.1</version> </dependency>
2, in the new resource shiro.ini
# Define user [the Users] # user name = password role. May correspond to a plurality of user roles, such as while drilling green, yellow diamond Chy1 = ABCD, Common chy2 = ABCD, VIP chy3 = ABCD, SVIP
# define roles [Roles] # role = permissions. There may be multiple privilege, separated by commas Common = Watch VIP = Watch, downloads SVIP = *
3, the new entity class User
public class User { private String name; private String password; public User() { } public User(String name, String password) { this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
4, a new toolbar UserShiro
public class UserShiro { / ** * specified SecurityManager, Realm, the User packaging the Subject * @param User User * @return the Subject Shiro authentication, authorization must first User packaging operation Subject, operated by the Subject * / public static the getSubject the Subject (the User User) { // configure SecurityManager. IniSecurityManagerFactory obsolete, instead of using DefaultSecurityManager DefaultSecurityManager securityManager = new new DefaultSecurityManager (); // .ini file manner of Realm IniRealm The realm = new new IniRealm ( "CLASSPATH: shiro.ini" ); securityManager.setRealm (The realm); // specified to use the SecurityManager SecurityUtils.setSecurityManager (securityManager); // get the Subject Object Subject Subject = SecurityUtils.getSubject (); return Subject; } / ** * check whether the user is the specified character, such as student, teacher, guest, common, vip, svip etc. * @param user user * @param role role * @return Boolean whether the user is assigned the role of * / public static Boolean the hasRole (the user user, role String) { the Subject Subject = the getSubject (user); return subject.hasRole (role); } / ** * Get the character corresponding to the user. Baidu network disk such as login to be displayed at the head of the user identity * @param User * @return String corresponding to the user role * / public static String getRole (the User User) { the Subject Subject = the getSubject (User); String [] allRole {= "Common", "VIP", "SVIP" }; // if the user has multiple roles simultaneously, such as while drilling green, yellow diamond, can be placed in the array, return the collection String role = null ; for (String ELE: allRole) { IF (subject.hasRole (ELE)) { Role = ELE; BREAK; } } Return Role; } / ** checks whether the user has a permission can perform an operation, such as downloading files, download speed * @param User User Information * @param the permit permission to check * @return Boolean whether the user It has specified permissions * / public static Boolean isPermitted (the user user, the permit String) { the Subject Subject = the getSubject (user); return subject.isPermitted (the permit); } / ** * check Log * @param user user login information * @ returnboolean login result, matches * / public static boolean Login (the User User) { the Subject Subject = the getSubject (User); // pass token package user information UsernamePasswordToken token = new new UsernamePasswordToken (user.getName (), user.getPassword ()) ; the try { // the token and compare the information in the Realm. The return value of this method is void, if no match is found, an exception is thrown directly subject.login (token); } the catch (of AuthenticationException E1) { // Realm no matching user return to false ; } return to true ; } /** * 登出 */ public static void logout(User user){ Subject subject = getSubject(user); subject.logout(); } }
5, the new test class Test
public class the Test { public static void main (String [] args) { the User User = new new the User ( "chy3", "ABCD" ); IF (UserShiro.login (User)) { System.out.println ( "Login Successful" ); System.out.println ( "your identity is:" + UserShiro.getRole (the user)); } the else { System.out.println ( "user name or password error" ); } } }
6, operation, effects as follows
Successful login
your identity: svip