shiro加密
使用MD5加密 认证
//自定义的Realm 域 public class CustomRealmSecret extends AuthorizingRealm { @Override //授权 protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { String username = (String) principals.getPrimaryPrincipal(); //通过用户名username,获取角色 Set<String> roles=getRolesByUsername(username); //通过用户名username,获取角色de权限 Set<String> permissions=getPermissionsByUsrname(username); //返回AuthorizationInfo对象,先创建 SimpleAuthorizationInfo info=new SimpleAuthorizationInfo(); //设置授权 ---角色 info.setRoles(roles); //设置授权 ---权限 info.setStringPermissions(permissions); return info; } private Set<String> getPermissionsByUsrname(String username) { //也是从数据库去,这里写死 Set<String> permissions=new HashSet<>(); permissions.add("user:select"); permissions.add("user:update"); permissions.add("user:delete"); permissions.add("user:insert"); return permissions; } private Set<String> getRolesByUsername(String username) { //也是从数据库去,这里写死 Set<String> roles=new HashSet<>(); roles.add("admin"); roles.add("user"); return roles; } @Override //renz protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { String username = (String) token.getPrincipal(); String pwd=getPswByUsername(username); if (pwd==null){ return null; } SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(username,pwd,"customRealmSecret"); return info; } private String getPswByUsername(String username) { Map<String,String> map=new HashMap<>(); //加密的密码admin --->21232f297a57a5a743894a0e4a801fc3 map.put("admin","21232f297a57a5a743894a0e4a801fc3"); super.setName("customRealmSecret"); return map.get(username); } @Test public void MD5Test() { Md5Hash md5Hash=new Md5Hash("admin"); System.out.println(md5Hash); } }
测试类
public class CustomRealmSecretTest { @Test public void testcustomRealmSecret(){ CustomRealmSecret realmSecret=new CustomRealmSecret(); //密码加密 HashedCredentialsMatcher matcher=new HashedCredentialsMatcher(); //使用MD5加密 matcher.setHashAlgorithmName("MD5"); //加密次数 1次 matcher.setHashIterations(1); //自定的域中加入加密 realmSecret.setCredentialsMatcher(matcher); DefaultSecurityManager securityManager=new DefaultSecurityManager(); securityManager.setRealm(realmSecret); SecurityUtils.setSecurityManager(securityManager); Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token=new UsernamePasswordToken("admin","admin"); //认证 subject.login(token); System.out.println(subject.isAuthenticated()); //授权 // subject.checkRoles("admin","user"); // subject.checkPermissions("user:delete","user:update"); } }
使用MD5,再 加盐salt
测试类(其实没有变到,和之前代码一样,变的是自定义realm类,realm添加了盐salt的操作)
public class CustomRealmSecretTest { @Test public void testcustomRealmSecret(){ CustomRealmSecret realmSecret=new CustomRealmSecret(); //密码加密 HashedCredentialsMatcher matcher=new HashedCredentialsMatcher(); //使用MD5加密 matcher.setHashAlgorithmName("MD5"); //加密次数 1次 matcher.setHashIterations(1); //自定的域中加入加密 realmSecret.setCredentialsMatcher(matcher); DefaultSecurityManager securityManager=new DefaultSecurityManager(); securityManager.setRealm(realmSecret); SecurityUtils.setSecurityManager(securityManager); Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token=new UsernamePasswordToken("admin","admin"); //认证 subject.login(token); System.out.println(subject.isAuthenticated()); //授权 // subject.checkRoles("admin","user"); // subject.checkPermissions("user:delete","user:update"); } }
自定义realm//自定义的Realm 域
//自定义的Realm 域 public class CustomRealmSecret extends AuthorizingRealm { @Override //授权 protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { String username = (String) principals.getPrimaryPrincipal(); //通过用户名username,获取角色 Set<String> roles=getRolesByUsername(username); //通过用户名username,获取角色de权限 Set<String> permissions=getPermissionsByUsrname(username); //返回AuthorizationInfo对象,先创建 SimpleAuthorizationInfo info=new SimpleAuthorizationInfo(); //设置授权 ---角色 info.setRoles(roles); //设置授权 ---权限 info.setStringPermissions(permissions); return info; } private Set<String> getPermissionsByUsrname(String username) { //也是从数据库去,这里写死 Set<String> permissions=new HashSet<>(); permissions.add("user:select"); permissions.add("user:update"); permissions.add("user:delete"); permissions.add("user:insert"); return permissions; } private Set<String> getRolesByUsername(String username) { //也是从数据库去,这里写死 Set<String> roles=new HashSet<>(); roles.add("admin"); roles.add("user"); return roles; } @Override //renz protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { String username = (String) token.getPrincipal(); String pwd=getPswByUsername(username); if (pwd==null){ return null; } SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(username,pwd,"customRealmSecret"); info.setCredentialsSalt(ByteSource.Util.bytes("salt")); return info; } private String getPswByUsername(String username) { Map<String,String> map=new HashMap<>(); //加密的密码admin再加盐 --->c657540d5b315892f950ff30e1394480 map.put("admin","c657540d5b315892f950ff30e1394480"); super.setName("customRealmSecret"); return map.get(username); } @Test public void MD5Test() { Md5Hash md5Hash=new Md5Hash("admin","salt"); System.out.println(md5Hash); //c657540d5b315892f950ff30e1394480 } }