免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
まず、プロジェクトを作成します
新しいMavenのプロジェクトspringsecurity-デモ、および編集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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<name>springsecurity-demo</name>
<artifactId>springsecurity-demo</artifactId>
<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-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>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
第二に、実装コード
1.起動クラス作成し
、以下のように、SRCのio.binghe.demoパッケージアイテム/メイン/ Javaで作成されたが、パッケージDemoApplicationクラスを作成します。
package io.binghe.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author binghe
* @version 1.0.0
* @description 项目启动类
*/
@SpringBootApplication
@EnableAutoConfiguration
public class DemoApplication {
public static void main(String[] args){
SpringApplication.run(DemoApplication.class, args);
}
}
2.次のように、io.binghe.demoパケットでServletInitializerクラスを作成します。
package io.binghe.demo;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
/**
* @author binghe
* @version 1.0.0
* @description Servlet初始化类
*/
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
}
3.次のように設定SpringSecurityで、パッケージSpringSecurityConfig io.binghe.demoクラスを作成し、コードは次のとおりです。
package io.binghe.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
* @author binghe
* @version 1.0.0
* @description SpringSecurity配置类
*/
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.logout().permitAll()
.and()
.formLogin();
http.csrf().disable();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/js/**", "/css/**", "/images/**");
}
}
4.新しいパッケージio.binghe.demo.controller、パッケージ作成DemoControllerクラスは、次のように、コントローラクラスを作成します。
package io.binghe.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author binghe
* @version 1.0.0
* @description 测试使用的Controller
*/
@RestController
public class DemoController {
@RequestMapping(value = "/")
public String home(){
return "hello string boot";
}
@RequestMapping(value = "/hello")
public String hello(){
return "hello world";
}
}
第三に、テスト
1.プロジェクトの開始
操作DemoApplicationの主な方法を
2.テストエンジニアリング
、それぞれ、ブラウザでhttp:// localhostを:8080とにhttp:// localhost:8080 /こんにちは 。// localhostを:あなたは、httpへの直接アクセスを確認でき8080、およびhttp:// localhost:8080にアクセス/こんにちはユーザー名とパスワードを入力する必要があります。説明SpringBoot統合SpringSecurity成功。
以下、図面を伴い:
ログインはhttp:// localhostを:8080回の結果は以下のとおりです。
訪問にhttp:// localhost:8080 /ハロー結果は以下のとおりです。