400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

怎样让ApacheShiro保护你的应用

本篇文章给大家分享的是有关怎样让Apache Shiro保护你的应用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联长期为成百上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为黄南州企业提供专业的成都网站建设、成都网站设计黄南州网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

* authentication : 认证
* authorization : 授权
* cryptography : 密码学

一:概述

(一)What is Apache Shiro?

(二)Apache Shiro Features

怎样让Apache Shiro保护你的应用

二:核心概念:Subject,SecurityManager,Realms

(一)Subject

import org.apache.shiro.subject.Subject;
import org.apache.shiro.SecurityUtils;
...
Subject currentUser = SecurityUtils.getSubject();

(二)SecurityManager

1.Subject 的“幕后”推手是 SecurityManager

2.那么,如何设置 SecurityManager 呢?

(三)Realms

三:认证(authentication)

//1. 接受提交的当事人和证书:
AuthenticationToken token = new UsernamePasswordToken(username, password);
//2. 获取当前 Subject:
Subject currentUser = SecurityUtils.getSubject();
//3. 登录: 
currentUser.login(token);
//3. 登录:
try {
    currentUser.login(token);
} catch (IncorrectCredentialsException ice) {
    …
} catch (LockedAccountException lae) {
    …
}
…
catch (AuthenticationException ae) {…
}

四:授权(Authorization)

if ( subject.hasRole(“administrator”) ) {
    // 显示‘Create User’按钮 
} else {
    // 按钮置灰?
}
if ( subject.isPermitted(“user:create”) ) {
    // 显示‘Create User’按钮 
} else {
    // 按钮置灰?
}
if ( subject.isPermitted(“user:delete:jsmith”) ) {
    // 删除‘jsmith’用户 
} else {
    // 不删除‘jsmith’
}

小结

五:会话管理器(Session Managerment)

Session session = subject.getSession();
Session session = subject.getSession(boolean create);
Session session = subject.getSession();
session.getAttribute("key", someValue);
Date start = session.getStartTimestamp();
Date timestamp = session.getLastAccessTime();
session.setTimeout(millis);
...

六:加密(Cryptography)

(一)哈希

1.不使用Shiro,你需要如下步骤才能完成上述内容:

1)将文件转换成字节数组。
2)使用MessageDigest类对字节数组进行哈希,处理相关异常。
try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.digest(bytes);
    byte[] hashed = md.digest();
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}
3)将哈希后的字节数组编码成十六进制字符。

2.使用shiro完成

String hex = new Md5Hash(myFile).toHex();
String encodedPassword = new Sha512Hash(password, salt, count).toBase64();

(二)密码

AesCipherService cipherService = new AesCipherService();
cipherService.setKeySize(256);

// 创建一个测试密钥: 
byte[] testKey = cipherService.generateNewKey();
// 加密文件的字节: 
byte[] encrypted = cipherService.encrypt(fileBytes, testKey);

七:web支持(Web Support)

(一)web.xml中的ShiroFilter


    ShiroFilter
    org.apache.shiro.web.servlet.IniShiroFilter
     


    ShiroFilter
    /* 

(二)URL 特定的 Filter 链

[urls]
/assets/** = anon
/user/signup = anon
/user/** = user
/rpc/rest/** = perms[rpc:invoke], authc
/** = authc

(三)JSP 标签库

<%@ taglib prefix="shiro" 
   uri="http://shiro.apache.org/tags" %>
...

Hello             !             ! Register today!

Shiro 还支持其他许多 Web 特性,如简单的“记住我”服务,REST 和 BASIC 认证。当然,如果想使用 Shiro 原生的企业会话,它还提供透明的 HttpSession 支持。参见Apache Shiro Web 文档可以了解更多内容。

(四)Web 会话管理

1.缺省 Http 会话

2.Web 层中 Shiro 的原生会话

以上就是怎样让Apache Shiro保护你的应用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


当前题目:怎样让ApacheShiro保护你的应用
当前URL:http://mzwzsj.com/article/jhhisi.html

其他资讯

让你的专属顾问为你服务