[SPRING] Spring Rest Service - 로그인을 시도 할 때 잘못된 CSRF 토큰
SPRINGSpring Rest Service - 로그인을 시도 할 때 잘못된 CSRF 토큰
Spring Security (3.2.5.RELEASE)가 활성화 된 Spring MVC REST 서비스가있다. @EnableWebMvcSecurity를 켜면 http : // localhost : 8080 / login에서 자동으로 로그인 양식이 생성됩니다. 이 양식을 사용하여 로그인하면 모든 것이 올바르게 작동합니다.
문제는 직접 POST 요청을 보내서 로그인을 시도 할 때 발생합니다. 게시물 요청시 사용자 이름과 비밀번호를 제공합니다. 또한 http 헤더 'X-CSRF-TOKEN'을 포함하고 헤더 값에 대해 쿠키에 생성 된 JSESSIONID를 사용합니다. 그러나이 POST 요청을 보내면 다음 결과가 반환됩니다.
HTTP Status 403 - Invalid CSRF Token '29F5E49EFE8D758D4903C0491D56433E'
was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'.
내가 뭘 잘못하고 있죠? 잘못된 토큰 값을 제공합니까? 이 JSESSIONID는 무엇입니까? 이 헤더에 값을 입력하지 않거나 머리글을 모두 생략하면 "Null CSRF token found"가 표시됩니다.
아래는 Spring Security 구성입니다.
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/secure/**").authenticated()
.and()
.formLogin()
.usernameParameter("username")
.passwordParameter("password")
.and()
.logout()
.and()
.httpBasic()
.and()
.csrf();
}
}
나는 정말 어떤 도움을 주셔서 감사합니다! 미리 감사드립니다.
해결법
-
==============================
1.
$(function () { var token = $('#logoutform>input').val(); var header = $('#logoutform>input').attr('name'); $(document).ajaxSend(function(e, xhr, options) { xhr.setRequestHeader('X-CSRF-TOKEN', token); }); });
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
-
==============================
2.로그인 양식을 제출할 때 csrf 토큰을 보내야합니다. 아래 양식을 HTML 양식에 추가하십시오.
로그인 양식을 제출할 때 csrf 토큰을 보내야합니다. 아래 양식을 HTML 양식에 추가하십시오.
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
-
==============================
3.당신은 , https://spring.io/blog/2013/08/21/spring-security-3-2-0-rc1- 하이라이트 -csrf-protection / # ajax-requests
당신은 , https://spring.io/blog/2013/08/21/spring-security-3-2-0-rc1- 하이라이트 -csrf-protection / # ajax-requests
또는 thymeleaf를 사용하는 경우
from https://stackoverflow.com/questions/28731699/spring-rest-service-invalid-csrf-token-when-i-attempt-to-login by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] Spring Data Mongodb - 다른 유형의 콜렉션을위한 저장소 (0) | 2019.04.08 |
---|---|
[SPRING] HttpSession이 SPRING_SECURITY_CONTEXT에 대해 null 개체를 반환했습니다. (0) | 2019.04.08 |
[SPRING] Spring MVC : @ResponseStatus (이유 = '')를 Tomcat의 @ResponseBody 예외 핸들러에서 사용하기 (0) | 2019.04.08 |
[SPRING] 스프링 보안 로그 아웃 처리 (0) | 2019.04.08 |
[SPRING] Spring ApplicationListener가 webapp에서 두 번 실행됩니다. (0) | 2019.04.08 |