失效链接处理 |
安全框架Shiro使用 PDF 下载
本站整理下载:
提取码:uk3i
相关截图:
主要内容:
1.参考网址:
https://www.w3cschool.cn/shiro/co4m1if2.html
2.Shiro的简介与核心对象介绍
2.1.Shiro中的核心模块
2.2.Shiro的架构
2.3.Shiro的核心对象(类和对象)
3.认证
3.1.Realm
3.1.1.SimpleAccountRealm
package com.shiro.authc;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
public class Realm_Simple {
public static void main(String[] args) {
SimpleAccountRealm realm = new SimpleAccountRealm();
realm.addAccount("admin", "123");
realm.addAccount("guest", "456");
realm.addAccount("test", "789");
//1、实例化SecurityManagert和对应的Realm数据。
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//注入一个Realm
securityManager.setRealm(realm);
//2、获取Subject对象
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
//3、程序提交用户名和密码交给Shiro来进行判断。
String username = "admin";
String password = "123";
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
subject.login(token);
System.out.println("认证成功");
System.out.println("认证后的身份信息 = " + subject.getPrincipal());
} catch (org.apache.shiro.authc.UnknownAccountException e) {
System.out.println("用户名不存在的异常");
} catch (org.apache.shiro.authc.IncorrectCredentialsException e) {
System.out.println("密码不正确");
}
}
}
3.1.2.IniRealm
3.1.2.1.Shiro-config.ini
[main]
authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy
authenticator.authenticationStrategy=$authenticationStrategy
securityManager.authenticator=$authenticator
authorizer=org.apache.shiro.authz.ModularRealmAuthorizer
permissionResolver=org.apache.shiro.authz.permission.WildcardPermissionResolver
authorizer.permissionResolver=$permissionResolver
securityManager.authorizer=$authorizer
[users]
admin=123
guest=456
test=789
[roles]
[urls]
package com.shiro.authc;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
public class Realm_Ini {
public static void main(String[] args) {
//1、创建一个shiro的配置文件,并且读取INI文件
Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-config.ini");
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
//2、获取Subject对象
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
//3、程序提交用户名和密码交给Shiro来进行判断。
String username = "admin";
String password = "123";
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
subject.login(token);
System.out.println("认证成功");
System.out.println("认证后的身份信息 = " + subject.getPrincipal());
System.out.println("是否通过认证 = " + subject.isAuthenticated());
} catch (org.apache.shiro.authc.UnknownAccountException e) {
System.out.println("用户名不存在的异常");
} catch (org.apache.shiro.authc.IncorrectCredentialsException e) {
System.out.println("密码不正确");
}
}
}
|