HTTP基本認証が設定されると、Webブラウザはユーザ認証用のログインダイアログを表示します。このチュートリアルでは、Spring SecurityでHTTP基本認証を設定する方法を説明します。

  <http>
    <intercept-url pattern="/welcome** " access="ROLE__USER"/>
    <http-basic/>
  </http>

最後のリンク://spring-security/spring-security-form-login-example/[Springセキュリティフォームベースログインの例]は再利用されますが、HTTP基本をサポートするように認証を切り替えます。

1.春のセキュリティ

HTTP Basicを有効にするには、 ”

form-login

“を ”

http-basic

“タグに変更してください。

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">

    <!-- HTTP basic authentication in Spring Security -->
    <http>
        <intercept-url pattern="/welcome** " access="ROLE__USER"/>
        <http-basic/>
    </http>

    <authentication-manager>
       <authentication-provider>
           <user-service>
           <user name="mkyong" password="123456" authorities="ROLE__USER"/>
           </user-service>
       </authentication-provider>
    </authentication-manager>

</beans:beans>

完了です。

デモ

セキュリティで保護されたURLにアクセスすると、ブラウザにログインダイアログボックスが自動的に表示されます。


URL:http://localhost:8080/SpringMVC/welcome


http基本例、title = "spring-security-http-basic"、width = 640、height = 465

ソースコードをダウンロードする

ダウンロードする – リンク://wp-content/uploads/2011/08/Spring-Security-HTTP-Basic-Authentication-Example.zip[Spring-Security-HTTP-Basic-Authentication-Example.zip](9 KB)

参考文献

  1. リンク://spring-security/spring-security-hello-world-example/[Spring

セキュリティのこんにちは世界の例]。リンク://spring-security/spring-security-form-login-example/[Spring

セキュリティフォームベースのログインの例]

リンク://タグ/認証/[認証]

spring security