Shiro超详细学习笔记(附源码),2024年最新python大学计算机

commons-logging

commons-logging

1.2

commons-httpclient

commons-httpclient

3.1

2.3 简单的Shiro测试案例


2.3.1 配置初始化文件

在resources文件下配置初始化文件——shiro-first.ini

#配置用户名,带用户数据表

[users]

kaka=1234

taotao=4567

2.3.2 编写测试文件

在test文件下编写测试类

import org.apache.shiro.SecurityUtils;

import org.apache.shiro.authc.*;

import org.apache.shiro.config.IniSecurityManagerFactory;

import org.apache.shiro.crypto.hash.Md5Hash;

import org.apache.shiro.mgt.SecurityManager;

import org.apache.shiro.subject.Subject;

import org.junit.Test;

/**

  • Created by Kak on 2020/9/2.

*/

public class TestShiroBase {

@Test

//认证处理

public void testShiroFirst(){

//根据ini初始化文件创建SecurityManager工厂

IniSecurityManagerFactory factory = new IniSecurityManagerFactory(“classpath:shiro-first.ini”);

//生成安全管理器,创建securityManager实例

SecurityManager securityManager = factory.createInstance();

//使用shiro提供的工具装配安全管理器

SecurityUtils.setSecurityManager(securityManager);

//获取shiro访问的主体对象

Subject subject = SecurityUtils.getSubject();

//模拟用户登录操作创建访问主体的令牌

AuthenticationToken token = new UsernamePasswordToken(“kaka”, “1234”);

try{

subject.login(token);

}catch (IncorrectCredentialsException icex){

System.out.println(“用户口令错误!!!”);

}catch (UnknownAccountException uaEx){

System.out.println(“用户名错误!!!”);

}catch (AuthenticationException aex){

System.out.println(“用户认证失败!!!”);

}

if (subject.isAuthenticated()){

System.out.println(“用户登录成功!!!”);

}

}

}

2.3.3 运行结果

在这里插入图片描述

2.4 带角色权限的认证


2.4.1 配置初始化文件

在resources文件下配置初始化文件——shiro-perms.ini

[users]

kaka=1234,role1,role2

taotao=12345,role2,role3

[roles]

role1=stu:select,stu:insert

role2=stu:update,stu:delete

role3=emp:select

2.4.2 编写测试文件

在test文件下编写测试类

@Test

//加入权限

public void testShiroPerms(){

IniSecurityManagerFactory factory = new IniSecurityManagerFactory(“classpath:shiro-perms.ini”);

SecurityManager securityManager = factory.createInstance();

SecurityUtils.setSecurityManager(securityManager);

Subject subject = SecurityUtils.getSubject();

UsernamePasswordToken token = new UsernamePasswordToken(“kaka”,“1234”);

try{

subject.login(token);

}catch (AuthenticationException aex){

System.out.println(“用户认证错误,登录失败!!!”);

}

if(subject.isAuthenticated()){

System.out.println(“用户登录成功!!!”);

if(subject.isPermitted(“stu:select”)){

System.out.println(“用户有查询权限stu:select”);

}else{

System.out.println(“无权访问stu:select!!!”);

}

String[] perms = {“stu:select”,“stu:delete”};

boolean[] permitted = subject.isPermitted(perms);//我们需要判断的权限

if (permitted[1]){

System.out.println(“用户有权访问stu:delete”);

}

if(!subject.isPermittedAll(perms)){

System.out.println(“用户无权访问!!!”);

}

if (subject.hasRole(“role1”)){

System.out.println(“kaka有role1角色”);

}

}

}

2.4.3 运行结果

在这里插入图片描述

2.5 自定义realm的shiro案例


2.5.1 配置初始化文件

在resources文件下配置初始化文件——shiro-realm.ini

[main]

#自定义realm

myRealm=com.sx.kak.shiro.MyRealm

#在securityManager中设置自定义的realm

securityManager.realms=$myRealm

2.5.2 自定义安全策略

创建shiro包,在包下创建MyRealm.java

package com.sx.kak.shiro;

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.authz.AuthorizationInfo;

import org.apache.shiro.authz.SimpleAuthorizationInfo;

import org.apache.shiro.realm.AuthorizingRealm;

import org.apache.shiro.subject.PrincipalCollection;

import java.util.HashSet;

import java.util.Set;

/**

  • Created by Kak on 2020/9/2.

*/

public class MyRealm extends AuthorizingRealm{

//shiro中的授权实现

@Override

protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

Object principal = principalCollection.getPrimaryPrincipal();

//根据用户信息查询数据库中的权限

String userName = (String) principal;

//创建用户授权信息对象

SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

//创建权限集合

Set perms = new HashSet();

perms.add(“stu:select”);

perms.add(“stu:update”);

authorizationInfo.setStringPermissions(perms);

return authorizationInfo;

}

//shiro中的认证实现

@Override

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

//获取用户信息

String username = (String)authenticationToken.getPrincipal();

//获取密码

Object credentials = authenticationToken.getCredentials();

System.out.println(“userName:” + username+“password:”+credentials);

//根据用户名查询用户对象信息获取用密码

String userName=“kaka”;

String password=“1234”;

//将送来用户账号及根据账号查出的密码(凭证)封装成一个AuthenticationInfo对象,返回

SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userName, password, getName());

//将外部获取的用户信息及凭证信息 封装送给Authenticator对象进行认证

return simpleAuthenticationInfo;

}

}

2.5.3 编写测试文件

在test文件下编写测试类

@Test

//自定义安全策略的shiro

public void testShiroRealm(){

IniSecurityManagerFactory factory = new IniSecurityManagerFactory(“classpath:shiro-realm.ini”);

SecurityManager securityManager = factory.createInstance();

SecurityUtils.setSecurityManager(securityManager);

Subject subject = SecurityUtils.getSubject();

UsernamePasswordToken token = new UsernamePasswordToken(“kaka”, “1234”);

try{

subject.login(token);

}catch (UnknownAccountException uaex){

System.out.println(“账户名不存在!!!”);

}catch (IncorrectCredentialsException ice){

System.out.println(“凭证错误!!!”);

}catch (AuthenticationException ae){

System.out.println(“认证失败!!!”);

}

if(subject.isAuthenticated()){

System.out.println(“用户登录成功!!!”);

}else{

System.out.println(“用户登录失败!!!”);

}

if(subject.isPermittedAll(“stu:select”)){

System.out.println(“kaka用户有权访问stu:select”);

}else{

System.out.println(“kaka用户无权访问stu:select”);

}

}

2.5.4 运行结果

在这里插入图片描述

2.6 MD5密钥


一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致;

//生成MD5密钥

public void testMd5(){

String pwd = “1234”;

String salt = “kaka”;

Md5Hash md5Hash1 = new Md5Hash(pwd);

String md5Str1 = md5Hash1.toString();

System.out.println(“md5加密:” + md5Str1);

Md5Hash md5Hash2 = new Md5Hash(pwd, salt, 1);

String md5Str2 = md5Hash2.toString();

System.out.println(“加盐:”+md5Str2);

Md5Hash md5Hash3 = new Md5Hash(pwd, salt, 1024);

String md5Str3 = md5Hash3.toString();

System.out.println(“多次加盐:”+md5Str3);

}

md5加密:81dc9bdb52d04dc20036dbd8313ed055

加盐:4fa51ff55b001ea9b7c55338b76834f7

多次加盐:eaee658c75dc83917d7be1bd689ff15e

在这里插入图片描述

2.7 自定义realm的shiro加入MD5案例


2.7.1 配置初始化文件

[main]

#定义凭证匹配器

credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher

#设置散列算法

credentialsMatcher.hashAlgorithmName=md5

#设置散列次数

credentialsMatcher.hashIterations=1

#将凭证匹配器设置到realm

myRealm=com.sx.kak.shiro.MyRealmMd5

#在securityManager中设置自定义的realm

securityManager.realms=$myRealm

myRealm.credentialsMatcher=$credentialsMatcher

2.7.2 自定义安全策略(MD5)

创建shiro包,在包下创建MyRealmMd5.java

package com.sx.kak.shiro;

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.authz.AuthorizationInfo;

import org.apache.shiro.realm.AuthorizingRealm;

import org.apache.shiro.subject.PrincipalCollection;

import org.apache.shiro.util.ByteSource;

/**

  • Created by Kak on 2020/9/2.

*/

public class MyRealmMd5 extends AuthorizingRealm{

//授权

@Override

protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

return null;

}

//认证

@Override

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

//用户名

String principal = (String)authenticationToken.getPrincipal();

//根据用户名查询用户信息(密码)

String hashedCredentials =“4fa51ff55b001ea9b7c55338b76834f7”;

ByteSource credentialsSalt = ByteSource.Util.bytes(“kaka”);

/**

  • principal:用户信息

  • hashedCredentials:加密之后的密文

  • credentialsSalt:加密时加的盐

  • AuthorizingRealm 的派生类名称

*/

SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(principal, hashedCredentials, credentialsSalt, getName());

return authenticationInfo;

}

}

2.7.3 编写测试文件

在test文件下编写测试类

@Test

//自定义安全策略的shiro加密钥

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数同学面临毕业设计项目选题时,很多人都会感到无从下手,尤其是对于计算机专业的学生来说,选择一个合适的题目尤为重要。因为毕业设计不仅是我们在大学四年学习的一个总结,更是展示自己能力的重要机会。

因此收集整理了一份《2024年计算机毕业设计项目大全》,初衷也很简单,就是希望能够帮助提高效率,同时减轻大家的负担。
img
img
img

既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!

由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频

如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
img

dCredentials, credentialsSalt, getName());

return authenticationInfo;

}

}

2.7.3 编写测试文件

在test文件下编写测试类

@Test

//自定义安全策略的shiro加密钥

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数同学面临毕业设计项目选题时,很多人都会感到无从下手,尤其是对于计算机专业的学生来说,选择一个合适的题目尤为重要。因为毕业设计不仅是我们在大学四年学习的一个总结,更是展示自己能力的重要机会。

因此收集整理了一份《2024年计算机毕业设计项目大全》,初衷也很简单,就是希望能够帮助提高效率,同时减轻大家的负担。
[外链图片转存中…(img-zFlPb857-1712533239352)]
[外链图片转存中…(img-GsHu4x4J-1712533239353)]
[外链图片转存中…(img-qkpbYIBr-1712533239353)]

既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!

由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频

如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
[外链图片转存中…(img-uYhwsTjL-1712533239354)]

  • 25
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值