文章目录
Shiro加密
1.1 在CustomRealmTest中设置Shiro加密名称和加密次数
//Shiro加密
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
//加密名称
matcher.setHashAlgorithmName("md5");
//加密的次数
matcher.setHashIterations(1);
customRealm.setCredentialsMatcher(matcher);
CustomRealmTest类完整代码
package com.amoscxy.test;
import com.amoscxy.shiro.realm.CustomRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
public class CustomRealmTest {
@Test
public void testAuthentication(){
CustomRealm customRealm = new CustomRealm();
//1.构建SecurityManager
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
defaultSecurityManager.setRealm(customRealm);
//Shiro加密
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
//加密名称
matcher.setHashAlgorithmName("md5");
//加密的次数
matcher.setHashIterations(1);
customRealm.setCredentialsMatcher(matcher);
//2.主体提交认证请求
SecurityUtils.setSecurityManager(defaultSecurityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("Mark","1234567");
subject.login(token);
System.out.println("isAuthenticated:"+subject.isAuthenticated());
subject.checkRole("admin");
subject.checkPermissions("user:add","user:delete");
}
}
1.2 计算CustomRealm中密码的加盐md5加密值,修改密码为加密值
Map<String,String> userMap = new HashMap<String, String>(16);
{
userMap.put("Mark","d40fdd323f5322ff34a41f026f35cf20");
super.setName("customRealm");
}
1.3 CustomRealm中的认证函数doGetAuthenticationInfo中设置盐值
/**
* 做认证
* @param authenticationToken
* @return
* @throws AuthenticationException:主体传过来的认证信息
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//1.从主体传过来的认证信息中,获得用户名
String userName = (String)authenticationToken.getPrincipal();
//2.通过用户名到数据库获取凭证
String password = getPasswordByUserName(userName);
if(password == null){
return null;
}
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName,password,"customRealm");
//设置盐值
authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("Mark"));
return authenticationInfo;
}
CustomRealm类完整代码
package com.amoscxy.shiro.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.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import java.util.*;
/**
* Created by cxy on 2018/9/18.
*/
public class CustomRealm extends AuthorizingRealm {
Map<String,String> userMap = new HashMap<String, String>(16);
{
userMap.put("Mark","d40fdd323f5322ff34a41f026f35cf20");
super.setName("customRealm");
}
/**
* 做授权
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//1.从主体传过来的认证信息中,获得用户名
String userName = (String)principalCollection.getPrimaryPrincipal();
//2.从数据库或者缓存中获取角色数据
Set<String> roles = getRolesByUserName(userName);
//从数据库或者缓存中获取权限数据
Set<String> permissions = getPermissionsByUserName(userName);
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.setStringPermissions(permissions);
simpleAuthorizationInfo.setRoles(roles);
return simpleAuthorizationInfo;
}
/**
* 做认证
* @param authenticationToken
* @return
* @throws AuthenticationException:主体传过来的认证信息
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//1.从主体传过来的认证信息中,获得用户名
String userName = (String)authenticationToken.getPrincipal();
//2.通过用户名到数据库获取凭证
String password = getPasswordByUserName(userName);
if(password == null){
return null;
}
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName,password,"customRealm");
//设置盐值
authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("Mark"));
return authenticationInfo;
}
/**
* 模拟数据库或缓存中获取权限数据
* @param userName
* @return
*/
private Set<String> getPermissionsByUserName(String userName) {
Set<String> sets = new HashSet<String>();
sets.add("user:delete");
sets.add("user:add");
return sets;
}
/**
* 模拟从数据库或者缓存中获取角色
* @param userName
* @return
*/
private Set<String> getRolesByUserName(String userName) {
Set<String> sets = new HashSet<String>();
sets.add("admin");
sets.add("user");
return sets;
}
/**
* 模拟数据库查询凭证
* @param userName
* @return
*/
private String getPasswordByUserName(String userName) {
return userMap.get(userName);
}
public static void main(String[] args) {
Md5Hash md5Hash = new Md5Hash("1234567","Mark");
System.out.println(md5Hash.toString());
}
}