Shiro六(与Spring Boot集成)

    xiaoxiao2023-11-19  153

    Shiro—与Spring Boot集成

    一.创建maven工程并加入依赖包:

    <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot-web-starter</artifactId> <version>1.5.0-SNAPSHOT</version> </dependency>

    二. 编写Realm实现:

    @Bean public Realm realm() { ... }

    三. 编写ShiroFilterChainDefinition

    @Bean public ShiroFilterChainDefinition shiroFilterChainDefinition() { DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition(); // logged in users with the 'admin' role chainDefinition.addPathDefinition("/admin/**", "authc, roles[admin]"); // logged in users with the 'document:read' permission chainDefinition.addPathDefinition("/docs/**", "authc, perms[document:read]"); // all other paths require a logged in user chainDefinition.addPathDefinition("/**", "authc"); return chainDefinition; }

    四. 启用S​​hiro注解

    @RequiresPermissions("document:read") public void readDocument() { ... }

    @Controller public class AccountInfoController { @RequiresRoles("admin") @RequestMapping("/admin/config") public String adminConfig(Model model) { return "view"; } }
    最新回复(0)