[users]zhang=123wang=123
Package Org.zln.hello;import Org.apache.log4j.logmanager;import org.apache.log4j.logger;import Org.apache.shiro.securityutils;import Org.apache.shiro.authc.usernamepasswordtoken;import Org.apache.shiro.config.inisecuritymanagerfactory;import Org.apache.shiro.mgt.securitymanager;import Org.apache.shiro.subject.subject;import org.apache.shiro.util.factory;/** * Created by Sherry on 16/9/8. */public class HelloWorld {private static Logger Logger = Logmanager.getlogger (Helloworld.class); private static Subject Subject; static {//1, get SecurityManager factory, use Ini Config file to initialize SecurityManager Factory<SecurityManager>factory = new Inisecuritymanagerfactory ("Classpath:shiro.ini"); 2, Get SecurityManager instance and bind to securityutils securitymanager SecurityManager = Factory.getinstance (); Securityutils.setsecuritymanager (SecurityManager); 3. Get Subject and create username/password Authentication Token (i.e. user identity/credentials) Subject = Securityutils.getsubject (); public static Boolean login (String username,string password) {Usernamepasswordtoken token = new Usernamepasswo Rdtoken (Username,password); 4, login, that is, authentication try {subject.login (token); }catch (Exception e) {logger.info ("Authentication failed"); Logger.error (E.getmessage (), E); return false; } return subject.isauthenticated ();//whether successful login} public static void Main (string[] args) {//6, exit s Ubject.logout (); }}
The default use of Relam is Inirelam
Shiro's HelloWorld