あなたはクラスパス上の春ブーツセキュリティの依存関係を追加すると、春ブートアプリケーションは自動的にエンドポイントのすべてのHTTP基本認証を提供します。エンドポイント“/”
とは、“/home”
任意の認証を必要としません。他のすべてのエンドポイントは、認証を必要とします。
春ブーツセキュリティに春のブートアプリケーションに追加するには、ビルド構成ファイルで春ブーツスターターセキュリティの依存関係を追加する必要があります。
Mavenのユーザーができるのpom.xml、次のファイルの依存関係を追加します。
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
Gradleではユーザーができるbuild.gradle、次のファイルの依存関係を追加します。
compile("org.springframework.boot:spring-boot-starter-security")
Webアプリケーションのセキュリティ対策
まず、Webアプリケーションの危険な使用Thymeleafのテンプレートを作成します。
その後、中のsrc /メイン/リソース/テンプレートディレクトリを作成するhome.html
ファイルを。
<!DOCTYPE html>
<html xmlns = "http://www.w3.org/1999/xhtml"
xmlns:th = "http://www.thymeleaf.org"
xmlns:sec = "http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head> <title>Spring Security示例</title> </head> <body> <h1>欢迎您!</h1> <p>点击 <a th:href = "@{/hello}">这里</a> 看到问候语.</p> </body> </html>
ThymeleafのテンプレートはHTMLファイルで定義された単純なビューを使用しました/hello
。さて、src/main/resources/templates
:ディレクトリ内のファイルを作成hello.htmlを。
<!DOCTYPE html>
<html xmlns = "http://www.w3.org/1999/xhtml"
xmlns:th = "http://www.thymeleaf.org"
xmlns:sec = "http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head> <title>Hello World!</title> </head> <body> <h1>Hello world!</h1> </body> </html>
ビューコントローラ-今、私たちは、ホームビューに春のMVCやハローを設定する必要があります。これを行うには、拡張作成WebMvcConfigurerAdapter
MVCのコンフィギュレーション・ファイルのを。
package com.yiibai.websecuritydemo;
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/home").setViewName("home"); registry.addViewController("/").setViewName("home"); registry.addViewController("/hello").setViewName("hello"); registry.addViewController("/login").setViewName("login"); } }
今のプロファイルを構築するために春ブーツスターターセキュリティの依存関係を追加します。Mavenのユーザーができるのpom.xml、次のファイルの依存関係を追加します。
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
Gradleではユーザーができるbuild.gradle、次のファイルの依存関係を追加します。
compile("org.springframework.boot:spring-boot-starter-security")
さて、HTTPエンドポイントにアクセスするために基本認証を使用するようにアプリケーションを保護するために使用されたWebセキュリティの設定ファイルを作成します。
package com.yiibai.websecuritydemo;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/", "/home").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("user").password("password").roles("USER"); } }
さて、中のsrc /メイン/リソースディレクトリを作成するにはlogin.htmlとの HTTPエンドポイントのログイン画面を経由してアクセスをユーザーに許可するファイルを。
<!DOCTYPE html>
<html xmlns = "http://www.w3.org/1999/xhtml" xmlns:th = "http://www.thymeleaf.org"
xmlns:sec = "http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head> <title>Spring Security示例</title> </head> <body> <div th:if = "${param.error}"> 无效的用户名和密码. </div> <div th:if = "${param.logout}"> 你已经注销. </div> <form th:action = "@{/login}" method = "post"> <div> <label> 用户名 : <input type = "text" name = "username"/> </label> </div> <div> <label> 密码: <input type = "password" name = "password"/> </label> </div> <div> <input type = "submit" value = "登录"/> </div> </form> </body> </html>
最後に、更新hello.htmlのファイルが-次のように、ユーザーがアプリケーションと表示し、現在のユーザー名からログオフすることができます-
<!DOCTYPE html>
<html xmlns = "http://www.w3.org/1999/xhtml" xmlns:th = "http://www.thymeleaf.org"
xmlns:sec = "http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head> <title>Hello World!</title> </head> <body> <h1 th:inline = "text">您好,[[${#httpServletRequest.remoteUser}]]!</h1> <form th:action = "@{/logout}" method = "post"> <input type = "submit" value = "注销"/> </form> </body> </html>
次のように春のブートコード主なアプリケーションです -
package com.yiibai.websecuritydemo;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class WebsecurityDemoApplication { public static void main(String[] args) { SpringApplication.run(WebsecurityDemoApplication.class, args); } }
ここでは、完全なコードのビルド構成ファイルです。
Mavenのビルドファイル- のpom.xml次のように内容は以下のとおりです。
<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.yiibai</groupId> <artifactId>websecurity-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>websecurity-demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId>