总结原生Shiro的使用流程
一、准备
1. 配置依赖
<dependencies>
<!--配置shiro的依赖-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
2. 新建配置文件
在resources下新建shiro.ini,文件名称任意,扩展名必须是ini。
[users]
#zhangsan(账号)=zs(密码)
zhangsan=zs
lisi=ls
3.新建测试类
package com.bjsxt.test;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.UnauthorizedException;
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 java.util.UUID;
public class ShiroRun {
public static void main(String[] args) {
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager instance = factory.getInstance();
//获取subjectduix
SecurityUtils.setSecurityManager(instance);
Subject subject = SecurityUtils.getSubject();
//3.登录认证
//创建AuthenticationToken 对象存储要认真的信息
AuthenticationToken token = new UsernamePasswordToken("zhangsan","zc");
//认证
try{
subject.login(token);
System.out.println("认证成功");
}catch (UnknownAccountException e){
System.out.println("账户不存在");
} catch (IncorrectCredentialsException e) {
System.out.println("密码错误");
} catch (AuthenticationException e) {
e.printStackTrace();
}
}
}
二、授权
1. 判断角色
- ①修改配置文件
[users]
#zhangsan(账号)=zs(密码) role1,role2为角色
zhangsan=zs,role1,role2
lisi=ls
- ②修改代码
package com.bjsxt.test;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.UnauthorizedException;
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 java.util.UUID;
public class ShiroRun {
public static void main(String[] args) {
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager instance = factory.getInstance();
//获取subjectduix
SecurityUtils.setSecurityManager(instance);
Subject subject = SecurityUtils.getSubject();
//3.登录认证
//创建AuthenticationToken 对象存储要认真的信息
AuthenticationToken token = new UsernamePasswordToken("zhangsan","zs");
//认证
try{
subject.login(token);
System.out.println("认证成功");
}catch (UnknownAccountException e){
System.out.println("账户不存在");
} catch (IncorrectCredentialsException e) {
System.out.println("密码错误");
} catch (AuthenticationException e) {
e.printStackTrace();
}
//授权
//判断是否具备某个角色
boolean role1 = subject.hasRole("role1");
System.out.println("是否具备角色:"+role1);
}
}
2. 判断权限
- ①修改配置文件
[users]
#zhangsan(账号)=zs(密码) role1,role2为角色
zhangsan=zs,role1,role2
lisi=ls
[roles]
# user:insert代表user表具有insert权限
role1=user:insert,user:update
- ②修改代码
package com.bjsxt.test;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.UnauthorizedException;
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 java.util.UUID;
public class ShiroRun {
public static void main(String[] args) {
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager instance = factory.getInstance();
//获取subjectduix
SecurityUtils.setSecurityManager(instance);
Subject subject = SecurityUtils.getSubject();
//3.登录认证
//创建AuthenticationToken 对象存储要认真的信息
AuthenticationToken token = new UsernamePasswordToken("zhangsan","zs");
//认证
try{
subject.login(token);
System.out.println("认证成功");
}catch (UnknownAccountException e){
System.out.println("账户不存在");
} catch (IncorrectCredentialsException e) {
System.out.println("密码错误");
} catch (AuthenticationException e) {
e.printStackTrace();
}
//授权
//判断是否具备某个角色
boolean role1 = subject.hasRole("role1");
System.out.println("是否具备角色:"+role1);
//判断是否具备某个权限
try {
subject.checkPermission("user2:insert");
}catch (Exception e){
System.out.println("权限不足");
}
}
}
三、加密
在实际开发中数据库中一些敏感信息经常会被加密存储。如:用户密码等。
Shiro框架内嵌了很多加密算法。如MD5等。使用Shiro框架时可以很方便的实现
加密功能。
在往数据库存储密码时,通过MD5加密存储到数据库中,这样可以提高数据的安全性
使用方法
public class ShiroMd5 {
public static void main(String[] args) {
//要加密的数据
String pwd="wollo";
//1.MD5加密,原生的MD5加密已经没有以前那么安全了
Md5Hash md5Hash=new Md5Hash(pwd);
System.out.println("原生的MD5加密:"+md5Hash.toHex());
//2.带盐的MD5加密,比原始方式安全,但是也可以破解,只不过需要些时间罢了
Md5Hash md5Hash2=new Md5Hash(pwd,"bjsxt");
System.out.println("加盐的MD5加密:"+md5Hash2.toHex());
//3.带盐的迭代加密
Md5Hash md5Hash3=new Md5Hash(pwd,"bjsxt",2);
System.out.println("加盐的迭代的MD5加密:"+md5Hash3.toHex());
//4.使用父类完成加密
SimpleHash simpleHash=new SimpleHash("md5",pwd,"bjsxt",2);
System.out.println("使用父类加密:"+simpleHash.toHex());
}
}
四、自定义Realm
1. 自定义Realm类
- 在项目中新建com.bjsxt.realm.MyRealm
package com.bjsxt.realm;
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 token) throws AuthenticationException {
System.out.println("执行认证");
//用户名
String username = token.getPrincipal().toString();
//密码
String pwd = new String((char[])token.getCredentials());
System.out.println(username+" "+pwd);
//先从数据库查询select * from user where username=? 查看用户名是否存在
if(username.equals("admin")){//假设用户名为admin时能从数据库中查询出来
//根据之前查询出来的用户信息获取密码。假设查询出来的密码是pwd
String password= "pwd";
//此处需要注意,第二个参数是从数据库查询出来的密码,而不是传递过来的密码。
//第三个参数自定义。但是尽量不重复了。常直接使用用户名当做realname名字。
AuthenticationInfo info = new SimpleAuthenticationInfo(token.getPrincipal(),password,"realmname");
//shiro会判断从数据库查询出来的密码和客户端传递过来的密码是否一致。
return info;
}
//返回null说明用户名不存在。
return null;
}
}
2. 修改配置文件
[main]
myrealm=com.bjsxt.realm.MyRealm
securityManager.realms=$myrealm
[users]
#zhangsan(账号)=zs(密码) role1,role2为角色
zhangsan=zs,role1,role2
lisi=ls
[roles]
# user:insert代表user表具有insert权限
role1=user:insert,user:update
五、凭证匹配器
1. 修改自定义Realm类
- 在项目中新建com.bjsxt.realm.MyRealm
package com.bjsxt.realm;
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 token) throws AuthenticationException {
System.out.println("执行认证");
//用户名
String username = token.getPrincipal().toString();
//密码
String pwd = new String((char[])token.getCredentials());
System.out.println(username+" "+pwd);
//先从数据库查询select * from user where username=? 查看用户名是否存在
if(username.equals("admin")){//假设用户名为admin时能从数据库中查询出来
//密码必须是MD5加密后的密码(自行更改)
String password= "7614fd642608ca0755b78d2b2c352e19";
//假设id是从数据库中取出的id
Long id = 123L;
//第三个参数是加密的盐
AuthenticationInfo info = new SimpleAuthenticationInfo(token.getPrincipal(), password, ByteSource.Util.bytes(“加密使用的值”) ,token.getPrincipal().toString());
//shiro会判断从数据库查询出来的密码和客户端传递过来的密码是否一致。
return info;
}
//返回null说明用户名不存在。
return null;
}
}
2. 修改配置文件
[main]
#使用MD5加密
md5CredentialsMatcher=org.apache.shiro.authc.credential.Md5CredentialsMatcher
#因为为加密两次 所以也需要解两次
md5CredentialsMatcher.hashIterations=2
myrealm=com.bjsxt.shiro.MyRealm
myrealm.credentialsMatcher=$md5CredentialsMatcher
securityManager.realms=$myrealm
[users]
#zhangsan(账号)=zs(密码) role1,role2为角色
zhangsan=zs,role1,role2
lisi=ls
[roles]
# user:insert代表user表具有insert权限
role1=user:insert,user:update
本文由
loneliness
原创发布于
阳光沙滩
,未经作者授权,禁止转载