Spring Boot 整合 Shiro 非前后端分离实践

需积分: 0 0 下载量 144 浏览量 更新于2024-08-03 收藏 12KB MD 举报
"19掌握整合shrio非前后分离的写法" 在本文档中,我们将探讨如何在Spring Boot项目中整合Shiro框架进行权限管理,而不采用前后端分离的架构。Shiro是一个强大的且易用的安全框架,可以处理认证、授权、会话管理和加密等任务。以下是详细的整合步骤: ## 1. 创建项目 如描述所示,首先创建一个Spring Boot项目。图片链接指向的可能是一个示例的新建项目截图,但由于无法查看实际图片,这里假设已按照标准流程创建了一个新的Spring Boot项目。 ## 2. 添加依赖 在`pom.xml`文件中,我们需要引入Spring Boot的父依赖以及Shiro的核心库。以下是一段示例代码,展示了如何添加这些依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version> </dependency> <!-- 其他可能的依赖,例如数据库驱动 --> </dependencies> ``` 确保版本号与描述中的`<shiro.version>`相匹配。 ## 3. 配置Shiro 在`application.properties`或`application.yml`中,我们可以配置Shiro的基本设置,例如 Realm(权限域)和缓存管理等。例如: ```properties # application.properties 示例 shiro.loginUrl=/login shiro.successUrl=/index shiro.unauthorizedUrl=/unauthorized ``` ## 4. 创建Realm Realm是Shiro的核心组件,用于处理认证和授权。我们需要创建一个自定义的Realm,继承自`AuthorizingRealm`,并实现其`doGetAuthenticationInfo`和`doGetAuthorizationInfo`方法,以完成对用户身份和权限的验证。 ```java @Service public class CustomRealm extends AuthorizingRealm { @Autowired private UserService userService; // 实现认证逻辑 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // 从token中获取用户名,然后通过userService查找用户 // ... } // 实现授权逻辑 @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { // 从principals中获取用户名,然后查找用户的角色和权限 // ... } } ``` ## 5. 配置Shiro过滤器 在Spring Boot中,我们可以通过`ShiroFilterFactoryBean`来配置Shiro的过滤器链。在`WebSecurityConfig`或自定义配置类中添加以下代码: ```java @Configuration public class ShiroConfig { @Autowired private CustomRealm customRealm; @Bean public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) { ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean(); factoryBean.setSecurityManager(securityManager); // 设置过滤器链 Map<String, Filter> filters = new HashMap<>(); filters.put("authc", FormAuthenticationFilter.class); factoryBean.setFilters(filters); // 定义URL过滤规则 Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>(); filterChainDefinitionMap.put("/", "anon"); filterChainDefinitionMap.put("/login", "authc"); filterChainDefinitionMap.put("/logout", "logout"); filterChainDefinitionMap.put("/", "authc"); factoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return factoryBean; } @Bean public SecurityManager securityManager() { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(customRealm); return securityManager; } } ``` ## 6. 编写登录、登出及权限控制逻辑 创建Controller处理登录请求,比如`/login`,并实现用户的登录、登出功能。同时,Shiro会在每个请求之前自动调用过滤器链,执行权限检查。 ```java @RestController @RequestMapping("/api/auth") public class AuthController { @PostMapping("/login") public Result login(@RequestBody LoginRequest loginRequest) { // 使用Shiro的Subject进行登录操作 // ... } @GetMapping("/logout") public Result logout() { // 获取Subject并调用logout方法 // ... } } ``` ## 7. 集成Spring Security 虽然Shiro提供了一套完整的安全解决方案,但在某些情况下,可能还需要集成Spring Security来增强安全性。例如,Spring Security提供了更丰富的API和更细粒度的控制,如CSRF防护、跨站请求伪造防护等。 ## 8. 测试与调试 完成上述步骤后,运行项目并尝试进行登录、登出操作,以及访问受保护的资源,确保Shiro的认证和授权功能正常工作。 整合Shiro非前后分离的写法主要涉及项目创建、依赖引入、配置Shiro、创建Realm、配置过滤器、编写登录登出逻辑以及可能的Spring Security集成。理解并掌握这些步骤,可以帮助我们在Spring Boot应用中有效地实现权限管理。

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 上传