这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.baeldung.security.csrf;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.baeldung.security.spring.SecurityWithCsrfCookieConfig;
import com.baeldung.spring.MvcConfig;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(classes = { SecurityWithCsrfCookieConfig.class, MvcConfig.class })
public class CsrfCookieEnabledIntegrationTest extends CsrfAbstractIntegrationTest {

@Test
public void givenNoCsrf_whenAddFoo_thenForbidden() throws Exception {
// @formatter:off
mvc
.perform(post("/auth/foos")
.contentType(MediaType.APPLICATION_JSON)
.content(createFoo())
.with(testUser()))
.andExpect(status().isForbidden());
// @formatter:on
}

@Test
public void givenCsrf_whenAddFoo_thenCreated() throws Exception {
// @formatter:off
mvc
.perform(post("/auth/foos")
.contentType(MediaType.APPLICATION_JSON)
.content(createFoo())
.with(testUser())
.with(csrf()))
.andExpect(status().isCreated());
// @formatter:on
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,25 @@ public class CsrfDisabledIntegrationTest extends CsrfAbstractIntegrationTest {

@Test
public void givenNotAuth_whenAddFoo_thenUnauthorized() throws Exception {
mvc.perform(post("/auth/foos").contentType(MediaType.APPLICATION_JSON).content(createFoo())).andExpect(status().isUnauthorized());
// @formatter:off
mvc
.perform(post("/auth/foos")
.contentType(MediaType.APPLICATION_JSON)
.content(createFoo()))
.andExpect(status().isUnauthorized());
// @formatter:on
}

@Test
public void givenAuth_whenAddFoo_thenCreated() throws Exception {
mvc.perform(post("/auth/foos").contentType(MediaType.APPLICATION_JSON).content(createFoo()).with(testUser())).andExpect(status().isCreated());
// @formatter:off
mvc
.perform(post("/auth/foos")
.contentType(MediaType.APPLICATION_JSON)
.content(createFoo())
.with(testUser()))
.andExpect(status().isCreated());
// @formatter:on
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,27 @@ public class CsrfEnabledIntegrationTest extends CsrfAbstractIntegrationTest {

@Test
public void givenNoCsrf_whenAddFoo_thenForbidden() throws Exception {
mvc.perform(post("/auth/foos").contentType(MediaType.APPLICATION_JSON).content(createFoo()).with(testUser())).andExpect(status().isForbidden());
// @formatter:off
mvc
.perform(post("/auth/foos")
.contentType(MediaType.APPLICATION_JSON)
.content(createFoo())
.with(testUser()))
.andExpect(status().isForbidden());
// @formatter:on
}

@Test
public void givenCsrf_whenAddFoo_thenCreated() throws Exception {
mvc.perform(post("/auth/foos").contentType(MediaType.APPLICATION_JSON).content(createFoo()).with(testUser()).with(csrf())).andExpect(status().isCreated());
// @formatter:off
mvc
.perform(post("/auth/foos")
.contentType(MediaType.APPLICATION_JSON)
.content(createFoo())
.with(testUser())
.with(csrf()))
.andExpect(status().isCreated());
// @formatter:on
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ public AuthenticationManager authenticationManagerBean() throws Exception {

@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user1").password("user1Pass").authorities("ROLE_USER").and().withUser("admin").password("adminPass").authorities("ROLE_ADMIN");
// @formatter:off
auth
.inMemoryAuthentication()
.withUser("user1")
.password("user1Pass")
.authorities("ROLE_USER")
.and()
.withUser("admin")
.password("adminPass")
.authorities("ROLE_ADMIN");
// @formatter:on
}

@Override
Expand All @@ -45,8 +55,7 @@ protected void configure(final HttpSecurity http) throws Exception {
.and()
.httpBasic()
.and()
.headers().cacheControl().disable()
;
.headers().cacheControl().disable();
// @formatter:on
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.baeldung.security.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityWithCsrfCookieConfig extends WebSecurityConfigurerAdapter {

public SecurityWithCsrfCookieConfig() {
super();
}

@Bean("authenticationManager")
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}

@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.inMemoryAuthentication()
.withUser("user1")
.password("user1Pass")
.authorities("ROLE_USER")
.and()
.withUser("admin")
.password("adminPass")
.authorities("ROLE_ADMIN");
// @formatter:on
}

@Override
public void configure(final WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**");
}

@Override
protected void configure(final HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests()
.antMatchers("/auth/admin/*").hasAnyRole("ROLE_ADMIN")
.anyRequest().authenticated()
.and()
.httpBasic()
.and()
.headers().cacheControl().disable()
// Stateless API CSRF configuration
.and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
// @formatter:on
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ public AuthenticationManager authenticationManagerBean() throws Exception {

@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user1").password("user1Pass").authorities("ROLE_USER").and().withUser("admin").password("adminPass").authorities("ROLE_ADMIN");
// @formatter:off
auth
.inMemoryAuthentication()
.withUser("user1")
.password("user1Pass")
.authorities("ROLE_USER")
.and()
.withUser("admin")
.password("adminPass")
.authorities("ROLE_ADMIN");
// @formatter:on
}

@Override
Expand All @@ -47,8 +57,7 @@ protected void configure(final HttpSecurity http) throws Exception {
.and()
.headers().cacheControl().disable()
.and()
.csrf().disable()
;
.csrf().disable();
// @formatter:on
}

Expand Down