shiro
(java security framework)
Apache Shiro is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography and session management. Using Shiro's easy-to-understand API, you can quickly and easily get any application, from the smallest mobile applications to the largest web and enterprise applications.
The following is a simple implementation of hello word
Next, add the core package of shiro in the pom.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hp.shiro</groupId> <artifactId>Shiro</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Shiro</name> <description>Shiro</description> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.8.0-beta2</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> </dependencies> </project>
Add the username and password required for login in src/main/resources. Since it is the first hello word, it does not connect to the database for verification, as shown in the figure
(Note that the file needs to be placed in resources to be read)
package com.hp.hello; 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; public class HelloWord { public static void main(String[] args) { //Read the configuration file to initialize the SecurityManager factory IniSecurityManagerFactory Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro.ini"); //Get the SecurityManager instance SecurityManager instance = factory.getInstance(); //Bind the instance of SecurityManager to SecurityUtils SecurityUtils.setSecurityManager(instance); //Get the currently executing user Subject subject = SecurityUtils.getSubject(); //create token token username/password UsernamePasswordToken token=new UsernamePasswordToken("hello", "1123"); try{ //login/authentication subject.login(token); System.out.println("Authentication successful..."); }catch(Exception e){ System.out.println("Authentication failed..."); } //quit subject.logout(); } }
Then run the project as shown in the figure, see the input error
Change the username and password to the correct one
There is also a bug, the log4j log can't come out, and I will solve it next time