![](/img/trans.png)
[英]springboot - basic ActiveDirectory auth then JWT token
[英]Both JWT and form auth in one SpringBoot app?
我有一個使用spring security的spring boot應用程序。我實現了基於表單的auth,並且運行良好。 我希望該應用程序可以作為我構建的角度應用程序的后端。我對CORS有所了解,但是建議如何將JWT auth添加到現有的spring boot應用程序中。
我建議使用AOP概念來驗證/驗證您的jwt令牌。
首先,您需要創建一個自定義注釋。 命名為JWTsecured
@Component
@Target(value = {ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface JwtSecured {
}
現在,您有了一個控制器,其中的api需要通過JWT保護。
@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一起使用的方式。 還有其他幾種方法。 隨時聯系
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.