add more test
This commit is contained in:
parent
4e4d11574a
commit
7b2dec656d
@ -25,28 +25,29 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
private static final String ROLE_PREFIX = "ROLE_";
|
||||
public static final String DEFAULT_PASSWORD = "password";
|
||||
|
||||
@Bean
|
||||
static PasswordEncoder bCryptPasswordEncoder() {
|
||||
return new BCryptPasswordEncoder(10);
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
UserDetailsService customUserDetailsService() {
|
||||
return new UserDetailsService() {
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
//authenticate and return dummy user
|
||||
Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
|
||||
authorities.add(new SimpleGrantedAuthority(ROLE_PREFIX + username));
|
||||
return new User(username, bCryptPasswordEncoder().encode(DEFAULT_PASSWORD), authorities);
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
// authenticate, grant ADMIN role and return dummy user
|
||||
Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
|
||||
authorities.add(new SimpleGrantedAuthority(ROLE_PREFIX + "ADMIN"));
|
||||
return new User(username, bCryptPasswordEncoder().encode(DEFAULT_PASSWORD), authorities);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.userDetailsService(customUserDetailsService()).passwordEncoder(bCryptPasswordEncoder());
|
||||
auth.userDetailsService(customUserDetailsService())
|
||||
.passwordEncoder(bCryptPasswordEncoder());
|
||||
|
||||
}
|
||||
|
||||
@ -59,11 +60,15 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.csrf();
|
||||
http.headers().frameOptions().sameOrigin();
|
||||
|
||||
http.antMatcher("/**").userDetailsService(customUserDetailsService())
|
||||
http.headers()
|
||||
.frameOptions()
|
||||
.sameOrigin();
|
||||
|
||||
http.antMatcher("/**")
|
||||
.userDetailsService(customUserDetailsService())
|
||||
.authorizeRequests()
|
||||
.antMatchers("/**").permitAll()
|
||||
.antMatchers("/**")
|
||||
.permitAll()
|
||||
.and()
|
||||
.httpBasic();
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ public class HomeControllerTest {
|
||||
|
||||
@Test
|
||||
public void home() throws Exception {
|
||||
String body = this.restTemplate.withBasicAuth("ADMIN", SecurityConfig.DEFAULT_PASSWORD)
|
||||
String body = this.restTemplate.withBasicAuth("testUser", SecurityConfig.DEFAULT_PASSWORD)
|
||||
.getForEntity("/", String.class)
|
||||
.getBody();
|
||||
System.out.println(body);
|
||||
@ -31,7 +31,7 @@ public class HomeControllerTest {
|
||||
assertTrue(body.contains("ADMIN ROLE"));
|
||||
|
||||
// test <sec:authentication property="principal.username" />
|
||||
assertTrue(body.contains("principal.username: ADMIN"));
|
||||
assertTrue(body.contains("principal.username: testUser"));
|
||||
|
||||
// test <sec:csrfInput />
|
||||
assertTrue(body.contains("<input type=\"hidden\" name=\"_csrf\" value=\""));
|
||||
|
Loading…
x
Reference in New Issue
Block a user