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

架构师精进

共 3904字,需浏览 8分钟

 · 2021-12-18

前面介绍了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。

org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-thymeleaforg.springframework.bootspring-boot-starter-security
上面除了引入Security组件外,因为我们要做Web系统的权限验证,所以还添加了Web和Thymeleaf组件。


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

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

# securityspring.security.user.name=adminspring.security.user.password=admin
在application.properties配置文件中增加了管理员的用户名和密码。


3. 添加Controller

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

@Controllerpublic class SecurityController {    @RequestMapping("/")    public String index() {        return "index";    }}


4. 创建前端页面

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

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><body><h1>Helloh1><p>我是登录后才可以看的页面p>body>html>

5. 测试验证

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

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

            


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


四、登录验证

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

1. 创建页面content.html

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

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"><body><h1>contenth1><p>我是登录后才可以看的页面p><form method="post" action="/logout">    <button type="submit">退出button>form>body>html>
在上面的示例中,我们看到退出使用post请求,因为Security退出请求默认只支持 post 。


2. 修改index.html 页面

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

<p>点击 <a th:href="@{/content}">这里a>进入管理页面p>
在上面的示例中,index页面属于公共页面,无权限验证,从index页面进入content页面时需要登录验证。


3. 修改Controller控制器

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

@RequestMapping("/")public String index() {    return "index";} @RequestMapping("/content")public String content() {    return "content";}


4. 创建 SecurityConfig 类

创建 Security的配置文件SecurityConfig类,它继承于 WebSecurityConfigurerAdapter,现自定义权限验证配置。示例代码如下:

@Configuration@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter {    @Override    protected void configure(HttpSecurity http) throws Exception {        http.authorizeRequests()            .antMatchers("/", "/home").permitAll()            .anyRequest().authenticated()            .and()            .formLogin()            .permitAll()            .and()            .logout()            .permitAll()            .and()            .csrf()            .ignoringAntMatchers("/logout");    }}
在上面的示例程序中,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 页面单击“退出”按钮,会退出登录状态,跳转到登录页面并提示已经退出。


最后

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


浏览 27
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报