add more test

This commit is contained in:
nnhai1991@gmail.com 2018-08-11 16:15:49 +08:00
parent 4e4d11574a
commit 7b2dec656d
2 changed files with 22 additions and 17 deletions

View File

@ -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();
}

View File

@ -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=\""));