【问题标题】:why my security strategy works once on 2 calls为什么我的安全策略在 2 次通话中有效
【发布时间】:2019-07-09 15:53:18
【问题描述】:

我使用 REST API(使用 Spring boot 和 Jhipster)上传文件,并以 Async 方法启动处理。

@Async("taskExecutor")
public void importArticle(HttpServletRequest request,
                          ImportFichierDTO importFichierDTO,
                          List<Article> articles,
                          Boolean update,
                          Long userId) {
    log.debug("Request to importCompteur pour article");

在这个处理中,我需要对连接的用户(使用 JWT)进行数据控制。

我使用此链接 (https://github.com/eugenp/tutorials/blob/master/spring-security-rest/src/main/java/org/baeldung/spring/SecurityJavaConfig.java) 来配置策略

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@Import(SecurityProblemSupport.class)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    private final AuthenticationManagerBuilder authenticationManagerBuilder;
    private final UserDetailsService userDetailsService;
    private final TokenProvider tokenProvider;
    private final CorsFilter corsFilter;

    private final SecurityProblemSupport problemSupport;

    public SecurityConfiguration(AuthenticationManagerBuilder authenticationManagerBuilder, UserDetailsService userDetailsService, TokenProvider tokenProvider, CorsFilter corsFilter, SecurityProblemSupport problemSupport) {
        this.authenticationManagerBuilder = authenticationManagerBuilder;
        this.userDetailsService = userDetailsService;
        this.tokenProvider = tokenProvider;
        this.corsFilter = corsFilter;
       this.problemSupport = problemSupport;
SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);

}

当我第一次调用我的 API 时,一切正常。第二次,连接用户的空数据出现异常 第三次,全部工作,以此类推……

有堆栈跟踪:

java.util.NoSuchElementException: No value present
    at java.util.Optional.get(Optional.java:135)
    at fr.yoni.yvidya.security.AccessChecker.checkUserAccessToEntityThroughtWebService(AccessChecker.java:59)
    at fr.yoni.yvidya.service.impl.EntiteServiceImpl.checkRightsByEntite(EntiteServiceImpl.java:188)
    at fr.yoni.yvidya.service.impl.EntiteServiceImpl.checkExistAndAccess(EntiteServiceImpl.java:238)
    at fr.yoni.yvidya.service.impl.EntiteServiceImpl$$FastClassBySpringCGLIB$$24d83d44.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
    at fr.yoni.yvidya.aop.logging.LoggingAspect.logAround(LoggingAspect.java:85)
    at sun.reflect.GeneratedMethodAccessor430.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at fr.yoni.yvidya.service.impl.EntiteServiceImpl$$EnhancerBySpringCGLIB$$d8e4bae5.checkExistAndAccess(<generated>)
    at fr.yoni.yvidya.service.impl.ArticleServiceImpl.update(ArticleServiceImpl.java:251)
    at fr.yoni.yvidya.service.impl.ArticleServiceImpl$$FastClassBySpringCGLIB$$7ebdd41.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
    at fr.yoni.yvidya.aop.logging.LoggingAspect.logAround(LoggingAspect.java:85)
    at sun.reflect.GeneratedMethodAccessor430.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at fr.yoni.yvidya.service.impl.ArticleServiceImpl$$EnhancerBySpringCGLIB$$809100fc.update(<generated>)
    at fr.yoni.yvidya.service.thread.ImportFichier.importArticle(ImportFichier.java:539)
    at fr.yoni.yvidya.service.thread.ImportFichier$$FastClassBySpringCGLIB$$c2f0d691.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
    at fr.yoni.yvidya.aop.logging.LoggingAspect.logAround(LoggingAspect.java:85)
    at sun.reflect.GeneratedMethodAccessor430.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor.lambda$createWrappedRunnable$1(ExceptionHandlingAsyncTaskExecutor.java:68)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

这里是触发异常的行:

String login = SecurityUtils.getCurrentUserLogin().get();

你有解释吗?

【问题讨论】:

  • 你有这两种情况可以添加stacktrace/log吗?
  • @JonathanJohx 我在帖子中添加了堆栈跟踪

标签: java spring-boot spring-security jhipster


【解决方案1】:

在您发布之前和之后,至少有 2 个错误报告为此提出:

SecurityUtils.getCurrentUserLogin() with oauth2 does not return the current user

SecurityUtils .getCurrentUserLogin in a microservice returns "null" with OAuth2 and Okta

如果您仍然遇到此问题,您可以尝试解决方法来获取用户的身份验证:

SecurityContextHolder.getContext().getAuthentication()

看看Baeldung - Retrieve User Information in Spring Security

这段糟糕的代码(无类型/空检查)获取了身份验证的所有属性,即令牌中的所有内容:

Map<String, Object> attrs = ((OAuth2AuthenticatedPrincipal)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getAttributes();

【讨论】:

    猜你喜欢
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2020-05-19
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    相关资源
    最近更新 更多