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,6 +25,7 @@ import org.springframework.security.crypto.password.PasswordEncoder;
public class SecurityConfig extends WebSecurityConfigurerAdapter { public class SecurityConfig extends WebSecurityConfigurerAdapter {
private static final String ROLE_PREFIX = "ROLE_"; private static final String ROLE_PREFIX = "ROLE_";
public static final String DEFAULT_PASSWORD = "password"; public static final String DEFAULT_PASSWORD = "password";
@Bean @Bean
static PasswordEncoder bCryptPasswordEncoder() { static PasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder(10); return new BCryptPasswordEncoder(10);
@ -33,12 +34,11 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean @Bean
UserDetailsService customUserDetailsService() { UserDetailsService customUserDetailsService() {
return new UserDetailsService() { return new UserDetailsService() {
@Override @Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//authenticate and return dummy user // authenticate, grant ADMIN role and return dummy user
Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>(); Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
authorities.add(new SimpleGrantedAuthority(ROLE_PREFIX + username)); authorities.add(new SimpleGrantedAuthority(ROLE_PREFIX + "ADMIN"));
return new User(username, bCryptPasswordEncoder().encode(DEFAULT_PASSWORD), authorities); return new User(username, bCryptPasswordEncoder().encode(DEFAULT_PASSWORD), authorities);
} }
}; };
@ -46,7 +46,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 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 @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http.csrf(); http.csrf();
http.headers().frameOptions().sameOrigin(); http.headers()
.frameOptions()
.sameOrigin();
http.antMatcher("/**").userDetailsService(customUserDetailsService()) http.antMatcher("/**")
.userDetailsService(customUserDetailsService())
.authorizeRequests() .authorizeRequests()
.antMatchers("/**").permitAll() .antMatchers("/**")
.permitAll()
.and() .and()
.httpBasic(); .httpBasic();
} }

View File

@ -19,7 +19,7 @@ public class HomeControllerTest {
@Test @Test
public void home() throws Exception { 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) .getForEntity("/", String.class)
.getBody(); .getBody();
System.out.println(body); System.out.println(body);
@ -31,7 +31,7 @@ public class HomeControllerTest {
assertTrue(body.contains("ADMIN ROLE")); assertTrue(body.contains("ADMIN ROLE"));
// test <sec:authentication property="principal.username" /> // test <sec:authentication property="principal.username" />
assertTrue(body.contains("principal.username: ADMIN")); assertTrue(body.contains("principal.username: testUser"));
// test <sec:csrfInput /> // test <sec:csrfInput />
assertTrue(body.contains("<input type=\"hidden\" name=\"_csrf\" value=\"")); assertTrue(body.contains("<input type=\"hidden\" name=\"_csrf\" value=\""));