当前位置:   article > 正文

SpringBoot 集成SpringSecurity JWT_springboot集成springsecurity+jwt

springboot集成springsecurity+jwt

1. 简介

今天ITDragon分享一篇在Spring Security 框架中使用JWT,以及对失效Token的处理方法。

1.1 SpringSecurity

Spring Security 是Spring提供的安全框架。提供认证、授权和常见的攻击防护的功能。功能丰富和强大。

Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications.

1.2 OAuth2

OAuth(Open Authorization)开放授权是为用户资源的授权定义一个安全、开放的标准。而OAuth2是OAuth协议的第二个版本。OAuth常用于第三方应用授权登录。在第三方无需知道用户账号密码的情况下,获取用户的授权信息。常见的授权模式有:授权码模式、简化模式、密码模式和客户端模式。

1.3 JWT

JWT(json web token)是一个开放的标准,它可以在各方之间作为JSON对象安全地传输信息。可以通过数字签名进行验证和信任。JWT可以解决分布式系统登陆授权、单点登录跨域等问题。

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object.

2. SpringBoot 集成 SpringSecurity

SpringBoot 集成Spring Security 非常方便,也是简单的两个步骤:导包和配置

2.1 导入Spring Security 库

作为Spring的自家项目,只需要导入spring-boot-starter-security 即可

compile('org.springframework.boot:spring-boot-starter-security')

2.2 配置Spring Security

第一步:创建Spring Security Web的配置类,并继承web应用的安全适配器WebSecurityConfigurerAdapter。

第二步:重写configure方法,可以添加登录验证失败处理器、退出成功处理器、并按照ant风格开启拦截规则等相关配置。

第三步:配置默认或者自定义的密码加密逻辑、AuthenticationManager、各种过滤器等,比如JWT过滤器。

配置代码如下:

`package com.itdragon.server.config

import com.itdragon.server.security.service.ITDragonJwtAuthenticationEntryPoint
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpMethod
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder

@Configuration
@EnableWebSecurity
class ITDragonWebSecurityConfig: WebSecurityConfigurerAdapter() {

@Autowired
lateinit var authenticationEntryPoint: ITDragonJwtAuthenticationEntryPoint

/**
 * 配置密码编码器
 */
@Bean
fun passwordEncoder(): PasswordEncoder{
    return BCryptPasswordEncoder()
}

override fun configure(http: HttpSecurity) {
    // 配置异常处理器
    http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
    		// 配置登出逻辑
            .and().logout()
            .logoutSuccessHandler(logoutSuccessHandler)
            // 开启权限拦截
            .and().authorizeRequests()
            // 开放不需要拦截的请求
            .antMatchers(HttpMethod.POST, "/itdragon/api/v1/user").permitAll()
            // 允许所有OPTIONS请求
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
            // 允许静态资源访问
            .antMatchers(HttpMethod.GET,
                    "/",
            
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/885802?site
推荐阅读
相关标签
  

闽ICP备14008679号