fix: 修正

This commit is contained in:
wangyu 2021-01-11 21:55:57 +08:00
parent c08dc11e05
commit 13e1180230
2 changed files with 54 additions and 45 deletions

View File

@ -4,10 +4,19 @@ import com.flyfish.framework.config.properties.JwtProperties;
import com.flyfish.framework.config.properties.SecurityProperties;
import com.flyfish.framework.configuration.jwt.JwtSecurityContextRepository;
import com.flyfish.framework.configuration.jwt.TokenProvider;
import com.flyfish.framework.domain.UserQo;
import com.flyfish.framework.domain.po.Role;
import com.flyfish.framework.domain.po.User;
import com.flyfish.framework.enums.RoleType;
import com.flyfish.framework.enums.UserStatus;
import com.flyfish.framework.enums.UserType;
import com.flyfish.framework.handler.JsonAuthenticationFailureHandler;
import com.flyfish.framework.handler.JsonAuthenticationSuccessHandler;
import com.flyfish.framework.handler.JsonLogoutSuccessHandler;
import com.flyfish.framework.initializer.UserInitializer;
import com.flyfish.framework.service.UserService;
import com.flyfish.framework.transform.ResultDataTransformer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@ -19,7 +28,6 @@ import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.server.authentication.HttpStatusServerEntryPoint;
@ -27,6 +35,8 @@ import org.springframework.security.web.server.context.ServerSecurityContextRepo
import org.springframework.security.web.server.context.WebSessionServerSecurityContextRepository;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers;
import java.util.Arrays;
/**
* @author wangyu
*/
@ -106,4 +116,34 @@ public class WebSecurityConfig {
.csrf().disable();
return http.build();
}
/**
* 默认的初始化
*
* @param userService 用户服务
* @param passwordEncoder 密码
* @return 结果
*/
@Bean
@ConditionalOnMissingBean(UserInitializer.class)
public UserInitializer userInitializer(UserService userService, PasswordEncoder passwordEncoder) {
return () -> {
UserQo qo = new UserQo();
qo.setUserType(UserType.SUPER_ADMIN.name());
if (userService.count(qo) == 0) {
// 初始化用户
User user = new User();
user.setUsername("admin");
user.setPassword(passwordEncoder.encode("admin123456"));
user.setUserType(UserType.SUPER_ADMIN);
user.setEnable(true);
user.setApp(false);
user.setUserStatus(UserStatus.NORMAL);
user.setPhone("10000000000");
user.setName("超级管理员");
user.setCode("Administrator");
userService.createSelective(user);
}
};
}
}

View File

@ -1,55 +1,24 @@
package com.flyfish.framework.initializer;
import com.flyfish.framework.domain.UserQo;
import com.flyfish.framework.domain.po.Role;
import com.flyfish.framework.domain.po.User;
import com.flyfish.framework.enums.RoleType;
import com.flyfish.framework.enums.UserStatus;
import com.flyfish.framework.enums.UserType;
import com.flyfish.framework.service.UserService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Arrays;
import org.springframework.beans.factory.InitializingBean;
/**
* 用户初始化器可以重写
*/
@Component
public class UserInitializer {
@Resource
private UserService userService;
@Resource
private PasswordEncoder passwordEncoder;
public interface UserInitializer extends InitializingBean {
/**
* 生成超级管理员
* 初始化
*/
@PostConstruct
private void init() {
UserQo qo = new UserQo();
qo.setUserType(UserType.SUPER_ADMIN.name());
if (userService.count(qo) == 0) {
User user = new User();
user.setUsername("admin");
user.setPassword(passwordEncoder.encode("admin"));
user.setUserType(UserType.SUPER_ADMIN);
user.setEnable(true);
user.setApp(false);
user.setUserStatus(UserStatus.NORMAL);
user.setRoles(Arrays.asList(Role.builder()
.type(RoleType.PC)
.admin(true)
.description("超级管理员")
.permissions(Arrays.asList())
.build()));
user.setPhone("10000000000");
user.setName("超级管理员");
user.setCode("Administrator");
userService.createSelective(user);
}
void initialize();
/**
* 间接初始化
*
* @throws Exception 异常
*/
@Override
default void afterPropertiesSet() throws Exception {
initialize();
}
}