Spring Boot如何集成Security实现安全认证

安全 应用安全
前面介绍了Spring Boot 使用JWT实现Token验证,其实Spring Boot 有完整的安全认证框架:Spring Security。接下来我们介绍如何集成Security 实现安全验证。

[[442464]]

前面介绍了Spring Boot 使用JWT实现Token验证,其实Spring Boot 有完整的安全认证框架:Spring Security。接下来我们介绍如何集成Security 实现安全验证。

一、Security简介

安全对于企业来说至关重要,必要的安全认证为企业阻挡了外部非正常的访问,保证了企业内部数据的安全。

当前,数据安全问题越来越受到行业内公司的重视。数据泄漏很大一部分原因是非正常权限访问导致的,于是使用合适的安全框架保护企业服务的安全变得非常紧迫。在Java领域,Spring Security无疑是最佳选择之一。

Spring Security 是 Spring 家族中的一个安全管理框架,能够基于 Spring 的企业应用系统提供声明式的安全访问控制解决方案。它提供了一组可以在Spring应用系统中灵活配置的组件,充分利用了 Spring的IoC、DI和AOP等特性,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

二、Spring Boot对Security的支持

虽然,在Spring Boot出现之前,Spring Security已经发展多年,但是使用并不广泛。安全管理这个领域一直是Shiro的天下,因为相对于Shiro,在项目中集成Spring Security还是一件麻烦的事情,所以Spring Security虽然比Shiro强大,但是却没有Shiro受欢迎。

随着Spring Boot的出现,Spring Boot 对Spring Security 提供了自动化配置方案,可以零配置使用 Spring Security。这使得Spring Security重新焕发新的活力。

Spring Boot 提供了集成 Spring Security 的组件包

spring-boot-starter-security,方便我们在 Spring Boot 项目中使用 Spring Security进行权限控制。

三、集成Security

在Spring Boot 项目中集成Spring Boot Security 非常简单,只需在项目中增加Spring Boot Security的依赖即可。下面通过示例演示Spring Boot中基础Security的登录验证。

1. 添加依赖

Spring Boot 提供了集成 Spring Security 的组件包

spring-boot-starter-security,方便我们在 Spring Boot 项目中使用 Spring Security。

  1. <dependency> 
  2.     <groupId>org.springframework.boot</groupId> 
  3.     <artifactId>spring-boot-starter-web</artifactId> 
  4. </dependency> 
  5. <dependency> 
  6.     <groupId>org.springframework.boot</groupId> 
  7.     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
  8. </dependency> 
  9. <dependency> 
  10.     <groupId>org.springframework.boot</groupId> 
  11.     <artifactId>spring-boot-starter-security</artifactId> 
  12. </dependency> 

上面除了引入Security组件外,因为我们要做Web系统的权限验证,所以还添加了Web和Thymeleaf组件。

2. 配置登录用户名和密码

用户名和密码在application.properties中进行配置。

  1. # security 
  2. spring.security.user.name=admin 
  3. spring.security.user.password=admin 

在application.properties配置文件中增加了管理员的用户名和密码。

3. 添加Controller

创建SecurityController 类,在类中添加访问页面的入口。

  1. @Controller 
  2. public class SecurityController { 
  3.     @RequestMapping("/"
  4.     public String index() { 
  5.         return "index"
  6.     } 

4. 创建前端页面

在resources/templates 目录下创建页面 index.html,这个页面就是具体的需要增加权限控制的页面,只有登录了才能进入此页。

  1. <!DOCTYPE html> 
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  3. <body> 
  4. <h1>Hello</h1> 
  5. <p>我是登录后才可以看的页面</p> 
  6. </body> 
  7. </html> 

我是登录后才可以看的页面 

5. 测试验证

配置完成后,重启项目,访问地址:http://localhost:8080/,页面会自动弹出一个登录框,如下图所示。 

Security安全认证 | Spring Boot如何集成Security实现安全认证 

系统自动跳转到Spring Security默认的登录页面,输入之前配置的用户名和密码就可以登录系统,登录后的页面如下图所示。 

Security安全认证 | Spring Boot如何集成Security实现安全认证 

通过上面的示例,我们看到Spring Security自动给所有访问请求做了登录保护,实现了页面权限控制。

四、登录验证

前面演示了在Spring Boot项目中集成Spring Security 实现简单的登录验证功能,在实际项目使用过程中,可能有的功能页面不需要进行登录验证,而有的功能页面只有进行登录验证才能访问。下面通过完整的示例程序演示如何实现Security的登录认证。

1. 创建页面content.html

先创建页面content.html,此页面只有登录用户才可查看,否则会跳转到登录页面,登录成功后才能访问。示例代码如下:

  1. <!DOCTYPE html> 
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
  3. <body> 
  4. <h1>content</h1> 
  5. <p>我是登录后才可以看的页面</p> 
  6. <form method="post" action="/logout"
  7.     <button type="submit">退出</button> 
  8. </form> 
  9. </body> 
  10. </html> 

在上面的示例中,我们看到退出使用post请求,因为Security退出请求默认只支持post 。

2. 修改index.html 页面

修改之前的index.html页面,增加登录按钮。

  1. <p>点击 <a th:href="@{/content}">这里</a>进入管理页面</p> 

在上面的示例中,index页面属于公共页面,无权限验证,从index页面进入content页面时需要登录验证。

3. 修改Controller控制器

修改之前的SecurityController控制器,增加content页面路由地址,示例代码如下:

  1. @RequestMapping("/"
  2. public String index() { 
  3.     return "index"
  4.   
  5. @RequestMapping("/content"
  6. public String content() { 
  7.     return "content"

4. 创建 SecurityConfig 类

创建 Security的配置文件SecurityConfig类,它继承于:

  1. @Configuration 
  2. @EnableWebSecurity 
  3. public class SecurityConfig extends WebSecurityConfigurerAdapter { 
  4.     @Override 
  5.     protected void configure(HttpSecurity http) throws Exception { 
  6.         http.authorizeRequests() 
  7.             .antMatchers("/""/home").permitAll() 
  8.             .anyRequest().authenticated() 
  9.             .and() 
  10.             .formLogin() 
  11.             .permitAll() 
  12.             .and() 
  13.             .logout() 
  14.             .permitAll() 
  15.             .and() 
  16.             .csrf() 
  17.             .ignoringAntMatchers("/logout"); 
  18.     } 

在上面的示例程序中,SecurityConfig类中配置 index.html 可以直接访问,但 content.html 需要登录后才可以查看,没有登录的自动跳转到登录页面。

  • @EnableWebSecurity:开启 Spring Security 权限控制和认证功能。
  • antMatchers("/", "/home").permitAll():配置不用登录可以访问的请求。
  • anyRequest().authenticated():表示其他的请求都必须有权限认证。
  • formLogin():定制登录信息。
  • loginPage("/login"):自定义登录地址,若注释掉,则使用默认登录页面。
  • logout():退出功能,Spring Security自动监控了/logout。
  • ignoringAntMatchers("/logout"):Spring Security 默认启用了同源请求控制,在这里选择忽略退出请求的同源限制。

5. 测试验证

修改完成之后重启项目,访问地址http://localhost:8080/可以看到 index 页面的内容,单击链接跳转到content页面时会自动跳转到登录页面,登录成功后才会自动跳转到

http://localhost:8080/content,在 content 页面单击“退出”按钮,会退出登录状态,跳转到登录页面并提示已经退出。 

Security安全认证 | Spring Boot如何集成Security实现安全认证 

登录、退出、请求受限页面退出后跳转到登录页面是常用的安全控制案例,也是账户系统基本的安全保障。

最后 

以上,我们就把Spring Boot如何集成Security实现安全认证介绍完了。

责任编辑:武晓燕 来源: 今日头条
相关推荐

2020-07-14 11:00:12

Spring BootRedisJava

2020-09-02 17:28:26

Spring Boot Redis集成

2021-08-05 10:40:37

加密方案Spring

2024-01-16 08:17:29

Mybatis验证业务

2023-12-08 12:12:21

2023-10-16 11:12:29

2022-06-16 10:38:24

URL权限源代码

2022-06-28 15:04:32

容器Docker

2022-06-28 15:06:35

容器Spring

2021-04-26 08:54:17

Spring BootSecurity防重登录

2018-11-02 15:45:41

Spring BootRedis数据库

2019-04-15 08:32:25

Spring Boot日志门面模式

2023-05-26 01:05:10

2021-04-28 06:26:11

Spring Secu功能实现源码分析

2024-02-02 09:08:32

2023-01-10 07:52:15

2021-10-18 12:04:22

Spring BootJava开发

2021-10-18 10:36:31

Spring Boot插件Jar

2023-11-01 08:58:10

2020-06-30 07:58:39

微服务Spring BootCloud
点赞
收藏

51CTO技术栈公众号