我有一个使用spring security的spring boot应用程序。我实现了基于表单的auth,并且运行良好。我希望该应用程序可以作为我构建的角度应用程序的后端。我对CORS有所了解,但是建议如何将JWT auth添加到现有的spring boot应用程序中。
答案 0 :(得分:1)
我建议使用AOP概念来验证/验证您的jwt令牌。
首先需要创建一个自定义注释。将其命名为JWTsecured
@Component
@Target(value = {ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface JwtSecured {
}
现在您有了一个控制器,需要在其中确保api的api安全。
@RestController
public class YourController {
@GetMapping(value="/testApi")
@JWTsecured
public void isTestApi() {
}
}
现在您必须编写一个方面来验证令牌...
@Component
@Aspect
public class JWTsecuredAspect {
@Around(value =" @within(com.JWTSecured) || @annotation(com.JWTSecured)")
public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {
String token = request.getHeader("Authorization");
if(!isTokenValidated(token)){
throw CustomException("Invalid Token.")
}
}
}
这是将其与auth一起使用的方式。 还有其他几种方法。 随时联系