shiro springboot3
时间: 2023-09-09 21:06:53 浏览: 109
根据引用,创建一个Spring Boot项目并引入Shiro依赖。使用以下代码创建一个配置类来配置Shiro环境。在该配置类中,您可以定义Shiro的各种配置项,例如Realm、Session管理器等。
```java
@Configuration
public class ShiroConfig {
// 配置Shiro的安全管理器
@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(myRealm());
return securityManager;
}
// 自定义的Realm
@Bean
public MyRealm myRealm() {
return new MyRealm();
}
// 配置Shiro的过滤器链
@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
chainDefinition.addPathDefinition("/user/logout", "logout");
chainDefinition.addPathDefinition("/**", "authc");
return chainDefinition;
}
// 配置Shiro的注解支持
@Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
autoProxyCreator.setProxyTargetClass(true);
return autoProxyCreator;
}
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
advisor.setSecurityManager(securityManager);
return advisor;
}
}
```
根据引用,开发一个UserController,并在其中定义一个logout()方法,用于退出登录。在该方法中,首先获取当前用户的Subject对象,然后调用subject.logout()方法退出登录。最后,使用重定向将用户重定向到登录页面。
```java
@Controller
@RequestMapping("user")
public class UserController {
@RequestMapping("logout")
public String logout(){
Subject subject = SecurityUtils.getSubject();
subject.logout();//退出用户
return "redirect:/login.jsp";
}
}
```
根据引用,根据不同的Spring Boot版本,Shiro的请求处理方式可能会有所不同。在Spring Boot 2.7.10中,请求会正常进入Shiro的过滤器中(org.apache.shiro.web.servlet.OncePerRequestFilter)进行处理。
希望这些信息能对您有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Shiro简单配置Springboot版(3)](https://blog.csdn.net/qq_39390455/article/details/109214894)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [SpringBoot3整合Shiro未生效问题](https://blog.csdn.net/banmao999/article/details/131420519)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文