Skip to content

shiro-spring-boot 整合shiro可以在spring-boot环境中的使用,只要在springboot配置文件中添加关于shiro权限的相关配置即可完成shiro与spring boot完美整合

License

SiGuiyang/shiro-spring-boot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

shiro-spring-boot

JDK版本在1.8以上

只需简单几个步骤就可以完成与spring-boot无缝对接

引入项目maven依赖,不需要引入shiro-spring 工程,本项目已经将shiro-spring 的关键内容重新拷贝,后期将逐步优化核心代码逻辑

	<dependency>
	    <groupId>quick.pager</groupId>
	    <artifactId>shiro-spring-boot-starter</artifactId>
	    <version>1.0.0</version>
	</dependency>

使用shiro-spring-boot方法

  1. 在spring boot启动类中添加@EnableShiroConfiguration 注解,启用shiro权限框架

    例如:

        @SpringBootApplication
        @EnableShiroConfiguration
        public class ShiroSimpleApplication {
        
            public static void main(String[] args) {
                SpringApplication.run(ShiroSimpleApplication.class, args);
            }
        
        }
  1. 编写权限控制xxxRealm extends AuthorizingRealm 类,用于shiro的登陆与权限验证管理

    例如:

        public class UserRealm extends AuthorizingRealm {
        
            @Override
            protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
                SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
                List<String> permission = new ArrayList<>();
                List<String> roles = new ArrayList<>();
                String username = (String) principals.getPrimaryPrincipal();
                if ("admin".equals(username)) {
                    roles.add("admin");
                    permission.add("admin:other");
                    permission.add("admin:success");
                } else {
                    roles.add("sp");
                    permission.add("admin:role:success");
                    permission.add("admin:role:sp");
                }
        
                info.addRoles(roles);
                info.addStringPermissions(permission);
        
                return info;
            }
        
            @Override
            protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        
                String password = new String((char[]) token.getCredentials());
                String username = (String) token.getPrincipal();
        
                SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, password, this.getName());
        
                return info;
            }
        }
  1. 在spring boot 资源文件application.yml 添加shiro相关配置信息,shiro-spring-boot 的资源前缀时 spring.shiro

    例如:

        spring:
          shiro:
            realm-class: quick.pager.shiro.boot.simple.shiro.UserRealm
            login-url: '/admin/login'
            success-url: '/admin/success'
            unauthorized-url: '/admin/404'
            filter-chain-definition-map: {'[/admin/login/page]':'anon','[/admin/login]':'anon','[/admin/logout]':'anon','[/admin/role/**]':'authc,roles[sp]','[/admin/**]':'authc,roles[admin]'}
            proxy-target-class: true 

更多配置信息请参考 ShiroProperties.java

具体可以参考shiro-spring-simpleDemo 工程,该Demo以结合Shiro权限的配置设置,可以在aop SubjectAspect 类查看是否具有的权限与路由

About

shiro-spring-boot 整合shiro可以在spring-boot环境中的使用,只要在springboot配置文件中添加关于shiro权限的相关配置即可完成shiro与spring boot完美整合

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published