前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot保护Web应用程序

Spring Boot保护Web应用程序

作者头像
黑洞代码
发布2021-12-04 15:35:18
7910
发布2021-12-04 15:35:18
举报
文章被收录于专栏:落叶飞翔的蜗牛

如果在类路径上添加了Spring Boot Security依赖项,则Spring Boot应用程序会自动为所有HTTP端点提供基本身份验证。端点“/”和“/home”不需要任何身份验证。所有其他端点都需要身份验证。

//更多请阅读:https://www.yiibai.com/spring-boot/spring_boot_securing_web_applications.html

要将Spring Boot Security添加到Spring Boot应用程序,需要在构建配置文件中添加Spring Boot Starter Security依赖项。

//更多请阅读:https://www.yiibai.com/spring-boot/spring_boot_securing_web_applications.html

Maven用户可以在pom.xml 文件中添加以下依赖项。

代码语言:javascript
复制
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>

保护Web应用程序

首先,使用Thymeleaf模板创建不安全的Web应用程序。然后,在 src/main/resources/templates 目录下创建一个home.html 文件。

//更多请阅读:https://www.yiibai.com/spring-boot/spring_boot_securing_web_applications.html

代码语言:javascript
复制
<!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。

//更多请阅读:https://www.yiibai.com/spring-boot/spring_boot_securing_web_applications.html

代码语言:javascript
复制
<!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>

现在,需要为Home和hello视图设置Spring MVC - View控制器。为此,创建一个扩展WebMvcConfigurerAdapter的MVC配置文件。

//更多请阅读:https://www.yiibai.com/spring-boot/spring_boot_securing_web_applications.html

代码语言:javascript
复制
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 文件中添加以下依赖项。

//更多请阅读:https://www.yiibai.com/spring-boot/spring_boot_securing_web_applications.html

代码语言:javascript
复制
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>

现在,创建一个Web安全配置文件,该文件用于保护应用程序以使用基本身份验证访问HTTP端点。

//更多请阅读:https://www.yiibai.com/spring-boot/spring_boot_securing_web_applications.html

代码语言:javascript
复制
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端点。

//更多请阅读:https://www.yiibai.com/spring-boot/spring_boot_securing_web_applications.html

代码语言:javascript
复制
<!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 文件 - 允许用户从应用程序注销并显示当前用户名,如下所示 -

//更多请阅读:https://www.yiibai.com/spring-boot/spring_boot_securing_web_applications.html

主 Spring Boot应用程序的代码如下 -

代码语言:javascript
复制
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);
   }
}

在Web浏览器中访问URL => http://localhost:8080/ ,将看到如下图所示。

输入用户名和密码(user/password),然后点击登录 -

看到问候语如下:

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-11-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 落叶飞翔的蜗牛 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 保护Web应用程序
相关产品与服务
多因子身份认证
多因子身份认证(Multi-factor Authentication Service,MFAS)的目的是建立一个多层次的防御体系,通过结合两种或三种认证因子(基于记忆的/基于持有物的/基于生物特征的认证因子)验证访问者的身份,使系统或资源更加安全。攻击者即使破解单一因子(如口令、人脸),应用的安全依然可以得到保障。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档