Springboot整合shiro
导入依赖
<!-- shiro-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0-RC2</version>
</dependency>
编写配置类
Subject:用户主体
SecutityManager:安全管理器
Realm:Shiro连接数据的桥梁
@Configuration
public class ShiroConfig {
// 创建ShiroFilterFactoryBean 需要关联DefaultWebSecurityManager //@Qualifier 是为了找到下面定义的securityManager
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
// 设置安全管理器
shiroFilterFactoryBean.setSecurityManager(securityManager);
return shiroFilterFactoryBean;
}
// 创建DefaultWebSecurityManager 需要关联Realm
@Bean(name = "securityManager")
public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){ //@Qualifier 是为了找到下面定义的Realm
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(userRealm);
return securityManager;
}
// 创建Realm Realm是自定义的
@Bean(name = "userRealm")
public UserRealm getRealm(){
return new UserRealm();
}
}
自定义Realm
public class UserRealm extends AuthorizingRealm {
/*
* 执行授权逻辑
* */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("执行授权逻辑");
return null;
}
/*
* 执行认证逻辑
* */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("执行认证逻辑");
return null;
}
}