Spring Boot 使用 JWT 实现身份认证和授权

JSON Web Token(JWT)是一种用于在网络应用之间安全传递信息的开放标准。它使用了一种紧凑且独立于语言的方式在各方之间传递信息,通常用于在客户端和服务器之间验证用户身份和授权访问资源。本文将介绍如何在Spring Boot中使用JWT实现身份认证和授权。

spring boot security oauth2 构建请求参数_spring boot

什么是JWT?

JWT是一个轻量级的令牌(token)协议,它使用JSON对象作为负载(payload)来传递信息,并使用数字签名(digital signature)来验证其真实性。JWT通常包括以下三个部分:

  1. Header(头部):包含了令牌的类型和使用的签名算法。
  2. Payload(负载):包含了一些声明(claims),例如用户ID、过期时间等。
  3. Signature(签名):用于验证令牌的真实性,确保它未被篡改。

JWT的主要优势在于它的轻量性、易于使用以及可扩展性。它可以在不同的应用程序之间安全传递信息,无需每次都访问数据库或使用其他形式的验证。

Spring Boot 中使用JWT的步骤

要在Spring Boot应用程序中使用JWT,需要执行以下步骤:

步骤1:添加依赖项

首先,您需要在pom.xml文件中添加以下依赖项以使用JWT库:

<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt-api</artifactId>
    <version>0.11.2</version>
</dependency>
<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt-impl</artifactId>
    <version>0.11.2</version>
</dependency>

步骤2:创建JWT工具类

接下来,您需要创建一个JWT工具类,用于生成和验证JWT令牌。以下是一个示例的JWT工具类:

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class JwtUtils {

    private final String secret = "your_secret_key";
    private final long expirationTime = 86400000; // 1 day in milliseconds

    // 生成JWT令牌
    public String generateToken(String username) {
        Date now = new Date();
        Date expirationDate = new Date(now.getTime() + expirationTime);

        Map<String, Object> claims = new HashMap<>();
        claims.put("sub", username);

        return Jwts.builder()
                .setClaims(claims)
                .setIssuedAt(now)
                .setExpiration(expirationDate)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }

    // 从JWT令牌中提取用户名
    public String extractUsername(String token) {
        Claims claims = Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();
        return claims.getSubject();
    }

    // 验证JWT令牌
    public boolean validateToken(String token) {
        try {
            Jwts.parser().setSigningKey(secret).parseClaimsJws(token);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

在上述代码中,JwtUtils类负责生成、解析和验证JWT令牌。

步骤3:创建身份认证和授权逻辑

您需要创建身份认证和授权逻辑以确保只有合法用户可以访问受保护的资源。可以使用Spring Security等安全框架来实现这些功能。以下是一个示例的Spring Security配置类,用于使用JWT进行身份认证和授权:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
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.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final JwtRequestFilter jwtRequestFilter;
    private final UserDetailsService userDetailsService;

    public SecurityConfig(JwtRequestFilter jwtRequestFilter, UserDetailsService userDetailsService) {
        this.jwtRequestFilter = jwtRequestFilter;
        this.userDetailsService = userDetailsService;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
            .authorizeRequests()
                .antMatchers("/authenticate").permitAll() // 放行认证接口
                .anyRequest().authenticated() // 所有其他请求需要认证
            .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
    }
}

在上述代码中,SecurityConfig类配置了哪些端点需要进行身份认证,以及如何使用JWT进行认证。

步骤4:创建认证控制器

创建一个认证控制器,它负责生成JWT令牌并将其返回给客户端。以下是一个示例的认证控制器:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

@RestController
public class AuthController {

    private final AuthenticationManager authenticationManager;
    private final JwtUtils jwtUtils;
    private final UserDetailsService userDetailsService;

    @Autowired
    public AuthController(AuthenticationManager authenticationManager, JwtUtils jwtUtils, UserDetailsService userDetailsService) {
        this.authenticationManager = authenticationManager;
        this.jwtUtils = jwtUtils;
        this.userDetailsService =

 userDetailsService;
    }

    @PostMapping("/authenticate")
    public ResponseEntity<?> createAuthenticationToken(@RequestBody AuthRequest authenticationRequest) throws Exception {
        authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword());

        final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername());

        final String jwt = jwtUtils.generateToken(userDetails.getUsername());

        return ResponseEntity.ok(new AuthResponse(jwt));
    }

    private void authenticate(String username, String password) throws Exception {
        try {
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
        } catch (Exception e) {
            throw new Exception("Invalid username or password");
        }
    }
}

在上述代码中,AuthController类包含了生成JWT令牌的逻辑。

步骤5:创建受保护的资源和授权逻辑

最后,您可以创建受保护的资源和相应的授权逻辑。在Spring Security配置中,您可以使用@PreAuthorize注解来限制访问资源的权限。以下是一个示例:

@RestController
public class ResourceController {

    @GetMapping("/hello")
    @PreAuthorize("hasRole('USER')")
    public String helloUser() {
        return "Hello, User!";
    }

    @GetMapping("/admin")
    @PreAuthorize("hasRole('ADMIN')")
    public String helloAdmin() {
        return "Hello, Admin!";
    }
}

在上述代码中,@PreAuthorize注解确保只有具有相应角色的用户可以访问/hello/admin端点。

测试JWT认证和授权

现在,您可以测试JWT认证和授权功能。首先,您可以使用/authenticate端点来获取JWT令牌,然后将该令牌包含在请求的Authorization头中,以访问受保护的资源。如果令牌有效并且用户具有所需的权限,将允许访问资源。

结论

JWT是一种强大的工具,可用于实现身份认证和授权功能。在Spring Boot中使用JWT可以简化安全性的实现,使您能够轻松地保护应用程序的资源。通过遵循本文中的步骤,您可以在Spring Boot应用程序中成功使用JWT。