shiro simple authentication

 

 

File to complete the authentication using static shiro.ini

Create a project to burst

     <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.4.1</version>
        </dependency>
        <!-- Shiro uses SLF4J for logging.  We'll use the 'simple' binding
             in this example app.  See http://www.slf4j.org for more info. -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.21</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.21</version>
            <scope>test</scope>
        </dependency>

 

The core of shiro and rely log4j

Incidentally, created log4j file

创建 shiro.ini

 

 

import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

public class TestAuthenticationApp {
    //日志输出工具
    private static final transient= Log LoggerFactory.getLogger Logger (TestAuthenticationApp. Class );
     public  static  void main (String [] args) { 
        
         String username = "zhangsan" ; 
         String password = "123456" ; 
        
         log.info ( "My First the Apache Shiro the Application" );
          @ 1 create security manager factory object 
         factory's <the securityManager> factory = new new IniSecurityManagerFactory ( "CLASSPATH: shiro.ini" );
          // 2 factory to create a security manager using 
         the securityManager securityManager = factory.getInstance ();
          // . 3 put the current security manager is bound to thread
         SecurityUtils.setSecurityManager (securityManager);
          // . 4 using SecurityUtils.getSubject () to give the body 
         the Subject the currentUser = SecurityUtils.getSubject ();
         // . 5 package username 
         AuthenticationToken the arg0 = new new UsernamePasswordToken (username, password); 
         currentUser.login (the arg0) ; 
         System.out.println ( "authentication" ); 
    } 
}

 

When a user name or password is incorrect, it will throw appropriate exception

Use cry cateh prompts to throw the appropriate Chinese

Guess you like

Origin www.cnblogs.com/xiaozhang666/p/12039143.html