spring boot 保护 web 应用程序——迹忆客-ag捕鱼王app官网
如果在 classpath 上添加了 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
click to see a greeting.
使用 thymeleaf 模板在 html 文件中定义的简单视图 /hello
。
现在,在 src/main/resources/templates 目录下创建一个 hello.html。
hello world!
现在,我们需要设置 spring mvc – 用于 home 和 hello 视图的视图控制器。
为此,创建一个扩展 webmvcconfigureradapter
的 mvc 配置文件。
package com.jiyik.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");
}
}
现在,将 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.jiyik.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/main/resources 目录下创建一个 login.html 文件,以允许用户通过登录屏幕访问 http 端点。
spring security example
invalid username and password.
you have been logged out.
最后,更新 hello.html 文件——允许用户退出应用程序并显示当前用户名,如下所示
hello world!
主要 spring boot 应用程序的代码如下
package com.jiyik.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);
}
}
现在,我们可以创建一个可执行的 jar 文件,并使用以下 maven 或 gradle 命令运行 spring boot 应用程序。
对于 maven,使用下面给出的命令
$ mvn clean install
成功之后,我们可以在 target 目录下找到 jar 文件。
对于 gradle,使用下面给出的命令
$ gradle clean build