roles; }"> roles; }"> roles; }">
@Entity
@Getter @Setter @EqualsAndHashCode(of = "id")
@Builder @NoArgsConstructor @AllArgsConstructor
public class Account {
@Id @GeneratedValue
private Integer id;
@Column(unique = true)
private String email;
private String password;
@ElementCollection(fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
private Set<AccountRole> roles;
}
unique 추가
spring.jackson.deserialization.fail-on-unknown-properties=true
spring.datasource.url=jdbc:mariadb://localhost:3306/springjpa
spring.datasource.username=cmlee
spring.datasource.password=pass
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.springframework.security=DEBUG
[email protected]
my-app.admin-password=admin
[email protected]
my-app.user-password=user
my-app.client-id=myApp
my-app.client-secret=pass
@Component
@ConfigurationProperties("my-app")
@Getter @Setter
public class AppProperties {
@NotEmpty
private String adminUsername;
@NotEmpty
private String adminPassword;
@NotEmpty
private String userUsername;
@NotEmpty
private String userPassword;
@NotEmpty
private String clientId;
@NotEmpty
private String clientSecret;
}
스프링 부트가 이곳으로 자동 바인딩 해준다.
@Configuration
public class AppConfig {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
@Bean
public ApplicationRunner applicationRunner() {
return new ApplicationRunner() {
@Autowired
AccountService accountService;
@Autowired
AppProperties appProperties;
@Override
public void run(ApplicationArguments args) throws Exception {
Account admin = Account.builder()
.email(appProperties.getAdminUsername())
.password(appProperties.getAdminPassword())
.roles(new HashSet<>(Arrays.asList(AccountRole.ADMIN, AccountRole.USER)))
.build();
Account user = Account.builder()
.email(appProperties.getUserUsername())
.password(appProperties.getUserPassword())
.roles(new HashSet<>(Collections.singletonList(AccountRole.USER)))
.build();
accountService.saveAccount(admin);
accountService.saveAccount(user);
}
};
}
}
받아 사용한다.
전부 변경