Skip to content

Commit

Permalink
🔧 : configurable admin password
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Jul 24, 2019
1 parent 1e5ff14 commit cab52cc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
</dependency>

<dependency>
<groupId>com.github.spullara.mustache.java</groupId>
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/codeka/gaia/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${gaia.ldap.enabled:false}")
private boolean ldapEnabled;

@Value("${gaia.admin-password:admin123}")
private String adminPassword;

@Bean
PasswordEncoder bcrypt(){
return new BCryptPasswordEncoder();
Expand All @@ -46,7 +49,7 @@ public void configure(AuthenticationManagerBuilder auth) throws Exception {
// configure default admin user
auth
.inMemoryAuthentication()
.withUser("admin").password(bcrypt().encode("admin123")).authorities("ROLE_ADMIN")
.withUser("admin").password(bcrypt().encode(adminPassword)).authorities("ROLE_ADMIN")
.and()
.withUser("user").password(bcrypt().encode("user123")).authorities("ROLE_USER");

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ management.health.ldap.enabled=${gaia.ldap.enabled:false}

spring.data.mongodb.uri=${gaia.mongodb.uri}

gaia.mongodb.uri=mongodb://localhost/gaia
gaia.mongodb.uri=mongodb://localhost/gaia

31 changes: 31 additions & 0 deletions src/test/java/io/codeka/gaia/config/SecurityConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.codeka.gaia.config;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;

@SpringBootTest(classes = {EmptyController.class, SecurityConfig.class})
@AutoConfigureMockMvc
@TestPropertySource(properties = "gaia.admin-password=admin456")
class SecurityConfigTest {

@Autowired
private MockMvc mockMvc;

@Test
void adminUserPassword_shouldBeConfigurable() throws Exception {
mockMvc.perform(formLogin().user("admin").password("admin456"))
.andExpect(authenticated().withUsername("admin"));
}

}

@Controller
class EmptyController{}

0 comments on commit cab52cc

Please sign in to comment.