SpringBoot 整合 JWT 实现 Token 验证

前言

        在Spring Security整合oauth2实现认证token也不满足实际生产需求的时候,可以整合Jwt实现token认证,完全手写获取token,认证token的方法。


Maven依赖包

<dependency>
    <groupId>com.auth0</groupId>
    <artifactId>java-jwt</artifactId>
    <version>3.11.0</version>
</dependency>

业务实现

1、创建Jwt生成验证token工具类

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.bw.dsm.entity.base.TokenEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class AppJwtUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppJwtUtil.class);

    public static final String secretKey = "asdfghjklsed";

    public static final long tokenExpireTime = 7200000;//#token过期时间:单位毫秒 2个小时

    public static final long refreshTokenExpireTime = 72000000000L;//refreshToken过期时间:单位毫秒

    public static final String jwtRefreshTokenKeyFormat = "jwt_refresh_token:";//refreshToken 存储key

    public static final String jwtBlacklistKeyFormat = "jwt_black_key:";//#token黑名单 存储key

    /**
     * 生成jwt
     *
     * @param platformID
     * @return
     */
    public static String buildJWT(String platformID,String platformSecret) {

        Date now = new Date();
        Algorithm algo = Algorithm.HMAC256(secretKey);
        String token = JWT.create()
                .withIssuer("MING")
                .withIssuedAt(now)
                .withExpiresAt(new Date(now.getTime() + tokenExpireTime))
                .withClaim("platformID", platformID)// 保存身份标识
                .withClaim("platformSecret",platformSecret)
                .sign(algo);
        return token;
    }

    /**
     * JWT验证
     *
     * @param token
     * @return userName
     */
    public static TokenEntity verifyJWT(String token) {
        TokenEntity tokenEntity = new TokenEntity();
        try {
            Algorithm algorithm = Algorithm.HMAC256(secretKey);
            JWTVerifier verifier = JWT.require(algorithm)
                    .withIssuer("MING")
                    .build();
            DecodedJWT jwt = verifier.verify(token);
            tokenEntity.setPlatformID(jwt.getClaim("platformID").asString());
            tokenEntity.setPlatformSecret(jwt.getClaim("platformSecret").asString());
            return tokenEntity;
        } catch (JWTVerificationException e) {
            LOGGER.error(e.getMessage(), e);
            return tokenEntity;
        }
    }

}

2、创建/oauth/token接口获取token

@RestController
@RequestMapping("/interaction/v1")
public class IndexController {

    @Value("${base.param.platformID}")
    private String platformID;

    @Value("${base.param.platformSecret}")
    private String platformSecret;

    @RequestMapping(value = "/oauth/token",method = RequestMethod.POST)
    public String getToken(@RequestBody String getParamInfo){
        MsgResult msg = new MsgResult();
        TokenData tokenData = new TokenData();
        getParamInfo = getParamInfo.replace("[+]","+");
        try {
            // 校验生成服务消息主题
            String data = Common.validMessage( getParamInfo );
            TokenEntity tokenEntity = Common.makeEntity(data,TokenEntity.class);
            if (!platformID.equals(tokenEntity.getPlatformID())){
                tokenData.setFailReason(1);
                throw new BusinessException("4004","平台标识错误");
            }
            if (!platformSecret.equals(tokenEntity.getPlatformSecret())){
                tokenData.setFailReason(2);
                throw new BusinessException("4004","平台秘钥错误");
            }
            String token = AppJwtUtil.buildJWT(
                    tokenEntity.getPlatformID(),tokenEntity.getPlatformSecret());
            tokenData.setSuccstat(0);
            tokenData.setFailReason(0);
            tokenData.setAccessToken( token );
            tokenData.setExpiresIn(7200000);
            msg.setCode("0");
            msg.setMesg("请求成功");
        } catch (BusinessException b){
            msg.setCode(b.getCode());
            msg.setMesg(b.getMessage());
            tokenData.setSuccstat(1);
        } catch (Exception e){
            msg.setCode("500");
            msg.setMesg(e.getMessage());
            tokenData.setSuccstat(1);
            tokenData.setFailReason(3);
        }
        msg.setData(Common.AESJaiMi(Common.makeJson(tokenData)));
        return Common.msgResult(msg);
    }
}

 这里是我的生产环境上的获取token接口,主要功能就是根据传递过来的参数使用Jwt工具类创建token,再组合成规定的返回格式返回给对方。

3、创建接口拦截验证token类

@Component
public class JwtInterceptors implements HandlerInterceptor {

    @Value("${base.param.platformID}")
    private String platformID;

    @Value("${base.param.platformSecret}")
    private String platformSecret;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        MsgResult msg = new MsgResult();
        //获取请求头部令牌
        String auth = request.getHeader("Authorization");
        try {
            if (StringUtils.isNotBlank(auth) && auth.indexOf("Bearer") >= 0) {
                auth = auth.substring("Bearer ".length() - 1, auth.length()).trim();
            } else {
                throw new BusinessException("4002","Token错误");
            }
            TokenEntity tokenEntity = AppJwtUtil.verifyJWT(auth);
            if (!platformID.equals(tokenEntity.getPlatformID())){
                throw new BusinessException("4002","Token错误");
            }
            if (!platformSecret.equals(tokenEntity.getPlatformSecret())){
                throw new BusinessException("4002","Token错误");
            }
//            //验证令牌
//            DecodedJWT decodedJWT = JwtUtils.verify(token);
            return true;
        } catch (BusinessException b){
            msg.setCode(b.getCode());
            msg.setMesg(b.getMessage());
            msg.setData(Common.AESJaiMi(auth));
        } catch (Exception e){
            msg.setCode("500");
            msg.setMesg(e.getMessage());
            msg.setData(Common.AESJaiMi(auth));
        }
        // 转json
        String json = Common.msgResult(msg);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().println(json);
        return false;
    }
}

基于HandlerInterceptor的实现类,对接口进行拦截,然后获取接口传递过来的token,用Jwt工具类进行解密,如果token验证通过则返回true并放行接口,验证不通过则返回错误信息。

4、创建拦截路径类

@Configuration
public class WebConfigurer implements WebMvcConfigurer {

    @Autowired
    private JwtInterceptors jwtInterceptors;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptors)
                // 放行接口
                .excludePathPatterns("/interaction/v1/oauth/token")
        .addPathPatterns("/**");
    }
}

5、测试获取token接口

至此功能实现。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot 集成 JWT 验证 Token 的步骤如下: 1. 引入相关依赖包 2. 实现自定义 JWT 验证过滤器 3. 在 Security 配置中配置 JWT 验证过滤器 4. 在需要验证的接口上添加 @PreAuthorize 注解 5. 处理 Token 相关操作(生成、刷新、验证等) 请注意,以上步骤仅是大致流程,具体实现可能会有所不同。如需细节请参考Spring Boot 官方文档或搜索相关教程。 ### 回答2: Spring Boot是一款非常方便的框架,可以快速搭建应用程序。同样的,JWT(JSON Web Token)也是一种非常流行的身份验证机制,使用标准的JSON格式,并且使用了数字签名方式验证身份。本文将介绍Spring Boot整合JWT验证Token。 首先,通过pom.xml文件引入相关依赖包: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.9.1</version> </dependency> ``` 接下来创建JWTUtils类来处理Token的创建与验证: ``` @Component public class JWTUtils { // 密钥 private static final String SECRET = "mySecret"; // 过期时间毫秒 private static final long EXPIRATION_TIME = 1800000; // 创建token public static String createToken(String username, String role) { return Jwts.builder() .setSubject(username) .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME)) .claim("roles", role) .signWith(SignatureAlgorithm.HS512, SECRET) .compact(); } // 验证token public static boolean validateToken(String token) { try { Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token); return true; } catch (SignatureException | ExpiredJwtException e) { return false; } } // 获取用户名 public static String getUsernameFromToken(String token) { return Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody().getSubject(); } // 获取角色 public static String getRoleFromToken(String token) { return Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody().get("roles").toString(); } } ``` 其中,createToken方法用于创建Token,validateToken方法用于验证Token,getUsernameFromToken和getRoleFromToken用于获取Token中的信息。 接下来,在Spring Boot中配置WebSecurityConfig: ``` @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() .authorizeRequests() .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() // 跨域请求会先进行一次options请求 .antMatchers("/auth/**").permitAll() // 登录注册接口放行 .anyRequest().authenticated() .and() .addFilter(new JWTAuthenticationFilter(authenticationManager())) .addFilter(new JWTAuthorizationFilter(authenticationManager())) .headers().cacheControl(); } @Override public void configure(WebSecurity web) { web.ignoring().antMatchers( "/error", "/v2/api-docs", "/configuration/ui", "/swagger-resources/**", "/configuration/**", "/swagger-ui.html", "/webjars/**"); } @Autowired public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { authenticationManagerBuilder.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder()); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } } ``` 其中,configure方法配置了安全规则,addFilter方法添加了JWT的过滤器,configureAuthentication方法配置用户认证方式。 最后,在Controller中使用接口进行调用: ``` @RestController public class UserController { @Autowired private UserService userService; @PostMapping("/auth/register") public String register(@RequestBody UserDTO userDTO) { if (userService.register(userDTO)) { return "注册成功"; } else { return "注册失败"; } } @PostMapping("/auth/login") public ResponseEntity<TokenDTO> login(@RequestParam String username, @RequestParam String password) { String role = "ROLE_USER"; // 这里演示只有ROLE_USER角色 String token = JWTUtils.createToken(username, role); return ResponseEntity.ok(new TokenDTO(token)); } @GetMapping("/user/info") public String userInfo(Authentication authentication) { String username = authentication.getName(); String role = JWTUtils.getRoleFromToken(((JwtUserDetails) authentication.getPrincipal()).getToken()); return username + "-" + role; } } ``` 其中,login方法用于登录获取Token,userInfo方法用于获取当前用户信息。这里使用了Spring Security提供的authentication对象来获取用户信息。 综上,Spring Boot整合JWT验证Token并不复杂,通过相关依赖包、JWTUtils、WebSecurityConfig和Controller的配置,即可完成Token身份验证,保证接口的安全性。 ### 回答3: Spring Boot是一个Java开发框架,它可以减少Web应用程序的开发时间和复杂性。在开发Web应用程序时,安全性是一个关键问题,可以使用JWT(Json Web Token)来提高应用程序的安全性。 JWT是一种基于JSON的安全令牌,可以在用户与应用程序之间传递信息。JWT包含了许多有用的信息,例如用户的ID、角色、权限等等。在Spring Boot中,我们可以使用jwt.io上的库来生成和解码JWT令牌。 首先,在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> <scope>runtime</scope> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>0.11.2</version> <scope>runtime</scope> </dependency> ``` 接下来,我们需要创建一个JWTUtils工具类。该类将用于编码、解码和验证JWT令牌。实现代码如下: ``` import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.Date; @Component public class JWTUtils { @Value("${jwt.secret}") private String secret; @Value("${jwt.expiration}") private Long expiration; public String generateToken(String username) { Date now = new Date(); Date expiryDate = new Date(now.getTime() + expiration * 1000); return Jwts.builder() .setSubject(username) .setIssuedAt(now) .setExpiration(expiryDate) .signWith(SignatureAlgorithm.HS512, secret) .compact(); } public String getUsernameFromToken(String token) { Claims claims = Jwts.parser() .setSigningKey(secret) .parseClaimsJws(token) .getBody(); return claims.getSubject(); } public boolean validateToken(String token, UserDetails userDetails) { String username = getUsernameFromToken(token); return (username.equals(userDetails.getUsername()) && !isTokenExpired(token)); } private boolean isTokenExpired(String token) { Date expiryDate = Jwts.parser() .setSigningKey(secret) .parseClaimsJws(token) .getBody() .getExpiration(); return expiryDate.before(new Date()); } } ``` 在上面的代码中,我们使用@Value注释从application.properties文件中读取秘密和过期时间。generateToken方法是用于生成JWT令牌的方法。setSubject()定义了JWT令牌的主题,setIssuedAt()是JWT生成时间,setExpiration()是JWT令牌的超时时间。最后,我们使用signWith()进行签名和压缩。 getUsernameFromToken()方法获取JWT令牌中的用户名。validateToken()用于验证JWT令牌是否有效。isTokenExpired()方法检查JWT令牌是否已过期。 为了让Spring Boot知道我们在哪里可以找到JWTUtils类,我们需要在Application.java类中添加@ComponentScan注解,如下所示: ``` @SpringBootApplication @ComponentScan({"com.example.jwt"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 最后,我们在Spring Boot中创建一个Controller类。该类有一个login()方法,该方法接收用户名和密码并验证该用户是否存在。如果用户存在,则生成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.AuthenticationException; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.web.bind.annotation.*; import com.example.jwt.JWTUtils; @RestController @RequestMapping("/api/auth") public class AuthController { @Autowired private AuthenticationManager authenticationManager; @Autowired private JWTUtils jwtUtils; @Autowired private UserDetailsService userDetailsService; @PostMapping("/login") public ResponseEntity<?> login(@RequestBody LoginRequest loginRequest) throws AuthenticationException { try { authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword())); } catch (UsernameNotFoundException e) { throw new RuntimeException("User not found"); } final UserDetails userDetails = userDetailsService.loadUserByUsername(loginRequest.getUsername()); final String token = jwtUtils.generateToken(userDetails.getUsername()); return ResponseEntity.ok(new AuthResponse(token)); } } ``` 在上面的代码中,我们使用@Autowired注释从其他类中获取所需的依赖项。login()方法中,我们使用authenticationManager.authenticate()方法验证用户是否存在。如果该用户不存在,则会抛出一个异常。然后,我们使用userDetailsService.loadUserByUsername()方法加载用户详细信息。最后,我们调用JWTUtils.generateToken()方法生成JWT令牌。 在上面的代码中,我们还创建了一个名为AuthResponse的简单类,该类是返回的响应对象,包含JWT令牌。 ``` public class AuthResponse { private String token; public AuthResponse(String token) { this.token = token; } public String getToken() { return token; } public void setToken(String token) { this.token = token; } } ``` 以上就是Spring Boot整合JWT验证token的简单代码示例。该代码示例可以让开发者更加容易地理解Spring Boot如何使用JWT来增强应用程序的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值