2016-08-11 2 views
1

私はこの問題について調査していましたが、それでも回避できません。 問題は、私がajaxに電話をかけようとしているときに来るだけです。 「セッションが見つかりませんでしたので、提供されたCSRFトークンを確認できませんでした

@RestController 
public class CsrfController { 

    @RequestMapping("/csrf") 
    public CsrfToken csrf(CsrfToken token) { 
     return token; 
    } 
} 

と私:システムはエラーSpring MVC and CSRF Integrationから基づいCould not verify the provided CSRF token because your session was not found.

を返します、私はこれを使用する必要があり、私はJavaのコンフィグを使用している場合は、これを解決するために@EnableWebSecurityを含めるする必要がありますが、XMLを使用している場合上記のクラスの使い方が分かりません。

本当に解決策があるのか​​、それとも私が使用できる解決策があるのか​​という疑問がありますか?

これはセキュリティ設定のxmlファイルです。

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:c="http://www.springframework.org/schema/c" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 

    <!-- Global Security Settings --> 
    <global-method-security pre-post-annotations="enabled" /> 
    <context:component-scan base-package="com.my.web" /> 

    <!-- Reads WEB Configuration file to resolve ${} and read @Value for Security--> 
    <context:property-placeholder location="classpath:cfg/web.cfg" /> 
    <context:annotation-config /> 

    <!-- Security Access Configuration --> 

    <http auto-config="false" use-expressions="true" authentication-manager-ref="CAP" disable-url-rewriting="true" entry-point-ref="IAE"> 

     <session-management session-fixation-protection="newSession" session-authentication-error-url="/logout?timeout" > 
      <concurrency-control max-sessions="1" expired-url="/logout?expired" /> 
     </session-management> 
     <custom-filter position="PRE_AUTH_FILTER" ref="entryFilter" /> 
     <intercept-url pattern="/resources/**" access="permitAll()" requires-channel="https" /> 
     <intercept-url pattern="/clearcache" access="permitAll()" requires-channel="https" /> 
     <intercept-url pattern="/logout" access="permitAll()" requires-channel="https" /> 
     <intercept-url pattern="/**" access="isFullyAuthenticated()" requires-channel="https" /> 

     <port-mappings > 
      <port-mapping http="7001" https="7002" /> 
     </port-mappings> 

    <headers> 
     <frame-options policy="SAMEORIGIN" /> 
     <hsts /> 
     <cache-control /> 
     <xss-protection /> 
     <content-type-options /> 
    </headers> 

    </http> 

    <beans:bean id="entryFilter" class="com.my.web.security.HeaderFilter" > 
     <beans:property name="authenticationManager" ref="CAP"/> 
     </beans:bean> 
    <beans:bean id="IAE" class="com.my.web.security.CustomAuthenticationEntryPoint" /> 
    <beans:bean id="CAP" class="com.my.web.security.CustomAuthenticationManager" /> 

    <beans:import resource="../aop/aspect-security.xml" /> 
</beans:beans> 

さらに、私はCA Siteminderと同様のシステムを使用しており、ログインフォームのないヘッダー情報に基づいてユーザーを検証します。

+0

どのようにAJAX呼び出しがセッションを表示していますか? – Raedwald

+0

は、ajaxの 'POST'呼び出しのためです。それはcsrfトークンを必要とし、それはセッショントークンと一致します – FreezY

+0

あなたはAJAX呼び出しのコードを提供する必要があると思います。 – Raedwald

答えて

-1

ここで司会のいくつかは本当に答えと質問を区別することはできませんので、私はここで何かを強調したいので:

これが答えです!

[OK]をので、ここのものは、文字通り同じ原因を持つ他の質問から、主にそれがフォーム・ログインなしRestheaderを使用してシステムに影響を与えます。これは、webappがコンテナシステムにあることを意味します。したがって、コンテナ内のシステムとコンテナ内のシステムの両方の位置は、クライアント側ではまったく異なります。ここで

は、コンテナのWebアプリケーションのために...

例です:Webアプリケーションのために

www.myweb.com/main/ 

容器に:

www.myweb.com/main/child 

この異なる意志がアクセスを拒否し、確かに理由は403を与えます私たちが探しているものはそこには存在しません。

例:

www.myweb.com/main/child/playground 

/playground URIが存在するが、確かにされるが、ここで禁止されている:

www.myweb.com/main/playground 

だからあなたのURIを確認し、相対パスを使用していることを確認してみてください。そのはるかに良い。

P/s:何らかの理由で、サーバー側から呼び出されたものはコンテナ内またはコンテナ内のシステムのまま残ります。これはサーバー側が絶対パスではなく相対パスを使用しているために発生します。

+0

あなたの例ではホストはまだ同じです: 'www.myweb.com'なので、全く問題はありません。 – MariuszS

1

春には、すべてのフォーム送信時にcsrfトークンを送信する必要があります。これを実現するにはJSPに次のコードを挿入するたとえば可能です:

<form> 
[...] 
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> 
[...] 
</form> 

敬具

0

私はSOに新しい」とこれが私の最初の答えですので、私と一緒にクマ。 これはjQueryのを使用している場合は、AJAX呼び出しを行う前に行われなければならないものです。

$(document).ready(

     function() { 

      var token = $("meta[name='_csrf']").attr("content"); 

      $.ajaxSetup({ 
       headers : { 
        "X-CSRF-Token" : token 
       } 
      }); 

}); 
関連する問題