项目结构
image.pngpom.xml
<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.project.tingshuo</groupId>
<artifactId>shior02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>0.2.23</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
</project>
自定义的Realm
package shiro02;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;
/**
* 自定义Realm
* @author wqj24
*
*/
public class myrealm implements Realm{
@Override
public String getName() {
return "myrealm";
}
@Override
public boolean supports(AuthenticationToken token) {
return token instanceof UsernamePasswordToken;
}
@Override
public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
// 获取用户名跟密码
String username = (String)token.getPrincipal();
String password = new String((char[])token.getCredentials());
// 如果用户名错误
if (!"admin".equals(username)) {
throw new UnknownAccountException();
}
// 如果密码错误
if (!"ad123".equals(password)) {
throw new IncorrectCredentialsException();
}
// 验证通过
return new SimpleAuthenticationInfo(username, password, getName());
}
}
配置文件 shiro-realm.ini
[main]
#声明realm
myrealm=shiro02.myrealm
#指定securityManager的realm实现
securityManager.realms=$myrealm
配置文件格式不能是txt
应为ini
且编码格式为ANSI
也可以声明多个realm
,并指定多个实现如:
[main]
#声明realm
myrealm1=shior02.myrealm01
myrealm2=shior02.myrealm02
myrealm3=shior02.myrealm03
#指定securityManager的realm实现
securityManager.realms=$myrealm1,$myrealm02
也可以不用显示的指定securityManager
的realm
实现,securityManager
会按照 realm
声明的顺序自动发现,显示指点后,没有指定的realm
就会被忽略。
测试
package shiro02;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
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;
import org.junit.jupiter.api.Test;
public class TestMain {
public static void main(String[] args) {
// 获取SecurityManager工厂
Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:shiro-realm.ini");
// 获通过工厂获取SecurityManager实例
SecurityManager securityManager = factory.getInstance();
// 将实例绑定给SecurityUtils
SecurityUtils.setSecurityManager(securityManager);
// 获取Subject
Subject subject = SecurityUtils.getSubject();
// 创建Token
UsernamePasswordToken token = new UsernamePasswordToken("admin", "ad123");
// 登录(验证用户)
try {
subject.login(token);
} catch (AuthenticationException e) {
// 验证失败
e.printStackTrace();
}
if (subject.isAuthenticated()) {
System.out.println("登录成功!!!");
subject.logout();
}
}
测试结果:
image.png
网友评论