更新starter初始化顺序

This commit is contained in:
mazhicheng 2020-01-20 18:14:38 +08:00
parent ba38e3c2a3
commit b14b05358f
6 changed files with 71 additions and 89 deletions

View File

@ -6,7 +6,6 @@ import com.diboot.core.util.V;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.config.Cons;
import com.diboot.iam.service.IamPermissionService;
import com.diboot.iam.starter.IamBaseProperties;
import com.diboot.iam.util.AnnotationUtils;
import com.diboot.iam.vo.PermissionVO;
import lombok.extern.slf4j.Slf4j;
@ -34,16 +33,13 @@ public class AnnotationExtractor {
@Autowired
private IamPermissionService iamPermissionService;
@Autowired
private IamBaseProperties iamBaseProperties;
/**
* 更新permissions
*/
public void updatePermissions(){
public void updatePermissions(boolean isEnablePermissionUpdate){
// 提取到的全部注解必须先执行该初始化确保url和permissionCode被加载
List<PermissionVO> voListInCode = AnnotationExtractor.extractAllPermissions();
if(!iamBaseProperties.isEnablePermissionUpdate()){
List<PermissionVO> voListInCode = extractAllPermissions();
if(!isEnablePermissionUpdate){
return;
}
log.info("diboot-iam 开始异步更新权限 ...");

View File

@ -3,20 +3,25 @@ package com.diboot.iam.starter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.diboot.core.service.impl.DictionaryServiceImpl;
import com.diboot.core.util.D;
import com.diboot.core.util.DateConverter;
import com.diboot.core.util.V;
import com.diboot.iam.annotation.process.AnnotationExtractor;
import com.diboot.iam.config.Cons;
import com.diboot.iam.jwt.BaseJwtRealm;
import com.diboot.iam.jwt.DefaultJwtAuthFilter;
import com.diboot.iam.service.impl.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.mgt.SessionsSecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@ -24,6 +29,7 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
@ -48,13 +54,29 @@ import java.util.Map;
@EnableAsync
@EnableConfigurationProperties({IamBaseProperties.class})
@ComponentScan(basePackages = {"com.diboot.iam"})
@AutoConfigureAfter(value = ShiroProxyConfig.class)
@Order(3)
@MapperScan(basePackages={"com.diboot.iam.mapper"})
@AutoConfigureAfter(value = {DictionaryServiceImpl.class,
IamRoleServiceImpl.class, IamUserServiceImpl.class, IamUserRoleServiceImpl.class, IamAccountServiceImpl.class, IamPermissionServiceImpl.class,
IamBaseInitializer.class, AnnotationExtractor.class, ShiroProxyConfig.class})
@Order(2)
public class IamBaseAutoConfig implements WebMvcConfigurer {
@Autowired
Environment environment;
@Autowired
IamBaseProperties iamBaseProperties;
@Bean
@ConditionalOnMissingBean(IamBasePluginManager.class)
public IamBasePluginManager iamBasePluginManager(){
log.info("开始初始化IamBasePluginManager");
IamBasePluginManager pluginManager = new IamBasePluginManager();
pluginManager.initPlugin(iamBaseProperties);
log.info("IamBasePluginManager初始化完成");
return pluginManager;
}
/**
* 根据用户配置的缓存类初始化CacheManager默认为Shiro内存缓存MemoryConstrainedCacheManager
* @return

View File

@ -1,10 +1,8 @@
package com.diboot.iam.starter;
import com.diboot.core.service.DictionaryService;
import com.diboot.core.starter.SqlHandler;
import com.diboot.core.util.JSON;
import com.diboot.core.vo.DictionaryVO;
import com.diboot.iam.annotation.process.AnnotationExtractor;
import com.diboot.iam.config.Cons;
import com.diboot.iam.entity.IamAccount;
import com.diboot.iam.entity.IamRole;
@ -14,27 +12,20 @@ import com.diboot.iam.service.IamAccountService;
import com.diboot.iam.service.IamRoleService;
import com.diboot.iam.service.IamUserRoleService;
import com.diboot.iam.service.IamUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
/**
* IAM相关的初始化Listener
* IAM组件相关的初始化
* @author mazc@dibo.ltd
* @version v2.0
* @date 2019/12/23
*/
@Component
public class IamBaseInitListener implements ApplicationListener<ContextRefreshedEvent> {
@Slf4j
public class IamBaseInitializer{
@Autowired
private AnnotationExtractor annotationExtractor;
@Autowired
private IamBaseProperties iamBaseProperties;
@Autowired
private Environment environment;
@Autowired
private DictionaryService dictionaryService;
@Autowired
@ -43,39 +34,13 @@ public class IamBaseInitListener implements ApplicationListener<ContextRefreshed
private IamUserService iamUserService;
@Autowired
private IamUserRoleService iamUserRoleService;
@Autowired
private IamAccountService iamAccountService;
// 验证SQL
private static final String VALIDATE_SQL = "SELECT id FROM ${SCHEMA}.iam_permission WHERE id=0";
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
// 初始化数据库表
initTableStructureAndData();
// 异步更新注解
annotationExtractor.updatePermissions();
}
/**
* 初始化表结构及数据
*/
private void initTableStructureAndData(){
// 检查数据库字典是否已存在
if(iamBaseProperties.isInitSql() && SqlHandler.checkIsTableExists(VALIDATE_SQL) == false){
// 执行初始化SQL
SqlHandler.initBootstrapSql(this.getClass(), environment, "iam-base");
// 插入相关数据DictRole等
insertInitData();
}
}
/**
* 插入初始化数据
*/
private void insertInitData(){
public void insertInitData(){
// 插入iam-base所需的数据字典
String[] DICT_INIT_DATA = {
"{\"type\":\"AUTH_TYPE\", \"itemName\":\"登录认证方式\", \"description\":\"IAM用户登录认证方式\", \"children\":[{\"itemName\":\"用户名密码\", \"itemValue\":\"PWD\", \"sortId\":1},{\"itemName\":\"公众号\", \"itemValue\":\"WX_MP\", \"sortId\":2},{\"itemName\":\"企业微信\", \"itemValue\":\"WX_CP\", \"sortId\":3}]}",

View File

@ -0,0 +1,39 @@
package com.diboot.iam.starter;
import com.diboot.core.plugin.PluginManager;
import com.diboot.core.starter.SqlHandler;
import com.diboot.core.util.ContextHelper;
import com.diboot.iam.annotation.process.AnnotationExtractor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
/**
* IAM组件相关的初始化
* @author mazc@dibo.ltd
* @version v2.0
* @date 2019/12/23
*/
@Slf4j
public class IamBasePluginManager implements PluginManager {
// 验证SQL
private static final String VALIDATE_SQL = "SELECT id FROM ${SCHEMA}.iam_permission WHERE id=0";
public void initPlugin(IamBaseProperties iamBaseProperties){
// 检查数据库字典是否已存在
if(iamBaseProperties.isInitSql()){
Environment environment = ContextHelper.getApplicationContext().getEnvironment();
SqlHandler.init(environment);
if(SqlHandler.checkIsTableExists(VALIDATE_SQL) == false){
log.info("执行初始化SQL");
// 执行初始化SQL
SqlHandler.initBootstrapSql(this.getClass(), environment, "iam-base");
// 插入相关数据DictRole等
ContextHelper.getBean(IamBaseInitializer.class).insertInitData();
log.info("初始化SQL执行完成");
}
}
// 异步更新注解
ContextHelper.getBean(AnnotationExtractor.class).updatePermissions(iamBaseProperties.isEnablePermissionUpdate());
}
}

View File

@ -4,7 +4,6 @@ import lombok.Getter;
import lombok.Setter;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 认证相关的配置参数
@ -12,7 +11,6 @@ import org.springframework.stereotype.Component;
* @version v2.0
* @date 2019/12/23
*/
@Component
@Getter @Setter
@ConfigurationProperties(prefix = "diboot.iam")
public class IamBaseProperties {

View File

@ -1,38 +0,0 @@
package com.diboot.iam.starter;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Mybatis-Plus配置
* @author mazc@dibo.ltd
* @version v2.0
* @date 2019/1/19
*/
@Configuration
@MapperScan(basePackages={"com.diboot.iam.mapper"})
public class IamMybatisPlusConfig {
/**
* Mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
/***
* 逻辑删除注入
* @return
*/
@Bean
public ISqlInjector sqlInjector() {
return new DefaultSqlInjector();
}
}