|
@@ -15,15 +15,21 @@
|
|
|
*/
|
|
|
package org.springframework.security.test.web.servlet.showcase.secured;
|
|
|
|
|
|
+import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
|
|
|
+import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
|
|
|
+import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
|
|
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
+
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
import org.junit.runner.RunWith;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
-import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
|
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
|
+import org.springframework.security.test.context.support.WithAnonymousUser;
|
|
|
import org.springframework.security.test.context.support.WithMockUser;
|
|
|
import org.springframework.test.context.ContextConfiguration;
|
|
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
@@ -33,11 +39,6 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
import org.springframework.web.context.WebApplicationContext;
|
|
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
|
|
|
|
-import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
|
|
|
-import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
|
|
|
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
-
|
|
|
@RunWith(SpringJUnit4ClassRunner.class)
|
|
|
@ContextConfiguration(classes = WithUserClassLevelAuthenticationTests.Config.class)
|
|
|
@WebAppConfiguration
|
|
@@ -72,6 +73,16 @@ public class WithUserClassLevelAuthenticationTests {
|
|
|
.andExpect(authenticated().withUsername("user").withRoles("ADMIN"));
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ @WithAnonymousUser
|
|
|
+ public void requestProtectedUrlWithAnonymous() throws Exception {
|
|
|
+ mvc.perform(get("/"))
|
|
|
+ // Ensure did not get past security
|
|
|
+ .andExpect(status().isUnauthorized())
|
|
|
+ // Ensure not authenticated
|
|
|
+ .andExpect(unauthenticated());
|
|
|
+ }
|
|
|
+
|
|
|
@EnableWebSecurity
|
|
|
@EnableWebMvc
|
|
|
static class Config extends WebSecurityConfigurerAdapter {
|
|
@@ -84,7 +95,7 @@ public class WithUserClassLevelAuthenticationTests {
|
|
|
.antMatchers("/admin/**").hasRole("ADMIN")
|
|
|
.anyRequest().authenticated()
|
|
|
.and()
|
|
|
- .formLogin();
|
|
|
+ .httpBasic();
|
|
|
}
|
|
|
// @formatter:on
|
|
|
|