Shiro加密
CustomRealm.java
package com.exp.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.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class CustomRealm extends AuthorizingRealm { Map<String,String> usermap = new HashMap<String, String>(); { usermap.put("exp","ca0937264d2b52158717c3bdcada675e"); // <- 123456 to md5 + salt 'exp' super.setName("customRealm"); } protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { String userName = (String) principals.getPrimaryPrincipal(); //从数据库或者缓存中获取角色数据 Set<String> roles = getRolesByUserName(userName); Set<String> permissions = getPermissionByUserName(userName); SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); simpleAuthorizationInfo.setStringPermissions(permissions); simpleAuthorizationInfo.setRoles(roles); return simpleAuthorizationInfo; } private Set<String> getPermissionByUserName(String userName){ Set<String> sets = new HashSet<String>(); sets.add("user:del"); sets.add("user:add"); return sets; } private Set<String> getRolesByUserName(String userName){ Set<String> sets = new HashSet<String>(); sets.add("admin"); sets.add("user"); return sets; } protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // 1.从主体传过来的认证信息中,获得用户名 String userName = (String) token.getPrincipal(); // 2.通过用户名到数据库中获取凭证 String password = getPasswordByUserName(userName); if(password == null){ return null; } SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo("exp",password,"customRealm"); authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("exp"));// 自导自演??? return authenticationInfo; } /** * 模拟数据库查询凭证 * @param userName * @return */ private String getPasswordByUserName(String userName){ return usermap.get(userName); } public static void main(String[] args) { Md5Hash md5Hash = new Md5Hash("123456","exp"); System.out.println(md5Hash.toString()); } }
CustomRealmTest.java
package com.exp.test; import com.alibaba.druid.pool.DruidDataSource; import com.exp.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.realm.jdbc.JdbcRealm; import org.apache.shiro.subject.Subject; import org.junit.Test; public class CustomRealmTest { @Test public void Authenication(){ CustomRealm customRealm = new CustomRealm(); //1. 构建SecurityManager环境 DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager(); defaultSecurityManager.setRealm(customRealm); //加密配置 HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(); matcher.setHashAlgorithmName("md5");// md5方式加密 matcher.setHashIterations(1);// 加密次数设置 customRealm.setCredentialsMatcher(matcher); //2.主体提交认证请求 SecurityUtils.setSecurityManager(defaultSecurityManager); Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken("exp","123456"); //用户登录认证 subject.login(token); System.out.println("expworld=>"+ subject.isAuthenticated()); //expworld=>true subject.checkRole("user"); subject.checkPermission("user:add"); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?