package com.alatus.shirotest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
public class ShiroRun {
public static void main(String[] args) {
// 初始化获取SecurityManager
IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
// 获取管理器的实例
SecurityManager securityManager = factory.getInstance();
// 把管理器对象放进工具中
SecurityUtils.setSecurityManager(securityManager);
// 获取Subject对象
Subject subject = SecurityUtils.getSubject();
// 通过工具创建Token对象,web应用用户名密码由页面传递
AuthenticationToken token = new UsernamePasswordToken("zhangsan","admin123");
// 校验通过即可完成登录
try{
subject.login(token);
// 如果登录失败会抛出异常的
System.out.println("登陆成功");
// 判断角色
boolean role1 = subject.hasRole("role1");
System.out.println("有没有role1"+role1);
// 判断权限
boolean permitted = subject.isPermitted("user:insert");
System.out.println("有没有插入权限"+permitted);
// check权限的方法,没有返回值,如果没有会直接抛异常
subject.checkPermission("user:select");
}catch (IncorrectCredentialsException e){
System.out.println("密码错误");
e.printStackTrace();
}
catch (UnknownAccountException e){
System.out.println("用户不存在");
e.printStackTrace();
}
catch (AuthenticationException e){
System.out.println("登录异常");
e.printStackTrace();
}
}
}
package com.alatus.shirotest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
public class ShiroRun {
public static void main(String[] args) {
// 初始化获取SecurityManager
IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
// 获取管理器的实例
SecurityManager securityManager = factory.getInstance();
// 把管理器对象放进工具中
SecurityUtils.setSecurityManager(securityManager);
// 获取Subject对象
Subject subject = SecurityUtils.getSubject();
// 通过工具创建Token对象,web应用用户名密码由页面传递
AuthenticationToken token = new UsernamePasswordToken("zhangsan","admin123");
// 校验通过即可完成登录
try{
subject.login(token);
// 如果登录失败会抛出异常的
System.out.println("登陆成功");
// 判断角色
boolean role1 = subject.hasRole("role1");
System.out.println("有没有role1"+role1);
// 判断权限
boolean permitted = subject.isPermitted("user:insert");
System.out.println("有没有插入权限"+permitted);
// check权限的方法,没有返回值,如果没有会直接抛异常
subject.checkPermission("user:select");
}catch (IncorrectCredentialsException e){
System.out.println("密码错误");
e.printStackTrace();
}
catch (UnknownAccountException e){
System.out.println("用户不存在");
e.printStackTrace();
}
catch (AuthenticationException e){
System.out.println("登录异常");
e.printStackTrace();
}
}
}
package com.alatus.shirotest;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
public class ShiroMD5 {
public static void main(String[] args) {
// 密码明文用于测试
String password = "admin123";
// 使用MD5加密
Md5Hash md5Hash = new Md5Hash(password);
System.out.println(md5Hash.toHex());
// 带盐的MD5加密
Md5Hash md5Hash2 = new Md5Hash(password,"salt");
System.out.println(md5Hash2.toHex());
// 多次迭代加密
Md5Hash md5Hash3 = new Md5Hash(password,"salt",3);
// 这里做了带盐的三次迭代加密,更难破解
System.out.println(md5Hash3.toHex());
// 使用父类进行加密
SimpleHash simpleHash = new SimpleHash("MD5",password,"salt",3);
// 这是通过父类的带盐三次加密,可以指定类型的
System.out.println(simpleHash.toHex());
}
}
package com.alatus.shirotest;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
public class ShiroMD5 {
public static void main(String[] args) {
// 密码明文用于测试
String password = "admin123";
// 使用MD5加密
Md5Hash md5Hash = new Md5Hash(password);
System.out.println(md5Hash.toHex());
// 带盐的MD5加密
Md5Hash md5Hash2 = new Md5Hash(password,"salt");
System.out.println(md5Hash2.toHex());
// 多次迭代加密
Md5Hash md5Hash3 = new Md5Hash(password,"salt",3);
// 这里做了带盐的三次迭代加密,更难破解
System.out.println(md5Hash3.toHex());
// 使用父类进行加密
SimpleHash simpleHash = new SimpleHash("MD5",password,"salt",3);
// 这是通过父类的带盐三次加密,可以指定类型的
System.out.println(simpleHash.toHex());
}
}
package com.alatus.shirotest;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;
public class MyRealm extends AuthenticatingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
// 自定义的登录验证方法
// Shiro的login方法底层会调用这个类的认证方法进行认证
// 需要配置让自定义的realm生效,可以在imi文件中配置,也可以在spring的ioc容器配置
// 该方法只是获取进行对比的信息,认证逻辑还是Shiro底层的认证逻辑
// 获取身份信息
String username = authenticationToken.getPrincipal().toString();
System.out.println(username);
// 获取凭证信息
String password = authenticationToken.getCredentials().toString();
System.out.println(password);
// 访问数据库获取存储的信息
// 假设数据库我们找到只有一个叫zhangsan的用户
if(username.equals("zhangsan")){
// 从数据库查询加密后的密文
String pwd = "2c72932f1de524f5a0062c288847f97b";
// 封装到校验逻辑对象
AuthenticationInfo info = new SimpleAuthenticationInfo(
authenticationToken.getPrincipal(),
pwd,
ByteSource.Util.bytes("salt"),
authenticationToken.getPrincipal().toString()
);
// 返回数据
return info;
}
return null;
}
}
package com.alatus.shirotest;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;
public class MyRealm extends AuthenticatingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
// 自定义的登录验证方法
// Shiro的login方法底层会调用这个类的认证方法进行认证
// 需要配置让自定义的realm生效,可以在imi文件中配置,也可以在spring的ioc容器配置
// 该方法只是获取进行对比的信息,认证逻辑还是Shiro底层的认证逻辑
// 获取身份信息
String username = authenticationToken.getPrincipal().toString();
System.out.println(username);
// 获取凭证信息
String password = authenticationToken.getCredentials().toString();
System.out.println(password);
// 访问数据库获取存储的信息
// 假设数据库我们找到只有一个叫zhangsan的用户
if(username.equals("zhangsan")){
// 从数据库查询加密后的密文
String pwd = "2c72932f1de524f5a0062c288847f97b";
// 封装到校验逻辑对象
AuthenticationInfo info = new SimpleAuthenticationInfo(
authenticationToken.getPrincipal(),
pwd,
ByteSource.Util.bytes("salt"),
authenticationToken.getPrincipal().toString()
);
// 返回数据
return info;
}
return null;
}
}