shiro——二

简单地说一下shiro四个基本核心功能:

Authentication(认证)

过程可以分为三步:

1、Collect the user’s identifying information, calledprincipals, and supporting proof of identity, calledcredentials.(收集用户的标识信息,被称为主体,并支持身份证明,称为凭证。)

2、Submit the principals and credentials to the system.(向系统提交主体和凭证。)

3、If the submitted credentials match what the system expects for that user identity (principal), the user is considered authenticated. If they don’t match, the user is not considered authenticated.(如果提交的凭证与系统期望的用户标识(主体)匹配,那么用户就被认为是经过身份验证的。如果它们不匹配,则用户不被认为是经过身份验证的。)

Subject subject = SecurityUtils.getSubject();

AuthenticationToken authenticationToken = new MyUsernamePasswordToken(userName, password, verifyCode, rememberMe);

public String login() {

try/catch

}

Authentication(认证)之后,用户可以进入系统,但是,能做哪些操作呢?具体的就需要进一步判断。

Authorization(授权):

可以分为role(角色)与permission(权限)验证:

role check:

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

//show the ‘Create User’ button

} else {

//grey-out the button?

}

permission check:

if ( subject.isPermitted(“user:create”) ) {

//show the ‘Create User’ button

} else {

//grey-out the button?

}

role(角色)验证,在运行时不能添加或者删除。permission(权限)验证更加灵活,具有更细粒度,自己需要什么可以写什么。

Session Management(会话管理):

shiro 的session(会话)是独立于容器的,也就是说,不再需要专门的servlet与EJB容器了。对于企业缓存(enterprise caches)、关系数据库(relational databases)、NoSQL等可以一次性配置会话集群,不需要根据部署应用程序的方式重新配置应用程序。

Session session = subject.getSession();

Cryptography(加密):

简化利用JDK的密码支持使得数据混淆加密。使用的是hash(哈希)与ciphers(密码)。

之前使用MD5的加密方式如下:

try {

MessageDigest md = MessageDigest.getInstance("MD5");

md.digest(bytes);

byte[] hashed = md.digest();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

}

使用shiro之后:

String hex = new Md5Hash(myFile).toHex();

怎么样?简单了太多了吧,一句话就搞定了。简单粗暴,代码也更优雅。

使用256-bit AES加密,shiro下代码如下:

AesCipherService cipherService = new AesCipherService();

cipherService.setKeySize(256);


推荐微信公众号【排骨肉段】,记录日常的美好。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、shiro的工作流程 项目每次启动时,根据shiroConfig的配置,将相应权限url加载到shiro框架中...
    夏日橘子冰阅读 1,905评论 4 2
  • 最终demo 开始之前,我们先造一些数据: 数据有了,下面开始Shrio的配置学习。开始之前先添加Shiro依赖 ...
    忧郁的小码仔阅读 8,705评论 11 21
  • Apache Shiro Apache Shiro 是一个强大而灵活的开源安全框架,它干净利落地处理身份认证,授权...
    罗志贇阅读 3,249评论 1 49
  • 一、架构 要学习如何使用Shiro必须先从它的架构谈起,作为一款安全框架Shiro的设计相当精妙。Shiro的应用...
    ITsupuerlady阅读 3,546评论 4 32
  • 在很多年前的夏天,我喜欢上了一个姑娘。她是海归,长得比汤唯还要好看。那时候,她是高大上设计师,我是矮穷矬程序员。 ...
    b77723ecf638阅读 241评论 0 0