Java实现Shiro JWT权限控制与登录验证详解

版权申诉
0 下载量 190 浏览量 更新于2024-07-02 收藏 150KB DOCX 举报
本文档主要介绍了如何在Java项目中使用Apache Shiro和JSON Web Tokens (JWT) 实现权限控制。首先,我们明确了所依赖的版本信息,包括JWT的`com.auth0.java-jwt`库版本3.4.0和Shiro的`org.apache.shiro.spring`版本1.3.2。 Shiro是一个强大的身份与访问管理框架,而JWT则提供了一种轻量级、自包含的方式来安全地传输信息,常用于API的身份验证。文档着重于针对HTTP请求(如GET和POST)的权限控制,指出直接重写Shiro的`isAccessAllowed`方法可能导致对GET/POST请求的过滤鉴权不完全符合官方设计,尽管这在实际应用中影响不大。 在数据库层面,设计了必要的表结构来支持权限管理,包括用户表(sys_amdin)、角色表(sys_role)、用户角色关联表(sys_amdin_role)、权限表(sys_permission)和角色权限关联表(sys_role_permission)。这些表用来存储用户、角色、权限以及它们之间的关系。 接下来,我们关注的是一个工具类`JwtUtilAdmin`,这个类用于处理JWT的生成、加密和解密操作。它从配置文件中获取过期时间,例如accessToken的有效期为2592000秒(30天),refreshToken的有效期为31104000秒(1年),而Shiro缓存的过期时间同样为1年。JWT的加密私钥也作为敏感信息存储在这里,并通过`@Value`注解从外部配置获取。 最后,`JwtUtilAdmin`类包含了JWT认证加密的关键方法,如`generateAccessToken`和`generateRefreshToken`,它们用于生成带有用户信息的JWT令牌,以便后续在API请求头中进行验证。这部分内容体现了将JWT与Shiro集成时的核心逻辑,通过JWT来实现无状态的API访问控制,提高了系统的安全性。 本文档详细介绍了如何使用Java和Shiro结合JWT技术实现RESTful API的权限控制,包括数据模型设计、JWT工具类的实现以及与Shiro的集成,确保了API的安全性和可扩展性。

SLF4J: No SLF4J providers were found. SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details. Exception in thread "main" org.apache.shiro.config.ConfigurationException: Unable to instantiate class [org.apache.shiro.web.mgt.DefaultWebSecurityManager] for object named 'securityManager'. Please ensure you've specified the fully qualified class name correctly. at org.apache.shiro.config.ReflectionBuilder.createNewInstance(ReflectionBuilder.java:309) at org.apache.shiro.config.ReflectionBuilder$InstantiationStatement.doExecute(ReflectionBuilder.java:927) at org.apache.shiro.config.ReflectionBuilder$Statement.execute(ReflectionBuilder.java:887) at org.apache.shiro.config.ReflectionBuilder$BeanConfigurationProcessor.execute(ReflectionBuilder.java:765) at org.apache.shiro.config.ReflectionBuilder.buildObjects(ReflectionBuilder.java:260) at org.apache.shiro.config.IniSecurityManagerFactory.buildInstances(IniSecurityManagerFactory.java:167) at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:130) at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:108) at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:94) at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:46) at org.apache.shiro.config.IniFactorySupport.createInstance(IniFactorySupport.java:123) at org.apache.shiro.util.AbstractFactory.getInstance(AbstractFactory.java:47) at com.xiu.Quickstart.main(Quickstart.java:26) Caused by: org.apache.shiro.util.UnknownClassException: Unable to load class named [org.apache.shiro.web.mgt.DefaultWebSecurityManager] from the thread context, current, or system/application ClassLoaders. All heuristics have been exhausted. Class could not be found. at org.apache.shiro.util.ClassUtils.forName(ClassUtils.java:152) at org.apache.shiro.util.ClassUtils.newInstance(ClassUtils.java:168) at org.apache.shiro.config.ReflectionBuilder.createNewInstance(ReflectionBuilder.java:302) ... 12 more

2023-06-10 上传