0

REST エンドポイントで Spring Security 3 を使用しています。基本的なSpring Securityを機能させることができました。

security-context.xml の一部

<security:http auto-config="true" use-expressions="true" access-denied-page="/rest/denied" >
<security:intercept-url pattern="/rest/*" access="ROLE_USER"/>

ウェブ上にある基本設定

<security:authentication-manager> 
      <security:authentication-provider user-service-ref="userDetailsService">
           <security:password-encoder ref="passwordEncoder"/>
     </security:authentication-provider>

<!-- Use a Md5 encoder since the user's passwords are stored as Md5 in the database -->
<bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"   id="passwordEncoder"/>

<!-- An in-memory list of users. No need to access an external database layer.
See Spring Security 3.1 Reference 5.2.1 In-Memory Authentication -->
  <!-- john's password is admin, while jane;s password is user  -->
  <security:user-service id="userDetailsService">
     <security:user name="john" password="21232f297a57a5a743894a0e4a801fc3"     authorities="ROLE_USER, ROLE_ADMIN" />
  <security:user name="jane" password="ee11cbb19052e40b07aac0ca060c23ee" authorities="ROLE_USER" />
</security:user-service>

RestTemplate POST を使用して j_spring_security_check にログインしたい。

HttpEntity<String> entity = new HttpEntity<String>(request, headers);
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("j_username", "john");
            map.put("j_password","21232f297a57a5a743894a0e4a801fc3");

            String response = restTemplate.postForObject("http://localhost:8080/rest/j_spring_security_check",  map, String.class);

しかし、ログでは、ユーザー名パラメーターが読み取られていないようです

DEBUG o.s.s.authentication.ProviderManager - Authentication attempt using org.springframework.security.authentication.dao.DaoAuthenticationProvider
DEBUG o.s.s.a.d.DaoAuthenticationProvider - User '' not found
DEBUG o.s.s.w.a.UsernamePasswordAuthenticationFilter - Authentication request failed: org.springframework.security.authentication.BadCredentialsException: Bad credentials

REST テンプレートを取得して認証資格情報を送信する正しい方法は何ですか? j_spring_security_check 以外にログイン/認証を取得するためのより良い方法はありますか? ヘッダーに情報が入りますか?

前もって感謝します。

4

1 に答える 1

1

これは別のSO質問の複製のようです。しかし、あなたはおそらくこれに間違った方法でアプローチしています。通常、RESTリクエストを発行している場合、同時に認証することはありません-これは意味がありません-確かにフォームPOSTスタイルのロジックを使用していません。

REST要求の認証には、別の形式の認証を使用する必要があります(これらの要求がプログラムで生成されると想定):*HTTP基本認証*X.509証明書

または、これがXHR / Javascriptオリジンを介して発生している場合は、リクエストが失敗し、ユーザーをログインメカニズムにリダイレクトする準備をしておく必要があります。通常、Spring Securityを使用したRESTスタイルのリクエストの処理は、通常の保護されたページの処理とはまったく同じではありません。ある程度の複雑さに備える必要があります。

幸運を!

于 2011-09-27T18:36:55.627 に答える