shrio(一)

shrio是一个Java安全框架,执行身份验证、授权、密码和会话管理。

 

shrio框架结构:

subject:主体,可以是用户也可以是程序,主体要访问系统,系统需要对主体进行认证、授权。

securityManager:安全管理器,主体进行认证和授权都 是通过securityManager进行。

authenticator:认证器,主体进行认证最终通过authenticator进行的。

authorizer:授权器,主体进行授权最终通过authorizer进行的。

sessionManager:web应用中一般是用web容器对session进行管理,shiro也提供一套session管理的方式。

SessionDao:  通过SessionDao管理session数据,针对个性化的session数据存储需要使用sessionDao。

cache Manager:缓存管理器,主要对session和授权数据进行缓存,比如将授权数据通过cacheManager进行缓存管理,和ehcache整合对缓存数据进行管理。

realm:域,领域,相当于数据源,通过realm存取认证、授权相关数据。

 

注意:在realm中存储授权和认证的逻辑。

 

cryptography:密码管理,提供了一套加密/解密的组件,方便开发。比如提供常用的散列、加/解密等功能。

比如 md5散列算法。

 

shrio基本jar包

 

配置shrio-first.ini配置文件

//创建SecurityManager工厂,通过ini配置文件创建SecurityManager工厂

Factor<SecurutyManager> factory  = new iniSecurityManagerFctory("classpath:shrio-first.ini");

//创建SecurityManager

SecurityManager securotyManager = factory.getInstance;

//将SecurityManager设置到当前环境

SecurityUtils.setSecurityManager(securotyManager);

// 从SecurityUtils里边创建一个subject
Subject subject = SecurityUtils.getSubject();

// 在认证提交前准备token(令牌)
// 这里的账号和密码 将来是由用户输入进去
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan",
"111111");

try {
// 执行认证提交
subject.login(token);
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// 是否认证通过
boolean isAuthenticated = subject.isAuthenticated();

System.out.println("是否认证通过:" + isAuthenticated);

// 退出操作
subject.logout();

// 是否认证通过
isAuthenticated = subject.isAuthenticated();

System.out.println("是否认证通过:" + isAuthenticated);

 

 自定义Realm:

package cn.itcast.shrio.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

 

public class CustomRealm extends AuthorizingRealm{

@Override
public String getName() {
// TODO 自动生成的方法存根
return "customRealm";
}

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
// TODO 自动生成的方法存根

//从token从获取身份信息
String usercode = (String) token.getPrincipal();
String password = new String((char[])token.getCredentials());

if(!"zhangsan".equals(usercode)) {
throw new UnknownAccountException("账号错误");
}
if(!"111111".equals(password)) {
throw new IncorrectCredentialsException("密码错误");
}

//验证成功返回
SimpleAuthenticationInfo simpleAuthenticationInfo =new SimpleAuthenticationInfo(usercode, password, this.getName());
return simpleAuthenticationInfo;

}

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// TODO 自动生成的方法存根
return null;
}

}

测试Realm方法:

public void testRealm() {

// 创建securityManager工厂,通过ini配置文件创建securityManager工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:shrio-realm.ini");

// 创建SecurityManager
SecurityManager securityManager = factory.getInstance();

// 将securityManager设置当前的运行环境中
SecurityUtils.setSecurityManager(securityManager);

// 从SecurityUtils里边创建一个subject
Subject subject = SecurityUtils.getSubject();

// 在认证提交前准备token(令牌)
// 这里的账号和密码 将来是由用户输入进去
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "111111");

try {
// 执行认证提交
subject.login(token);
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// 是否认证通过
boolean isAuthenticated = subject.isAuthenticated();

System.out.println("是否认证通过:" + isAuthenticated);

}

 

MD5算法配置:

[main]
#声明CustomRealmMd5
CustomRealmMd5=cn.itcast.shrio.realm.CustomRealmMd5
#声明credentialsMatcher
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
#加密方式
credentialsMatcher.hashAlgorithmName=md5
#加密次数
credentialsMatcher.hashIterations=1


#将realm设置到SecurityManager
CustomRealmMd5.credentialsMatcher=$credentialsMatcher
securityManager.realms=$CustomRealmMd5

 

package cn.itcast.shrio.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

 

public class CustomRealmMd5 extends AuthorizingRealm{
@Override
public String getName() {
// TODO 自动生成的方法存根
return "customRealmMd5";
}

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
// TODO 自动生成的方法存根
//从token从获取身份信息
String usercode = (String) token.getPrincipal();
String password = "f3694f162729b7d0254c6e40260bf15c";
String salt = "qwerty";
//验证成功返回
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(usercode, password, ByteSource.Util.bytes(salt), this.getName());
return simpleAuthenticationInfo;
}


@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// TODO 自动生成的方法存根
return null;
}

 

//MD5Realm测试


@Test
public void testRealmMd5() {

// 创建securityManager工厂,通过ini配置文件创建securityManager工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:shrio-realm-md5.ini");

// 创建SecurityManager
SecurityManager securityManager = factory.getInstance();

// 将securityManager设置当前的运行环境中
SecurityUtils.setSecurityManager(securityManager);

// 从SecurityUtils里边创建一个subject
Subject subject = SecurityUtils.getSubject();

// 在认证提交前准备token(令牌)
// 这里的账号和密码 将来是由用户输入进去
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "111111");

try {
// 执行认证提交
subject.login(token);
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// 是否认证通过
boolean isAuthenticated = subject.isAuthenticated();

System.out.println("是否认证通过:" + isAuthenticated);

}

}

posted on 2019-01-01 20:37  sunmmmmmy  阅读(247)  评论(0)    收藏  举报