如果在类路径上添加了Spring Boot Security依赖项,则Spring Boot应用程序会自动要求对所有HTTP端点进行基本身份验证.端点"/"和"/home"不需要任何身份验证.所有其他端点都需要身份验证.
为了向Spring Boot应用程序添加Spring Boot Security,我们需要在构建配置文件中添加Spring Boot Starter Security依赖项.
Maven用户可以在pom.xml文件中添加以下依赖项.
org.springframework.boot spring-boot-starter-security
Gradle用户可以在build.gradle文件中添加以下依赖项.
compile("org.springframework.boot:spring-boot-starter-security")
保护Web应用程序
首先,使用Thymeleaf模板创建一个不安全的Web应用程序.
然后,在 src/main/resources/templates 下创建一个home.html文件目录.
Spring Security Example Welcome!
Click here to see a greeting.
使用Thymeleaf模板在HTML文件中定义的简单视图/hello .
现在,在 src/main/resources/templates 目录下创建一个hello.html.
Hello World! Hello world!
现在,我们需要为Home和hello视图设置Spring MVC - View控制器.
为此,创建一个扩展WebMvcConfigurerAdapter的MVC配置文件.
package com.it1352.websecuritydemo; import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configurationpublic 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"); }}
现在,将Spring Boot Starter安全依赖项添加到构建配置文件中.
Maven用户可以在pom.xml文件中添加以下依赖项.
org.springframework.boot spring-boot-starter-security
Gradle用户可以在build.gradle文件中添加以下依赖项.
compile("org.springframework.boot:spring-boot-starter-security")
现在,创建一个Web安全配置文件,即用于保护您的应用程序通过使用基本身份验证来访问HTTP端点.
package com.it1352.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@EnableWebSecuritypublic 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/main/resources 下创建一个login.html文件允许用户通过登录屏幕访问HTTP端点的目录.
Spring Security Example Invalid username and password.You have been logged out.
最后,更新hello.html文件 - 允许用户从应用程序注销并显示当前用户名,如下所示 :
Hello World! Hello [[${#httpServletRequest.remoteUser}]]!
主要Spring Boot应用程序的代码在下面和下面给出;
package com.it1352.websecuritydemo; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class WebsecurityDemoApplication { public static void main(String[] args) { SpringApplication.run(WebsecurityDemoApplication.class, args); }}
下面给出了构建配置文件的完整代码.
Maven - pom.xml
4.0.0 com.IT屋 websecurity-demo 0.0.1-SNAPSHOT jar websecurity-demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-security org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.security spring-security-test test org.springframework.boot spring-boot-maven-plugin
Gradle - build.gradle
buildscript { ext { springBootVersion = '1.5.9.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") }}apply plugin: 'java'apply plugin: 'eclipse'apply plugin: 'org.springframework.boot'group = 'com.it1352'version = '0.0.1-SNAPSHOT'sourceCompatibility = 1.8repositories { mavenCentral()}dependencies { compile('org.springframework.boot:spring-boot-starter-security') compile('org.springframework.boot:spring-boot-starter-thymeleaf') compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') testCompile('org.springframework.security:spring-security-test')}
现在,创建一个可执行的JAR文件,使用以下Maven或Gradle命令运行Spring Boot应用程序.
Maven用户可以使用下面给出的命令 :
mvn clean install
在"BUILD SUCCESS"之后,你可以在目标目录下找到JAR文件.
Gradle用户可以使用如下所示的命令 :
gradle clean build
在"BUILD SUCCESSFUL"之后,您可以在build/libs目录下找到JAR文件.
现在,使用下面显示的命令运行JAR文件 : ;
java -jar
在网络浏览器中点击网址 http://localhost:8080/.您可以看到输出如图所示.