0
私は現在Spring Security 4に取り組んでいます。デフォルトのログイン方法を使用しているとすべてうまく動作します。しかし、実際には、Ajaxメソッドを使用してログインする必要があります。 controllers
でSpring Security Ajax Login
春-のsecurity.xml
<http use-expressions="true" auto-config="true">
<intercept-url pattern="/resources/**" access="permitAll" />
<form-login
login-processing-url="/resources/login-processing"
default-target-url="http://127.0.0.1:57633/"
/>
<csrf request-matcher-ref="csrfSecurityRequestMatcher"/>
<!-- <csrf disabled="true" /> -->
</http>
、私は@PreAuthorize
アノテーションを使用します。次のようにjQueryのでは、Ajaxのリクエストは、次のとおりです。
$.ajax({
url:"http://localhost:8080/myapp/resources/login-processing",
data:{username:$("#inputLogin")[0].value,
password:$("#inputPassword")[0].value
},
method:"POST",
success: function(data, textStatus, request) {
console.log(request.getResponseHeader())
}
})
.done(function(data, textStatus, request) {
console.log(request.getResponseHeader("Set-Cookie"))
})
.error(function(error) {
console.log(error)
})
次レスポンスヘッダと私はpermitAll
を設定していても、それはまだ応答403エラー:
Access-Control-Allow-Headers:x-requested-with
Access-Control-Allow-Methods:POST, GET, OPTIONS, DELETE
Access-Control-Allow-Origin:*
Access-Control-Max-Age:3600
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Language:en
Content-Length:1116
Content-Type:text/html;charset=utf-8
Date:Mon, 18 Apr 2016 02:07:26 GMT
Expires:0
Pragma:no-cache
Server:Apache-Coyote/1.1
Set-Cookie:JSESSIONID=974B1629ECE3EB289F35097C9E9D9FDC; Path=/cancercloud/; HttpOnly
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
私はAjaxのログインを設定するにはどうすればよいですか?
ユーザ名またはパスワードが間違っている可能性がありますか?ハードコーディングしてみてください。 「http:// localhost:8080/myapp/resources/login-processing」をサーブレット名のみに変更しようとしましたか? – Gero
ajaxにもcsrfを含めてください – FreezY