gpt4 book ai didi

angularjs - 使用 AngularJS 和 SpringSecurity3.2 进行 CSRF

转载 作者:行者123 更新时间:2023-12-03 00:35:05 24 4
gpt4 key购买 nike

AngularJS

index.html

<head>
<meta name="_csrf" content="${_csrf.token}"/>
<!-- default header name is X-CSRF-TOKEN -->
<meta name="_csrf_header" content="${_csrf.headerName}"/>
</head>

SpringSecurity 3.2

Spring 使用 HttpSessionCsrfTokenRepository,默认情况下将 CSRF 的 header 名称指定为 X-CSRF-TOKEN,但 Anuglar 约定为 X-XSRF-TOKEN>

我想扩展HttpSessionCsrfTokenRepository并覆盖 header 名称,但由于它被标记为final,所以我最终实现了一个自定义 token 存储库。

@Component
public class CustomCsrfTokenRepository implements CsrfTokenRepository {

public static final String CSRF_PARAMETER_NAME = "_csrf";

public static final String CSRF_HEADER_NAME = "X-XSRF-TOKEN";

private final Map<String, CsrfToken> tokenRepository = new ConcurrentHashMap<>();

public CustomCsrfTokenRepository() {
log.info("Creating {}", CustomCsrfTokenRepository.class.getSimpleName());
}

@Override
public CsrfToken generateToken(HttpServletRequest request) {
return new DefaultCsrfToken(CSRF_HEADER_NAME, CSRF_PARAMETER_NAME, createNewToken());
}

@Override
public void saveToken(CsrfToken token, HttpServletRequest request, HttpServletResponse response) {
String key = getKey(request);
if (key == null)
return;

if (token == null) {
tokenRepository.remove(key);
} else {
tokenRepository.put(key, token);
}
}

@Override
public CsrfToken loadToken(HttpServletRequest request) {
String key = getKey(request);
return key == null ? null : tokenRepository.get(key);
}

private String getKey(HttpServletRequest request) {
return request.getHeader("Authorization");
}

private String createNewToken() {
return UUID.randomUUID().toString();
}
}

SecurityConfig.java

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Inject
private CustomCsrfTokenRepository customCsrfTokenRepository;


@Override
protected void configure(HttpSecurity http) throws Exception {

http
// .addFilterAfter(new CsrfTokenGeneratorFilter(), CsrfFilter.class)
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.formLogin()
.loginProcessingUrl("/app/authentication")
.successHandler(ajaxAuthenticationSuccessHandler)
.failureHandler(ajaxAuthenticationFailureHandler)
.usernameParameter("j_username")
.passwordParameter("j_password")
.permitAll()
.and()

.csrf()
.csrfTokenRepository(customCsrfTokenRepository)
.and()
}
}
  1. 如何干净地覆盖 header 名称而不是创建自定义 csrfTokenRepository?

  2. 我还需要对单页进行其他配置更改吗AngularJS 等应用程序,因为这还无法运行。

最佳答案

当使用 Spring Security 的 Java 配置时,应该可以实现以下操作:

  public void configure(final HttpSecurity http) throws Exception
{
final HttpSessionCsrfTokenRepository tokenRepository = new HttpSessionCsrfTokenRepository();
tokenRepository.setHeaderName("X-XSRF-TOKEN");

http.csrf().csrfTokenRepository(tokenRepository);
}

复杂之处在于单页应用程序依赖于 AJAX,并且在 AJAX 请求中包含 CSRF token 有点复杂。使用 AngularJS 时,服务器应在首次请求时以及用户登录或注销时发送名为 XSRF-TOKEN 的 session cookie。然后,AngularJS 将在所有请求的 HTTP header X-XSRF-TOKEN 中返回此 cookie 的值,然后服务器可以检查该值。

关于angularjs - 使用 AngularJS 和 SpringSecurity3.2 进行 CSRF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24022967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com