春のセキュリティhello worldの例

Spring Security Hello Worldの例

このチュートリアルでは、Spring SecurityをSpring MVC Webアプリケーションと統合してURLアクセスを保護する方法を示します。 Spring Securityを実装した後、「admin」ページのコンテンツにアクセスするには、ユーザーは正しい「username」と「password」を入力する必要があります。

使用される技術:

  1. Spring 3.2.8.RELEASE

  2. Spring Security 3.2.3.RELEASE

  3. Eclipse 4.2

  4. JDK 1.6

  5. メーベン3

Note
Spring Security3.0にはJava5.0ランタイム環境以上が必要です

1. プロジェクトのデモ

2. ディレクトリ構造

このチュートリアルの最終的なディレクトリ構造を確認してください。

spring-security-helloworld-directory

3. Springセキュリティの依存関係

Springセキュリティを使用するには、spring-security-webspring-security-configが必要です。

pom.xml

    
        1.6
        3.2.8.RELEASE
        3.2.3.RELEASE
        1.2
    

    

        
        
            org.springframework
            spring-core
            ${spring.version}
        

        
            org.springframework
            spring-web
            ${spring.version}
        

        
            org.springframework
            spring-webmvc
            ${spring.version}
        

        
        
            org.springframework.security
            spring-security-web
            ${spring.security.version}
        

        
            org.springframework.security
            spring-security-config
            ${spring.security.version}
        

        
        
            jstl
            jstl
            ${jstl.version}
        

    

4. Spring MVC Webアプリケーション

シンプルなコントローラー:

  1. URL =/welcomeまたは/の場合、helloページを返します。

  2. URL =/adminの場合、管理ページを返します。

後で、Spring Securityを使用して、「/ admin」URLをユーザーログインフォームで保護する方法を示します。

HelloController.java

package com.example.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

    @RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
    public ModelAndView welcomePage() {

        ModelAndView model = new ModelAndView();
        model.addObject("title", "Spring Security Hello World");
        model.addObject("message", "This is welcome page!");
        model.setViewName("hello");
        return model;

    }

    @RequestMapping(value = "/admin**", method = RequestMethod.GET)
    public ModelAndView adminPage() {

        ModelAndView model = new ModelAndView();
        model.addObject("title", "Spring Security Hello World");
        model.addObject("message", "This is protected page!");
        model.setViewName("admin");

        return model;

    }

}

2つのJSPページ。

hello.jsp

<%@page session="false"%>


    

Title : ${title}

Message : ${message}

admin.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page session="true"%>


    

Title : ${title}

Message : ${message}

Welcome : ${pageContext.request.userPrincipal.name} | " > Logout

mvc-dispatcher-servlet.xml



    

    
      
        /WEB-INF/pages/
      
      
        .jsp
      
    

5. Spring Security:ユーザー認証

Spring Security XMLファイルを作成します。

spring-security.xml



    
        
    

    
      
        
        
        
      
    

これは、ユーザー「example」のみが/adminURLへのアクセスを許可されていることを示しています。

6. Spring Securityを統合する

SpringセキュリティをSpringMVC Webアプリケーションと統合するには、DelegatingFilterProxyをサーブレットフィルターとして宣言して、着信要求をインターセプトします。

web.xml



    Spring MVC Application

    
    
        mvc-dispatcher
        org.springframework.web.servlet.DispatcherServlet
        
        1
    
    
        mvc-dispatcher
        /
    

    
        org.springframework.web.context.ContextLoaderListener
        
    

        
    
        contextConfigLocation
        
            /WEB-INF/spring-security.xml
        
    

    
    
        springSecurityFilterChain
        org.springframework.web.filter.DelegatingFilterProxy
        
    

    
        springSecurityFilterChain
        /*
    

7. Demo

それだけですが、待ってください...ログインフォームはどこにありますか? カスタムログインフォームを定義しない場合、Springは簡単なログインフォームを自動的に作成します。

Custom Login Form
この「http://www.example.com/spring-security/spring-security-form-login-example/ [Spring Securityフォームのログイン例]」を読んで、作成方法を理解してください。 SpringSecurityのカスタムログインフォーム。

1. ウェルカムページ–http://localhost:8080/spring-security-helloworld-xml/welcome

spring-security-helloworld-welcome

2. /adminページにアクセスしようとすると、Spring Securityはリクエストをインターセプトして/spring_security_loginにリダイレクトし、事前定義されたログインフォームが表示されます。

spring-security-helloworld-login

3. ユーザー名とパスワードが正しくない場合、エラーメッセージが表示され、SpringはこのURL/spring_security_login?login_errorにリダイレクトします。

spring-security-helloworld-login-error

4. ユーザー名とパスワードが正しい場合、Springはリクエストを元のリクエストされたURLにリダイレクトし、ページを表示します。

spring-security-helloworld-admin

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

ダウンロード–spring-security-helloworld-xml.zip(9 KB)